mac80211: cleanup kmalloc/memset -> kcalloc
[linux-2.6/btrfs-unstable.git] / net / mac80211 / ht.c
blob7a38d2e76ca9c6fbf89515fa4a7df5a46fe26360
1 /*
2 * HT handling
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/wireless.h>
18 #include <net/mac80211.h>
19 #include "ieee80211_i.h"
20 #include "sta_info.h"
21 #include "wme.h"
23 void ieee80211_ht_cap_ie_to_sta_ht_cap(struct ieee80211_supported_band *sband,
24 struct ieee80211_ht_cap *ht_cap_ie,
25 struct ieee80211_sta_ht_cap *ht_cap)
27 u8 ampdu_info, tx_mcs_set_cap;
28 int i, max_tx_streams;
30 BUG_ON(!ht_cap);
32 memset(ht_cap, 0, sizeof(*ht_cap));
34 if (!ht_cap_ie)
35 return;
37 ht_cap->ht_supported = true;
39 ht_cap->cap = le16_to_cpu(ht_cap_ie->cap_info) & sband->ht_cap.cap;
40 ht_cap->cap &= ~IEEE80211_HT_CAP_SM_PS;
41 ht_cap->cap |= sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS;
43 ampdu_info = ht_cap_ie->ampdu_params_info;
44 ht_cap->ampdu_factor =
45 ampdu_info & IEEE80211_HT_AMPDU_PARM_FACTOR;
46 ht_cap->ampdu_density =
47 (ampdu_info & IEEE80211_HT_AMPDU_PARM_DENSITY) >> 2;
49 /* own MCS TX capabilities */
50 tx_mcs_set_cap = sband->ht_cap.mcs.tx_params;
52 /* can we TX with MCS rates? */
53 if (!(tx_mcs_set_cap & IEEE80211_HT_MCS_TX_DEFINED))
54 return;
56 /* Counting from 0, therefore +1 */
57 if (tx_mcs_set_cap & IEEE80211_HT_MCS_TX_RX_DIFF)
58 max_tx_streams =
59 ((tx_mcs_set_cap & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
60 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
61 else
62 max_tx_streams = IEEE80211_HT_MCS_TX_MAX_STREAMS;
65 * 802.11n D5.0 20.3.5 / 20.6 says:
66 * - indices 0 to 7 and 32 are single spatial stream
67 * - 8 to 31 are multiple spatial streams using equal modulation
68 * [8..15 for two streams, 16..23 for three and 24..31 for four]
69 * - remainder are multiple spatial streams using unequal modulation
71 for (i = 0; i < max_tx_streams; i++)
72 ht_cap->mcs.rx_mask[i] =
73 sband->ht_cap.mcs.rx_mask[i] & ht_cap_ie->mcs.rx_mask[i];
75 if (tx_mcs_set_cap & IEEE80211_HT_MCS_TX_UNEQUAL_MODULATION)
76 for (i = IEEE80211_HT_MCS_UNEQUAL_MODULATION_START_BYTE;
77 i < IEEE80211_HT_MCS_MASK_LEN; i++)
78 ht_cap->mcs.rx_mask[i] =
79 sband->ht_cap.mcs.rx_mask[i] &
80 ht_cap_ie->mcs.rx_mask[i];
82 /* handle MCS rate 32 too */
83 if (sband->ht_cap.mcs.rx_mask[32/8] & ht_cap_ie->mcs.rx_mask[32/8] & 1)
84 ht_cap->mcs.rx_mask[32/8] |= 1;
88 * ieee80211_enable_ht should be called only after the operating band
89 * has been determined as ht configuration depends on the hw's
90 * HT abilities for a specific band.
92 u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
93 struct ieee80211_ht_info *hti,
94 u16 ap_ht_cap_flags)
96 struct ieee80211_local *local = sdata->local;
97 struct ieee80211_supported_band *sband;
98 struct ieee80211_bss_ht_conf ht;
99 u32 changed = 0;
100 bool enable_ht = true, ht_changed;
101 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
103 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
105 memset(&ht, 0, sizeof(ht));
107 /* HT is not supported */
108 if (!sband->ht_cap.ht_supported)
109 enable_ht = false;
111 /* check that channel matches the right operating channel */
112 if (local->hw.conf.channel->center_freq !=
113 ieee80211_channel_to_frequency(hti->control_chan))
114 enable_ht = false;
116 if (enable_ht) {
117 channel_type = NL80211_CHAN_HT20;
119 if (!(ap_ht_cap_flags & IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
120 (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
121 (hti->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
122 switch(hti->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
123 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
124 channel_type = NL80211_CHAN_HT40PLUS;
125 break;
126 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
127 channel_type = NL80211_CHAN_HT40MINUS;
128 break;
133 ht_changed = conf_is_ht(&local->hw.conf) != enable_ht ||
134 channel_type != local->hw.conf.channel_type;
136 local->oper_channel_type = channel_type;
138 if (ht_changed) {
139 /* channel_type change automatically detected */
140 ieee80211_hw_config(local, 0);
143 /* disable HT */
144 if (!enable_ht)
145 return 0;
147 ht.operation_mode = le16_to_cpu(hti->operation_mode);
149 /* if bss configuration changed store the new one */
150 if (memcmp(&sdata->vif.bss_conf.ht, &ht, sizeof(ht))) {
151 changed |= BSS_CHANGED_HT;
152 sdata->vif.bss_conf.ht = ht;
155 return changed;
158 static void ieee80211_send_addba_request(struct ieee80211_sub_if_data *sdata,
159 const u8 *da, u16 tid,
160 u8 dialog_token, u16 start_seq_num,
161 u16 agg_size, u16 timeout)
163 struct ieee80211_local *local = sdata->local;
164 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
165 struct sk_buff *skb;
166 struct ieee80211_mgmt *mgmt;
167 u16 capab;
169 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
171 if (!skb) {
172 printk(KERN_ERR "%s: failed to allocate buffer "
173 "for addba request frame\n", sdata->dev->name);
174 return;
176 skb_reserve(skb, local->hw.extra_tx_headroom);
177 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
178 memset(mgmt, 0, 24);
179 memcpy(mgmt->da, da, ETH_ALEN);
180 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
181 if (sdata->vif.type == NL80211_IFTYPE_AP)
182 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
183 else
184 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
186 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
187 IEEE80211_STYPE_ACTION);
189 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
191 mgmt->u.action.category = WLAN_CATEGORY_BACK;
192 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
194 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
195 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
196 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
197 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
199 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
201 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
202 mgmt->u.action.u.addba_req.start_seq_num =
203 cpu_to_le16(start_seq_num << 4);
205 ieee80211_tx_skb(sdata, skb, 1);
208 static void ieee80211_send_addba_resp(struct ieee80211_sub_if_data *sdata, u8 *da, u16 tid,
209 u8 dialog_token, u16 status, u16 policy,
210 u16 buf_size, u16 timeout)
212 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
213 struct ieee80211_local *local = sdata->local;
214 struct sk_buff *skb;
215 struct ieee80211_mgmt *mgmt;
216 u16 capab;
218 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
220 if (!skb) {
221 printk(KERN_DEBUG "%s: failed to allocate buffer "
222 "for addba resp frame\n", sdata->dev->name);
223 return;
226 skb_reserve(skb, local->hw.extra_tx_headroom);
227 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
228 memset(mgmt, 0, 24);
229 memcpy(mgmt->da, da, ETH_ALEN);
230 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
231 if (sdata->vif.type == NL80211_IFTYPE_AP)
232 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
233 else
234 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
235 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
236 IEEE80211_STYPE_ACTION);
238 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
239 mgmt->u.action.category = WLAN_CATEGORY_BACK;
240 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
241 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
243 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
244 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
245 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
247 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
248 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
249 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
251 ieee80211_tx_skb(sdata, skb, 1);
254 static void ieee80211_send_delba(struct ieee80211_sub_if_data *sdata,
255 const u8 *da, u16 tid,
256 u16 initiator, u16 reason_code)
258 struct ieee80211_local *local = sdata->local;
259 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
260 struct sk_buff *skb;
261 struct ieee80211_mgmt *mgmt;
262 u16 params;
264 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
266 if (!skb) {
267 printk(KERN_ERR "%s: failed to allocate buffer "
268 "for delba frame\n", sdata->dev->name);
269 return;
272 skb_reserve(skb, local->hw.extra_tx_headroom);
273 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
274 memset(mgmt, 0, 24);
275 memcpy(mgmt->da, da, ETH_ALEN);
276 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
277 if (sdata->vif.type == NL80211_IFTYPE_AP)
278 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
279 else
280 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
281 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
282 IEEE80211_STYPE_ACTION);
284 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
286 mgmt->u.action.category = WLAN_CATEGORY_BACK;
287 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
288 params = (u16)(initiator << 11); /* bit 11 initiator */
289 params |= (u16)(tid << 12); /* bit 15:12 TID number */
291 mgmt->u.action.u.delba.params = cpu_to_le16(params);
292 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
294 ieee80211_tx_skb(sdata, skb, 1);
297 void ieee80211_send_bar(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid, u16 ssn)
299 struct ieee80211_local *local = sdata->local;
300 struct sk_buff *skb;
301 struct ieee80211_bar *bar;
302 u16 bar_control = 0;
304 skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
305 if (!skb) {
306 printk(KERN_ERR "%s: failed to allocate buffer for "
307 "bar frame\n", sdata->dev->name);
308 return;
310 skb_reserve(skb, local->hw.extra_tx_headroom);
311 bar = (struct ieee80211_bar *)skb_put(skb, sizeof(*bar));
312 memset(bar, 0, sizeof(*bar));
313 bar->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
314 IEEE80211_STYPE_BACK_REQ);
315 memcpy(bar->ra, ra, ETH_ALEN);
316 memcpy(bar->ta, sdata->dev->dev_addr, ETH_ALEN);
317 bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
318 bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
319 bar_control |= (u16)(tid << 12);
320 bar->control = cpu_to_le16(bar_control);
321 bar->start_seq_num = cpu_to_le16(ssn);
323 ieee80211_tx_skb(sdata, skb, 0);
326 void ieee80211_sta_stop_rx_ba_session(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid,
327 u16 initiator, u16 reason)
329 struct ieee80211_local *local = sdata->local;
330 struct ieee80211_hw *hw = &local->hw;
331 struct sta_info *sta;
332 int ret, i;
334 rcu_read_lock();
336 sta = sta_info_get(local, ra);
337 if (!sta) {
338 rcu_read_unlock();
339 return;
342 /* check if TID is in operational state */
343 spin_lock_bh(&sta->lock);
344 if (sta->ampdu_mlme.tid_state_rx[tid]
345 != HT_AGG_STATE_OPERATIONAL) {
346 spin_unlock_bh(&sta->lock);
347 rcu_read_unlock();
348 return;
350 sta->ampdu_mlme.tid_state_rx[tid] =
351 HT_AGG_STATE_REQ_STOP_BA_MSK |
352 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
353 spin_unlock_bh(&sta->lock);
355 /* stop HW Rx aggregation. ampdu_action existence
356 * already verified in session init so we add the BUG_ON */
357 BUG_ON(!local->ops->ampdu_action);
359 #ifdef CONFIG_MAC80211_HT_DEBUG
360 printk(KERN_DEBUG "Rx BA session stop requested for %pM tid %u\n",
361 ra, tid);
362 #endif /* CONFIG_MAC80211_HT_DEBUG */
364 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
365 &sta->sta, tid, NULL);
366 if (ret)
367 printk(KERN_DEBUG "HW problem - can not stop rx "
368 "aggregation for tid %d\n", tid);
370 /* shutdown timer has not expired */
371 if (initiator != WLAN_BACK_TIMER)
372 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
374 /* check if this is a self generated aggregation halt */
375 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
376 ieee80211_send_delba(sdata, ra, tid, 0, reason);
378 /* free the reordering buffer */
379 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
380 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
381 /* release the reordered frames */
382 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
383 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
384 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
387 /* free resources */
388 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
389 kfree(sta->ampdu_mlme.tid_rx[tid]);
390 sta->ampdu_mlme.tid_rx[tid] = NULL;
391 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
393 rcu_read_unlock();
398 * After sending add Block Ack request we activated a timer until
399 * add Block Ack response will arrive from the recipient.
400 * If this timer expires sta_addba_resp_timer_expired will be executed.
402 static void sta_addba_resp_timer_expired(unsigned long data)
404 /* not an elegant detour, but there is no choice as the timer passes
405 * only one argument, and both sta_info and TID are needed, so init
406 * flow in sta_info_create gives the TID as data, while the timer_to_id
407 * array gives the sta through container_of */
408 u16 tid = *(u8 *)data;
409 struct sta_info *temp_sta = container_of((void *)data,
410 struct sta_info, timer_to_tid[tid]);
412 struct ieee80211_local *local = temp_sta->local;
413 struct ieee80211_hw *hw = &local->hw;
414 struct sta_info *sta;
415 u8 *state;
417 rcu_read_lock();
419 sta = sta_info_get(local, temp_sta->sta.addr);
420 if (!sta) {
421 rcu_read_unlock();
422 return;
425 state = &sta->ampdu_mlme.tid_state_tx[tid];
426 /* check if the TID waits for addBA response */
427 spin_lock_bh(&sta->lock);
428 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
429 spin_unlock_bh(&sta->lock);
430 *state = HT_AGG_STATE_IDLE;
431 #ifdef CONFIG_MAC80211_HT_DEBUG
432 printk(KERN_DEBUG "timer expired on tid %d but we are not "
433 "expecting addBA response there", tid);
434 #endif
435 goto timer_expired_exit;
438 #ifdef CONFIG_MAC80211_HT_DEBUG
439 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
440 #endif
442 /* go through the state check in stop_BA_session */
443 *state = HT_AGG_STATE_OPERATIONAL;
444 spin_unlock_bh(&sta->lock);
445 ieee80211_stop_tx_ba_session(hw, temp_sta->sta.addr, tid,
446 WLAN_BACK_INITIATOR);
448 timer_expired_exit:
449 rcu_read_unlock();
452 void ieee80211_sta_tear_down_BA_sessions(struct ieee80211_sub_if_data *sdata, u8 *addr)
454 struct ieee80211_local *local = sdata->local;
455 int i;
457 for (i = 0; i < STA_TID_NUM; i++) {
458 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
459 WLAN_BACK_INITIATOR);
460 ieee80211_sta_stop_rx_ba_session(sdata, addr, i,
461 WLAN_BACK_RECIPIENT,
462 WLAN_REASON_QSTA_LEAVE_QBSS);
466 int ieee80211_start_tx_ba_session(struct ieee80211_hw *hw, u8 *ra, u16 tid)
468 struct ieee80211_local *local = hw_to_local(hw);
469 struct sta_info *sta;
470 struct ieee80211_sub_if_data *sdata;
471 u16 start_seq_num;
472 u8 *state;
473 int ret = 0;
475 if ((tid >= STA_TID_NUM) || !(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
476 return -EINVAL;
478 #ifdef CONFIG_MAC80211_HT_DEBUG
479 printk(KERN_DEBUG "Open BA session requested for %pM tid %u\n",
480 ra, tid);
481 #endif /* CONFIG_MAC80211_HT_DEBUG */
483 rcu_read_lock();
485 sta = sta_info_get(local, ra);
486 if (!sta) {
487 #ifdef CONFIG_MAC80211_HT_DEBUG
488 printk(KERN_DEBUG "Could not find the station\n");
489 #endif
490 ret = -ENOENT;
491 goto exit;
494 spin_lock_bh(&sta->lock);
496 /* we have tried too many times, receiver does not want A-MPDU */
497 if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_MAX_RETRIES) {
498 ret = -EBUSY;
499 goto err_unlock_sta;
502 state = &sta->ampdu_mlme.tid_state_tx[tid];
503 /* check if the TID is not in aggregation flow already */
504 if (*state != HT_AGG_STATE_IDLE) {
505 #ifdef CONFIG_MAC80211_HT_DEBUG
506 printk(KERN_DEBUG "BA request denied - session is not "
507 "idle on tid %u\n", tid);
508 #endif /* CONFIG_MAC80211_HT_DEBUG */
509 ret = -EAGAIN;
510 goto err_unlock_sta;
513 /* prepare A-MPDU MLME for Tx aggregation */
514 sta->ampdu_mlme.tid_tx[tid] =
515 kmalloc(sizeof(struct tid_ampdu_tx), GFP_ATOMIC);
516 if (!sta->ampdu_mlme.tid_tx[tid]) {
517 #ifdef CONFIG_MAC80211_HT_DEBUG
518 if (net_ratelimit())
519 printk(KERN_ERR "allocate tx mlme to tid %d failed\n",
520 tid);
521 #endif
522 ret = -ENOMEM;
523 goto err_unlock_sta;
525 /* Tx timer */
526 sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.function =
527 sta_addba_resp_timer_expired;
528 sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.data =
529 (unsigned long)&sta->timer_to_tid[tid];
530 init_timer(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
532 if (hw->ampdu_queues) {
533 /* create a new queue for this aggregation */
534 ret = ieee80211_ht_agg_queue_add(local, sta, tid);
536 /* case no queue is available to aggregation
537 * don't switch to aggregation */
538 if (ret) {
539 #ifdef CONFIG_MAC80211_HT_DEBUG
540 printk(KERN_DEBUG "BA request denied - "
541 "queue unavailable for tid %d\n", tid);
542 #endif /* CONFIG_MAC80211_HT_DEBUG */
543 goto err_unlock_queue;
546 sdata = sta->sdata;
548 /* Ok, the Addba frame hasn't been sent yet, but if the driver calls the
549 * call back right away, it must see that the flow has begun */
550 *state |= HT_ADDBA_REQUESTED_MSK;
552 /* This is slightly racy because the queue isn't stopped */
553 start_seq_num = sta->tid_seq[tid];
555 if (local->ops->ampdu_action)
556 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_TX_START,
557 &sta->sta, tid, &start_seq_num);
559 if (ret) {
560 /* No need to requeue the packets in the agg queue, since we
561 * held the tx lock: no packet could be enqueued to the newly
562 * allocated queue */
563 if (hw->ampdu_queues)
564 ieee80211_ht_agg_queue_remove(local, sta, tid, 0);
565 #ifdef CONFIG_MAC80211_HT_DEBUG
566 printk(KERN_DEBUG "BA request denied - HW unavailable for"
567 " tid %d\n", tid);
568 #endif /* CONFIG_MAC80211_HT_DEBUG */
569 *state = HT_AGG_STATE_IDLE;
570 goto err_unlock_queue;
573 /* Will put all the packets in the new SW queue */
574 if (hw->ampdu_queues)
575 ieee80211_requeue(local, ieee802_1d_to_ac[tid]);
576 spin_unlock_bh(&sta->lock);
578 /* send an addBA request */
579 sta->ampdu_mlme.dialog_token_allocator++;
580 sta->ampdu_mlme.tid_tx[tid]->dialog_token =
581 sta->ampdu_mlme.dialog_token_allocator;
582 sta->ampdu_mlme.tid_tx[tid]->ssn = start_seq_num;
585 ieee80211_send_addba_request(sta->sdata, ra, tid,
586 sta->ampdu_mlme.tid_tx[tid]->dialog_token,
587 sta->ampdu_mlme.tid_tx[tid]->ssn,
588 0x40, 5000);
589 /* activate the timer for the recipient's addBA response */
590 sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.expires =
591 jiffies + ADDBA_RESP_INTERVAL;
592 add_timer(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
593 #ifdef CONFIG_MAC80211_HT_DEBUG
594 printk(KERN_DEBUG "activated addBA response timer on tid %d\n", tid);
595 #endif
596 goto exit;
598 err_unlock_queue:
599 kfree(sta->ampdu_mlme.tid_tx[tid]);
600 sta->ampdu_mlme.tid_tx[tid] = NULL;
601 ret = -EBUSY;
602 err_unlock_sta:
603 spin_unlock_bh(&sta->lock);
604 exit:
605 rcu_read_unlock();
606 return ret;
608 EXPORT_SYMBOL(ieee80211_start_tx_ba_session);
610 int ieee80211_stop_tx_ba_session(struct ieee80211_hw *hw,
611 u8 *ra, u16 tid,
612 enum ieee80211_back_parties initiator)
614 struct ieee80211_local *local = hw_to_local(hw);
615 struct sta_info *sta;
616 u8 *state;
617 int ret = 0;
619 if (tid >= STA_TID_NUM)
620 return -EINVAL;
622 rcu_read_lock();
623 sta = sta_info_get(local, ra);
624 if (!sta) {
625 rcu_read_unlock();
626 return -ENOENT;
629 /* check if the TID is in aggregation */
630 state = &sta->ampdu_mlme.tid_state_tx[tid];
631 spin_lock_bh(&sta->lock);
633 if (*state != HT_AGG_STATE_OPERATIONAL) {
634 ret = -ENOENT;
635 goto stop_BA_exit;
638 #ifdef CONFIG_MAC80211_HT_DEBUG
639 printk(KERN_DEBUG "Tx BA session stop requested for %pM tid %u\n",
640 ra, tid);
641 #endif /* CONFIG_MAC80211_HT_DEBUG */
643 if (hw->ampdu_queues)
644 ieee80211_stop_queue(hw, sta->tid_to_tx_q[tid]);
646 *state = HT_AGG_STATE_REQ_STOP_BA_MSK |
647 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
649 if (local->ops->ampdu_action)
650 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_TX_STOP,
651 &sta->sta, tid, NULL);
653 /* case HW denied going back to legacy */
654 if (ret) {
655 WARN_ON(ret != -EBUSY);
656 *state = HT_AGG_STATE_OPERATIONAL;
657 if (hw->ampdu_queues)
658 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
659 goto stop_BA_exit;
662 stop_BA_exit:
663 spin_unlock_bh(&sta->lock);
664 rcu_read_unlock();
665 return ret;
667 EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);
669 void ieee80211_start_tx_ba_cb(struct ieee80211_hw *hw, u8 *ra, u16 tid)
671 struct ieee80211_local *local = hw_to_local(hw);
672 struct sta_info *sta;
673 u8 *state;
675 if (tid >= STA_TID_NUM) {
676 #ifdef CONFIG_MAC80211_HT_DEBUG
677 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
678 tid, STA_TID_NUM);
679 #endif
680 return;
683 rcu_read_lock();
684 sta = sta_info_get(local, ra);
685 if (!sta) {
686 rcu_read_unlock();
687 #ifdef CONFIG_MAC80211_HT_DEBUG
688 printk(KERN_DEBUG "Could not find station: %pM\n", ra);
689 #endif
690 return;
693 state = &sta->ampdu_mlme.tid_state_tx[tid];
694 spin_lock_bh(&sta->lock);
696 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
697 #ifdef CONFIG_MAC80211_HT_DEBUG
698 printk(KERN_DEBUG "addBA was not requested yet, state is %d\n",
699 *state);
700 #endif
701 spin_unlock_bh(&sta->lock);
702 rcu_read_unlock();
703 return;
706 WARN_ON_ONCE(*state & HT_ADDBA_DRV_READY_MSK);
708 *state |= HT_ADDBA_DRV_READY_MSK;
710 if (*state == HT_AGG_STATE_OPERATIONAL) {
711 #ifdef CONFIG_MAC80211_HT_DEBUG
712 printk(KERN_DEBUG "Aggregation is on for tid %d \n", tid);
713 #endif
714 if (hw->ampdu_queues)
715 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
717 spin_unlock_bh(&sta->lock);
718 rcu_read_unlock();
720 EXPORT_SYMBOL(ieee80211_start_tx_ba_cb);
722 void ieee80211_stop_tx_ba_cb(struct ieee80211_hw *hw, u8 *ra, u8 tid)
724 struct ieee80211_local *local = hw_to_local(hw);
725 struct sta_info *sta;
726 u8 *state;
727 int agg_queue;
729 if (tid >= STA_TID_NUM) {
730 #ifdef CONFIG_MAC80211_HT_DEBUG
731 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
732 tid, STA_TID_NUM);
733 #endif
734 return;
737 #ifdef CONFIG_MAC80211_HT_DEBUG
738 printk(KERN_DEBUG "Stopping Tx BA session for %pM tid %d\n",
739 ra, tid);
740 #endif /* CONFIG_MAC80211_HT_DEBUG */
742 rcu_read_lock();
743 sta = sta_info_get(local, ra);
744 if (!sta) {
745 #ifdef CONFIG_MAC80211_HT_DEBUG
746 printk(KERN_DEBUG "Could not find station: %pM\n", ra);
747 #endif
748 rcu_read_unlock();
749 return;
751 state = &sta->ampdu_mlme.tid_state_tx[tid];
753 /* NOTE: no need to use sta->lock in this state check, as
754 * ieee80211_stop_tx_ba_session will let only one stop call to
755 * pass through per sta/tid
757 if ((*state & HT_AGG_STATE_REQ_STOP_BA_MSK) == 0) {
758 #ifdef CONFIG_MAC80211_HT_DEBUG
759 printk(KERN_DEBUG "unexpected callback to A-MPDU stop\n");
760 #endif
761 rcu_read_unlock();
762 return;
765 if (*state & HT_AGG_STATE_INITIATOR_MSK)
766 ieee80211_send_delba(sta->sdata, ra, tid,
767 WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);
769 if (hw->ampdu_queues) {
770 agg_queue = sta->tid_to_tx_q[tid];
771 ieee80211_ht_agg_queue_remove(local, sta, tid, 1);
773 /* We just requeued the all the frames that were in the
774 * removed queue, and since we might miss a softirq we do
775 * netif_schedule_queue. ieee80211_wake_queue is not used
776 * here as this queue is not necessarily stopped
778 netif_schedule_queue(netdev_get_tx_queue(local->mdev,
779 agg_queue));
781 spin_lock_bh(&sta->lock);
782 *state = HT_AGG_STATE_IDLE;
783 sta->ampdu_mlme.addba_req_num[tid] = 0;
784 kfree(sta->ampdu_mlme.tid_tx[tid]);
785 sta->ampdu_mlme.tid_tx[tid] = NULL;
786 spin_unlock_bh(&sta->lock);
788 rcu_read_unlock();
790 EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb);
792 void ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_hw *hw,
793 const u8 *ra, u16 tid)
795 struct ieee80211_local *local = hw_to_local(hw);
796 struct ieee80211_ra_tid *ra_tid;
797 struct sk_buff *skb = dev_alloc_skb(0);
799 if (unlikely(!skb)) {
800 #ifdef CONFIG_MAC80211_HT_DEBUG
801 if (net_ratelimit())
802 printk(KERN_WARNING "%s: Not enough memory, "
803 "dropping start BA session", skb->dev->name);
804 #endif
805 return;
807 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
808 memcpy(&ra_tid->ra, ra, ETH_ALEN);
809 ra_tid->tid = tid;
811 skb->pkt_type = IEEE80211_ADDBA_MSG;
812 skb_queue_tail(&local->skb_queue, skb);
813 tasklet_schedule(&local->tasklet);
815 EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);
817 void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_hw *hw,
818 const u8 *ra, u16 tid)
820 struct ieee80211_local *local = hw_to_local(hw);
821 struct ieee80211_ra_tid *ra_tid;
822 struct sk_buff *skb = dev_alloc_skb(0);
824 if (unlikely(!skb)) {
825 #ifdef CONFIG_MAC80211_HT_DEBUG
826 if (net_ratelimit())
827 printk(KERN_WARNING "%s: Not enough memory, "
828 "dropping stop BA session", skb->dev->name);
829 #endif
830 return;
832 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
833 memcpy(&ra_tid->ra, ra, ETH_ALEN);
834 ra_tid->tid = tid;
836 skb->pkt_type = IEEE80211_DELBA_MSG;
837 skb_queue_tail(&local->skb_queue, skb);
838 tasklet_schedule(&local->tasklet);
840 EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);
843 * After accepting the AddBA Request we activated a timer,
844 * resetting it after each frame that arrives from the originator.
845 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
847 static void sta_rx_agg_session_timer_expired(unsigned long data)
849 /* not an elegant detour, but there is no choice as the timer passes
850 * only one argument, and various sta_info are needed here, so init
851 * flow in sta_info_create gives the TID as data, while the timer_to_id
852 * array gives the sta through container_of */
853 u8 *ptid = (u8 *)data;
854 u8 *timer_to_id = ptid - *ptid;
855 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
856 timer_to_tid[0]);
858 #ifdef CONFIG_MAC80211_HT_DEBUG
859 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
860 #endif
861 ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->sta.addr,
862 (u16)*ptid, WLAN_BACK_TIMER,
863 WLAN_REASON_QSTA_TIMEOUT);
866 void ieee80211_process_addba_request(struct ieee80211_local *local,
867 struct sta_info *sta,
868 struct ieee80211_mgmt *mgmt,
869 size_t len)
871 struct ieee80211_hw *hw = &local->hw;
872 struct ieee80211_conf *conf = &hw->conf;
873 struct tid_ampdu_rx *tid_agg_rx;
874 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
875 u8 dialog_token;
876 int ret = -EOPNOTSUPP;
878 /* extract session parameters from addba request frame */
879 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
880 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
881 start_seq_num =
882 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
884 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
885 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
886 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
887 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
889 status = WLAN_STATUS_REQUEST_DECLINED;
891 /* sanity check for incoming parameters:
892 * check if configuration can support the BA policy
893 * and if buffer size does not exceeds max value */
894 /* XXX: check own ht delayed BA capability?? */
895 if (((ba_policy != 1)
896 && (!(sta->sta.ht_cap.cap & IEEE80211_HT_CAP_DELAY_BA)))
897 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
898 status = WLAN_STATUS_INVALID_QOS_PARAM;
899 #ifdef CONFIG_MAC80211_HT_DEBUG
900 if (net_ratelimit())
901 printk(KERN_DEBUG "AddBA Req with bad params from "
902 "%pM on tid %u. policy %d, buffer size %d\n",
903 mgmt->sa, tid, ba_policy,
904 buf_size);
905 #endif /* CONFIG_MAC80211_HT_DEBUG */
906 goto end_no_lock;
908 /* determine default buffer size */
909 if (buf_size == 0) {
910 struct ieee80211_supported_band *sband;
912 sband = local->hw.wiphy->bands[conf->channel->band];
913 buf_size = IEEE80211_MIN_AMPDU_BUF;
914 buf_size = buf_size << sband->ht_cap.ampdu_factor;
918 /* examine state machine */
919 spin_lock_bh(&sta->lock);
921 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
922 #ifdef CONFIG_MAC80211_HT_DEBUG
923 if (net_ratelimit())
924 printk(KERN_DEBUG "unexpected AddBA Req from "
925 "%pM on tid %u\n",
926 mgmt->sa, tid);
927 #endif /* CONFIG_MAC80211_HT_DEBUG */
928 goto end;
931 /* prepare A-MPDU MLME for Rx aggregation */
932 sta->ampdu_mlme.tid_rx[tid] =
933 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
934 if (!sta->ampdu_mlme.tid_rx[tid]) {
935 #ifdef CONFIG_MAC80211_HT_DEBUG
936 if (net_ratelimit())
937 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
938 tid);
939 #endif
940 goto end;
942 /* rx timer */
943 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
944 sta_rx_agg_session_timer_expired;
945 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
946 (unsigned long)&sta->timer_to_tid[tid];
947 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
949 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
951 /* prepare reordering buffer */
952 tid_agg_rx->reorder_buf =
953 kcalloc(buf_size, sizeof(struct sk_buff *), GFP_ATOMIC);
954 if (!tid_agg_rx->reorder_buf) {
955 #ifdef CONFIG_MAC80211_HT_DEBUG
956 if (net_ratelimit())
957 printk(KERN_ERR "can not allocate reordering buffer "
958 "to tid %d\n", tid);
959 #endif
960 kfree(sta->ampdu_mlme.tid_rx[tid]);
961 goto end;
964 if (local->ops->ampdu_action)
965 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
966 &sta->sta, tid, &start_seq_num);
967 #ifdef CONFIG_MAC80211_HT_DEBUG
968 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
969 #endif /* CONFIG_MAC80211_HT_DEBUG */
971 if (ret) {
972 kfree(tid_agg_rx->reorder_buf);
973 kfree(tid_agg_rx);
974 sta->ampdu_mlme.tid_rx[tid] = NULL;
975 goto end;
978 /* change state and send addba resp */
979 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
980 tid_agg_rx->dialog_token = dialog_token;
981 tid_agg_rx->ssn = start_seq_num;
982 tid_agg_rx->head_seq_num = start_seq_num;
983 tid_agg_rx->buf_size = buf_size;
984 tid_agg_rx->timeout = timeout;
985 tid_agg_rx->stored_mpdu_num = 0;
986 status = WLAN_STATUS_SUCCESS;
987 end:
988 spin_unlock_bh(&sta->lock);
990 end_no_lock:
991 ieee80211_send_addba_resp(sta->sdata, sta->sta.addr, tid,
992 dialog_token, status, 1, buf_size, timeout);
995 void ieee80211_process_addba_resp(struct ieee80211_local *local,
996 struct sta_info *sta,
997 struct ieee80211_mgmt *mgmt,
998 size_t len)
1000 struct ieee80211_hw *hw = &local->hw;
1001 u16 capab;
1002 u16 tid, start_seq_num;
1003 u8 *state;
1005 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1006 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1008 state = &sta->ampdu_mlme.tid_state_tx[tid];
1010 spin_lock_bh(&sta->lock);
1012 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1013 spin_unlock_bh(&sta->lock);
1014 return;
1017 if (mgmt->u.action.u.addba_resp.dialog_token !=
1018 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
1019 spin_unlock_bh(&sta->lock);
1020 #ifdef CONFIG_MAC80211_HT_DEBUG
1021 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1022 #endif /* CONFIG_MAC80211_HT_DEBUG */
1023 return;
1026 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
1027 #ifdef CONFIG_MAC80211_HT_DEBUG
1028 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1029 #endif /* CONFIG_MAC80211_HT_DEBUG */
1030 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1031 == WLAN_STATUS_SUCCESS) {
1032 *state |= HT_ADDBA_RECEIVED_MSK;
1033 sta->ampdu_mlme.addba_req_num[tid] = 0;
1035 if (*state == HT_AGG_STATE_OPERATIONAL &&
1036 local->hw.ampdu_queues)
1037 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1039 if (local->ops->ampdu_action) {
1040 (void)local->ops->ampdu_action(hw,
1041 IEEE80211_AMPDU_TX_RESUME,
1042 &sta->sta, tid, &start_seq_num);
1044 #ifdef CONFIG_MAC80211_HT_DEBUG
1045 printk(KERN_DEBUG "Resuming TX aggregation for tid %d\n", tid);
1046 #endif /* CONFIG_MAC80211_HT_DEBUG */
1047 spin_unlock_bh(&sta->lock);
1048 } else {
1049 sta->ampdu_mlme.addba_req_num[tid]++;
1050 /* this will allow the state check in stop_BA_session */
1051 *state = HT_AGG_STATE_OPERATIONAL;
1052 spin_unlock_bh(&sta->lock);
1053 ieee80211_stop_tx_ba_session(hw, sta->sta.addr, tid,
1054 WLAN_BACK_INITIATOR);
1058 void ieee80211_process_delba(struct ieee80211_sub_if_data *sdata,
1059 struct sta_info *sta,
1060 struct ieee80211_mgmt *mgmt, size_t len)
1062 struct ieee80211_local *local = sdata->local;
1063 u16 tid, params;
1064 u16 initiator;
1066 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1067 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1068 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1070 #ifdef CONFIG_MAC80211_HT_DEBUG
1071 if (net_ratelimit())
1072 printk(KERN_DEBUG "delba from %pM (%s) tid %d reason code %d\n",
1073 mgmt->sa, initiator ? "initiator" : "recipient", tid,
1074 mgmt->u.action.u.delba.reason_code);
1075 #endif /* CONFIG_MAC80211_HT_DEBUG */
1077 if (initiator == WLAN_BACK_INITIATOR)
1078 ieee80211_sta_stop_rx_ba_session(sdata, sta->sta.addr, tid,
1079 WLAN_BACK_INITIATOR, 0);
1080 else { /* WLAN_BACK_RECIPIENT */
1081 spin_lock_bh(&sta->lock);
1082 sta->ampdu_mlme.tid_state_tx[tid] =
1083 HT_AGG_STATE_OPERATIONAL;
1084 spin_unlock_bh(&sta->lock);
1085 ieee80211_stop_tx_ba_session(&local->hw, sta->sta.addr, tid,
1086 WLAN_BACK_RECIPIENT);