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"
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;
32 memset(ht_cap, 0, sizeof(*ht_cap));
37 ht_cap->ht_supported = true;
39 ht_cap->cap = ht_cap->cap & 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))
56 /* Counting from 0, therefore +1 */
57 if (tx_mcs_set_cap & IEEE80211_HT_MCS_TX_RX_DIFF)
59 ((tx_mcs_set_cap & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
60 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
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,
96 struct ieee80211_local *local = sdata->local;
97 struct ieee80211_supported_band *sband;
98 struct ieee80211_bss_ht_conf ht;
100 bool enable_ht = true, ht_changed;
102 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
104 memset(&ht, 0, sizeof(ht));
106 /* HT is not supported */
107 if (!sband->ht_cap.ht_supported)
110 /* check that channel matches the right operating channel */
111 if (local->hw.conf.channel->center_freq !=
112 ieee80211_channel_to_frequency(hti->control_chan))
116 * XXX: This is totally incorrect when there are multiple virtual
117 * interfaces, needs to be fixed later.
119 ht_changed = local->hw.conf.ht.enabled != enable_ht;
120 local->hw.conf.ht.enabled = enable_ht;
122 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_HT);
127 ht.secondary_channel_offset =
128 hti->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET;
130 !(ap_ht_cap_flags & IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
131 (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
132 (hti->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY);
133 ht.operation_mode = le16_to_cpu(hti->operation_mode);
135 /* if bss configuration changed store the new one */
136 if (memcmp(&sdata->vif.bss_conf.ht, &ht, sizeof(ht))) {
137 changed |= BSS_CHANGED_HT;
138 sdata->vif.bss_conf.ht = ht;
144 static void ieee80211_send_addba_request(struct ieee80211_sub_if_data *sdata,
145 const u8 *da, u16 tid,
146 u8 dialog_token, u16 start_seq_num,
147 u16 agg_size, u16 timeout)
149 struct ieee80211_local *local = sdata->local;
150 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
152 struct ieee80211_mgmt *mgmt;
155 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
158 printk(KERN_ERR "%s: failed to allocate buffer "
159 "for addba request frame\n", sdata->dev->name);
162 skb_reserve(skb, local->hw.extra_tx_headroom);
163 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
165 memcpy(mgmt->da, da, ETH_ALEN);
166 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
167 if (sdata->vif.type == NL80211_IFTYPE_AP)
168 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
170 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
172 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
173 IEEE80211_STYPE_ACTION);
175 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
177 mgmt->u.action.category = WLAN_CATEGORY_BACK;
178 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
180 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
181 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
182 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
183 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
185 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
187 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
188 mgmt->u.action.u.addba_req.start_seq_num =
189 cpu_to_le16(start_seq_num << 4);
191 ieee80211_tx_skb(sdata, skb, 0);
194 static void ieee80211_send_addba_resp(struct ieee80211_sub_if_data *sdata, u8 *da, u16 tid,
195 u8 dialog_token, u16 status, u16 policy,
196 u16 buf_size, u16 timeout)
198 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
199 struct ieee80211_local *local = sdata->local;
201 struct ieee80211_mgmt *mgmt;
204 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
207 printk(KERN_DEBUG "%s: failed to allocate buffer "
208 "for addba resp frame\n", sdata->dev->name);
212 skb_reserve(skb, local->hw.extra_tx_headroom);
213 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
215 memcpy(mgmt->da, da, ETH_ALEN);
216 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
217 if (sdata->vif.type == NL80211_IFTYPE_AP)
218 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
220 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
221 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
222 IEEE80211_STYPE_ACTION);
224 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
225 mgmt->u.action.category = WLAN_CATEGORY_BACK;
226 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
227 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
229 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
230 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
231 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
233 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
234 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
235 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
237 ieee80211_tx_skb(sdata, skb, 0);
240 static void ieee80211_send_delba(struct ieee80211_sub_if_data *sdata,
241 const u8 *da, u16 tid,
242 u16 initiator, u16 reason_code)
244 struct ieee80211_local *local = sdata->local;
245 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
247 struct ieee80211_mgmt *mgmt;
250 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
253 printk(KERN_ERR "%s: failed to allocate buffer "
254 "for delba frame\n", sdata->dev->name);
258 skb_reserve(skb, local->hw.extra_tx_headroom);
259 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
261 memcpy(mgmt->da, da, ETH_ALEN);
262 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
263 if (sdata->vif.type == NL80211_IFTYPE_AP)
264 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
266 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
267 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
268 IEEE80211_STYPE_ACTION);
270 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
272 mgmt->u.action.category = WLAN_CATEGORY_BACK;
273 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
274 params = (u16)(initiator << 11); /* bit 11 initiator */
275 params |= (u16)(tid << 12); /* bit 15:12 TID number */
277 mgmt->u.action.u.delba.params = cpu_to_le16(params);
278 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
280 ieee80211_tx_skb(sdata, skb, 0);
283 void ieee80211_send_bar(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid, u16 ssn)
285 struct ieee80211_local *local = sdata->local;
287 struct ieee80211_bar *bar;
290 skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
292 printk(KERN_ERR "%s: failed to allocate buffer for "
293 "bar frame\n", sdata->dev->name);
296 skb_reserve(skb, local->hw.extra_tx_headroom);
297 bar = (struct ieee80211_bar *)skb_put(skb, sizeof(*bar));
298 memset(bar, 0, sizeof(*bar));
299 bar->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
300 IEEE80211_STYPE_BACK_REQ);
301 memcpy(bar->ra, ra, ETH_ALEN);
302 memcpy(bar->ta, sdata->dev->dev_addr, ETH_ALEN);
303 bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
304 bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
305 bar_control |= (u16)(tid << 12);
306 bar->control = cpu_to_le16(bar_control);
307 bar->start_seq_num = cpu_to_le16(ssn);
309 ieee80211_tx_skb(sdata, skb, 0);
312 void ieee80211_sta_stop_rx_ba_session(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid,
313 u16 initiator, u16 reason)
315 struct ieee80211_local *local = sdata->local;
316 struct ieee80211_hw *hw = &local->hw;
317 struct sta_info *sta;
322 sta = sta_info_get(local, ra);
328 /* check if TID is in operational state */
329 spin_lock_bh(&sta->lock);
330 if (sta->ampdu_mlme.tid_state_rx[tid]
331 != HT_AGG_STATE_OPERATIONAL) {
332 spin_unlock_bh(&sta->lock);
336 sta->ampdu_mlme.tid_state_rx[tid] =
337 HT_AGG_STATE_REQ_STOP_BA_MSK |
338 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
339 spin_unlock_bh(&sta->lock);
341 /* stop HW Rx aggregation. ampdu_action existence
342 * already verified in session init so we add the BUG_ON */
343 BUG_ON(!local->ops->ampdu_action);
345 #ifdef CONFIG_MAC80211_HT_DEBUG
346 printk(KERN_DEBUG "Rx BA session stop requested for %pM tid %u\n",
348 #endif /* CONFIG_MAC80211_HT_DEBUG */
350 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
351 &sta->sta, tid, NULL);
353 printk(KERN_DEBUG "HW problem - can not stop rx "
354 "aggregation for tid %d\n", tid);
356 /* shutdown timer has not expired */
357 if (initiator != WLAN_BACK_TIMER)
358 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
360 /* check if this is a self generated aggregation halt */
361 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
362 ieee80211_send_delba(sdata, ra, tid, 0, reason);
364 /* free the reordering buffer */
365 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
366 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
367 /* release the reordered frames */
368 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
369 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
370 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
374 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
375 kfree(sta->ampdu_mlme.tid_rx[tid]);
376 sta->ampdu_mlme.tid_rx[tid] = NULL;
377 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
384 * After sending add Block Ack request we activated a timer until
385 * add Block Ack response will arrive from the recipient.
386 * If this timer expires sta_addba_resp_timer_expired will be executed.
388 static void sta_addba_resp_timer_expired(unsigned long data)
390 /* not an elegant detour, but there is no choice as the timer passes
391 * only one argument, and both sta_info and TID are needed, so init
392 * flow in sta_info_create gives the TID as data, while the timer_to_id
393 * array gives the sta through container_of */
394 u16 tid = *(u8 *)data;
395 struct sta_info *temp_sta = container_of((void *)data,
396 struct sta_info, timer_to_tid[tid]);
398 struct ieee80211_local *local = temp_sta->local;
399 struct ieee80211_hw *hw = &local->hw;
400 struct sta_info *sta;
405 sta = sta_info_get(local, temp_sta->sta.addr);
411 state = &sta->ampdu_mlme.tid_state_tx[tid];
412 /* check if the TID waits for addBA response */
413 spin_lock_bh(&sta->lock);
414 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
415 spin_unlock_bh(&sta->lock);
416 *state = HT_AGG_STATE_IDLE;
417 #ifdef CONFIG_MAC80211_HT_DEBUG
418 printk(KERN_DEBUG "timer expired on tid %d but we are not "
419 "expecting addBA response there", tid);
421 goto timer_expired_exit;
424 #ifdef CONFIG_MAC80211_HT_DEBUG
425 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
428 /* go through the state check in stop_BA_session */
429 *state = HT_AGG_STATE_OPERATIONAL;
430 spin_unlock_bh(&sta->lock);
431 ieee80211_stop_tx_ba_session(hw, temp_sta->sta.addr, tid,
432 WLAN_BACK_INITIATOR);
438 void ieee80211_sta_tear_down_BA_sessions(struct ieee80211_sub_if_data *sdata, u8 *addr)
440 struct ieee80211_local *local = sdata->local;
443 for (i = 0; i < STA_TID_NUM; i++) {
444 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
445 WLAN_BACK_INITIATOR);
446 ieee80211_sta_stop_rx_ba_session(sdata, addr, i,
448 WLAN_REASON_QSTA_LEAVE_QBSS);
452 int ieee80211_start_tx_ba_session(struct ieee80211_hw *hw, u8 *ra, u16 tid)
454 struct ieee80211_local *local = hw_to_local(hw);
455 struct sta_info *sta;
456 struct ieee80211_sub_if_data *sdata;
461 if (tid >= STA_TID_NUM)
464 #ifdef CONFIG_MAC80211_HT_DEBUG
465 printk(KERN_DEBUG "Open BA session requested for %pM tid %u\n",
467 #endif /* CONFIG_MAC80211_HT_DEBUG */
471 sta = sta_info_get(local, ra);
473 #ifdef CONFIG_MAC80211_HT_DEBUG
474 printk(KERN_DEBUG "Could not find the station\n");
480 spin_lock_bh(&sta->lock);
482 /* we have tried too many times, receiver does not want A-MPDU */
483 if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_MAX_RETRIES) {
488 state = &sta->ampdu_mlme.tid_state_tx[tid];
489 /* check if the TID is not in aggregation flow already */
490 if (*state != HT_AGG_STATE_IDLE) {
491 #ifdef CONFIG_MAC80211_HT_DEBUG
492 printk(KERN_DEBUG "BA request denied - session is not "
493 "idle on tid %u\n", tid);
494 #endif /* CONFIG_MAC80211_HT_DEBUG */
499 /* prepare A-MPDU MLME for Tx aggregation */
500 sta->ampdu_mlme.tid_tx[tid] =
501 kmalloc(sizeof(struct tid_ampdu_tx), GFP_ATOMIC);
502 if (!sta->ampdu_mlme.tid_tx[tid]) {
503 #ifdef CONFIG_MAC80211_HT_DEBUG
505 printk(KERN_ERR "allocate tx mlme to tid %d failed\n",
512 sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.function =
513 sta_addba_resp_timer_expired;
514 sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.data =
515 (unsigned long)&sta->timer_to_tid[tid];
516 init_timer(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
518 /* create a new queue for this aggregation */
519 ret = ieee80211_ht_agg_queue_add(local, sta, tid);
521 /* case no queue is available to aggregation
522 * don't switch to aggregation */
524 #ifdef CONFIG_MAC80211_HT_DEBUG
525 printk(KERN_DEBUG "BA request denied - queue unavailable for"
527 #endif /* CONFIG_MAC80211_HT_DEBUG */
528 goto err_unlock_queue;
532 /* Ok, the Addba frame hasn't been sent yet, but if the driver calls the
533 * call back right away, it must see that the flow has begun */
534 *state |= HT_ADDBA_REQUESTED_MSK;
536 /* This is slightly racy because the queue isn't stopped */
537 start_seq_num = sta->tid_seq[tid];
539 if (local->ops->ampdu_action)
540 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_TX_START,
541 &sta->sta, tid, &start_seq_num);
544 /* No need to requeue the packets in the agg queue, since we
545 * held the tx lock: no packet could be enqueued to the newly
547 ieee80211_ht_agg_queue_remove(local, sta, tid, 0);
548 #ifdef CONFIG_MAC80211_HT_DEBUG
549 printk(KERN_DEBUG "BA request denied - HW unavailable for"
551 #endif /* CONFIG_MAC80211_HT_DEBUG */
552 *state = HT_AGG_STATE_IDLE;
553 goto err_unlock_queue;
556 /* Will put all the packets in the new SW queue */
557 ieee80211_requeue(local, ieee802_1d_to_ac[tid]);
558 spin_unlock_bh(&sta->lock);
560 /* send an addBA request */
561 sta->ampdu_mlme.dialog_token_allocator++;
562 sta->ampdu_mlme.tid_tx[tid]->dialog_token =
563 sta->ampdu_mlme.dialog_token_allocator;
564 sta->ampdu_mlme.tid_tx[tid]->ssn = start_seq_num;
567 ieee80211_send_addba_request(sta->sdata, ra, tid,
568 sta->ampdu_mlme.tid_tx[tid]->dialog_token,
569 sta->ampdu_mlme.tid_tx[tid]->ssn,
571 /* activate the timer for the recipient's addBA response */
572 sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.expires =
573 jiffies + ADDBA_RESP_INTERVAL;
574 add_timer(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
575 #ifdef CONFIG_MAC80211_HT_DEBUG
576 printk(KERN_DEBUG "activated addBA response timer on tid %d\n", tid);
581 kfree(sta->ampdu_mlme.tid_tx[tid]);
582 sta->ampdu_mlme.tid_tx[tid] = NULL;
585 spin_unlock_bh(&sta->lock);
590 EXPORT_SYMBOL(ieee80211_start_tx_ba_session);
592 int ieee80211_stop_tx_ba_session(struct ieee80211_hw *hw,
594 enum ieee80211_back_parties initiator)
596 struct ieee80211_local *local = hw_to_local(hw);
597 struct sta_info *sta;
601 if (tid >= STA_TID_NUM)
605 sta = sta_info_get(local, ra);
611 /* check if the TID is in aggregation */
612 state = &sta->ampdu_mlme.tid_state_tx[tid];
613 spin_lock_bh(&sta->lock);
615 if (*state != HT_AGG_STATE_OPERATIONAL) {
620 #ifdef CONFIG_MAC80211_HT_DEBUG
621 printk(KERN_DEBUG "Tx BA session stop requested for %pM tid %u\n",
623 #endif /* CONFIG_MAC80211_HT_DEBUG */
625 ieee80211_stop_queue(hw, sta->tid_to_tx_q[tid]);
627 *state = HT_AGG_STATE_REQ_STOP_BA_MSK |
628 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
630 if (local->ops->ampdu_action)
631 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_TX_STOP,
632 &sta->sta, tid, NULL);
634 /* case HW denied going back to legacy */
636 WARN_ON(ret != -EBUSY);
637 *state = HT_AGG_STATE_OPERATIONAL;
638 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
643 spin_unlock_bh(&sta->lock);
647 EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);
649 void ieee80211_start_tx_ba_cb(struct ieee80211_hw *hw, u8 *ra, u16 tid)
651 struct ieee80211_local *local = hw_to_local(hw);
652 struct sta_info *sta;
655 if (tid >= STA_TID_NUM) {
656 #ifdef CONFIG_MAC80211_HT_DEBUG
657 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
664 sta = sta_info_get(local, ra);
667 #ifdef CONFIG_MAC80211_HT_DEBUG
668 printk(KERN_DEBUG "Could not find station: %pM\n", ra);
673 state = &sta->ampdu_mlme.tid_state_tx[tid];
674 spin_lock_bh(&sta->lock);
676 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
677 #ifdef CONFIG_MAC80211_HT_DEBUG
678 printk(KERN_DEBUG "addBA was not requested yet, state is %d\n",
681 spin_unlock_bh(&sta->lock);
686 WARN_ON_ONCE(*state & HT_ADDBA_DRV_READY_MSK);
688 *state |= HT_ADDBA_DRV_READY_MSK;
690 if (*state == HT_AGG_STATE_OPERATIONAL) {
691 #ifdef CONFIG_MAC80211_HT_DEBUG
692 printk(KERN_DEBUG "Aggregation is on for tid %d \n", tid);
694 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
696 spin_unlock_bh(&sta->lock);
699 EXPORT_SYMBOL(ieee80211_start_tx_ba_cb);
701 void ieee80211_stop_tx_ba_cb(struct ieee80211_hw *hw, u8 *ra, u8 tid)
703 struct ieee80211_local *local = hw_to_local(hw);
704 struct sta_info *sta;
708 if (tid >= STA_TID_NUM) {
709 #ifdef CONFIG_MAC80211_HT_DEBUG
710 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
716 #ifdef CONFIG_MAC80211_HT_DEBUG
717 printk(KERN_DEBUG "Stopping Tx BA session for %pM tid %d\n",
719 #endif /* CONFIG_MAC80211_HT_DEBUG */
722 sta = sta_info_get(local, ra);
724 #ifdef CONFIG_MAC80211_HT_DEBUG
725 printk(KERN_DEBUG "Could not find station: %pM\n", ra);
730 state = &sta->ampdu_mlme.tid_state_tx[tid];
732 /* NOTE: no need to use sta->lock in this state check, as
733 * ieee80211_stop_tx_ba_session will let only one stop call to
734 * pass through per sta/tid
736 if ((*state & HT_AGG_STATE_REQ_STOP_BA_MSK) == 0) {
737 #ifdef CONFIG_MAC80211_HT_DEBUG
738 printk(KERN_DEBUG "unexpected callback to A-MPDU stop\n");
744 if (*state & HT_AGG_STATE_INITIATOR_MSK)
745 ieee80211_send_delba(sta->sdata, ra, tid,
746 WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);
748 agg_queue = sta->tid_to_tx_q[tid];
750 ieee80211_ht_agg_queue_remove(local, sta, tid, 1);
752 /* We just requeued the all the frames that were in the
753 * removed queue, and since we might miss a softirq we do
754 * netif_schedule_queue. ieee80211_wake_queue is not used
755 * here as this queue is not necessarily stopped
757 netif_schedule_queue(netdev_get_tx_queue(local->mdev, agg_queue));
758 spin_lock_bh(&sta->lock);
759 *state = HT_AGG_STATE_IDLE;
760 sta->ampdu_mlme.addba_req_num[tid] = 0;
761 kfree(sta->ampdu_mlme.tid_tx[tid]);
762 sta->ampdu_mlme.tid_tx[tid] = NULL;
763 spin_unlock_bh(&sta->lock);
767 EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb);
769 void ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_hw *hw,
770 const u8 *ra, u16 tid)
772 struct ieee80211_local *local = hw_to_local(hw);
773 struct ieee80211_ra_tid *ra_tid;
774 struct sk_buff *skb = dev_alloc_skb(0);
776 if (unlikely(!skb)) {
777 #ifdef CONFIG_MAC80211_HT_DEBUG
779 printk(KERN_WARNING "%s: Not enough memory, "
780 "dropping start BA session", skb->dev->name);
784 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
785 memcpy(&ra_tid->ra, ra, ETH_ALEN);
788 skb->pkt_type = IEEE80211_ADDBA_MSG;
789 skb_queue_tail(&local->skb_queue, skb);
790 tasklet_schedule(&local->tasklet);
792 EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);
794 void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_hw *hw,
795 const u8 *ra, u16 tid)
797 struct ieee80211_local *local = hw_to_local(hw);
798 struct ieee80211_ra_tid *ra_tid;
799 struct sk_buff *skb = dev_alloc_skb(0);
801 if (unlikely(!skb)) {
802 #ifdef CONFIG_MAC80211_HT_DEBUG
804 printk(KERN_WARNING "%s: Not enough memory, "
805 "dropping stop BA session", skb->dev->name);
809 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
810 memcpy(&ra_tid->ra, ra, ETH_ALEN);
813 skb->pkt_type = IEEE80211_DELBA_MSG;
814 skb_queue_tail(&local->skb_queue, skb);
815 tasklet_schedule(&local->tasklet);
817 EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);
820 * After accepting the AddBA Request we activated a timer,
821 * resetting it after each frame that arrives from the originator.
822 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
824 static void sta_rx_agg_session_timer_expired(unsigned long data)
826 /* not an elegant detour, but there is no choice as the timer passes
827 * only one argument, and various sta_info are needed here, so init
828 * flow in sta_info_create gives the TID as data, while the timer_to_id
829 * array gives the sta through container_of */
830 u8 *ptid = (u8 *)data;
831 u8 *timer_to_id = ptid - *ptid;
832 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
835 #ifdef CONFIG_MAC80211_HT_DEBUG
836 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
838 ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->sta.addr,
839 (u16)*ptid, WLAN_BACK_TIMER,
840 WLAN_REASON_QSTA_TIMEOUT);
843 void ieee80211_process_addba_request(struct ieee80211_local *local,
844 struct sta_info *sta,
845 struct ieee80211_mgmt *mgmt,
848 struct ieee80211_hw *hw = &local->hw;
849 struct ieee80211_conf *conf = &hw->conf;
850 struct tid_ampdu_rx *tid_agg_rx;
851 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
853 int ret = -EOPNOTSUPP;
855 /* extract session parameters from addba request frame */
856 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
857 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
859 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
861 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
862 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
863 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
864 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
866 status = WLAN_STATUS_REQUEST_DECLINED;
868 /* sanity check for incoming parameters:
869 * check if configuration can support the BA policy
870 * and if buffer size does not exceeds max value */
871 /* XXX: check own ht delayed BA capability?? */
872 if (((ba_policy != 1)
873 && (!(sta->sta.ht_cap.cap & IEEE80211_HT_CAP_DELAY_BA)))
874 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
875 status = WLAN_STATUS_INVALID_QOS_PARAM;
876 #ifdef CONFIG_MAC80211_HT_DEBUG
878 printk(KERN_DEBUG "AddBA Req with bad params from "
879 "%pM on tid %u. policy %d, buffer size %d\n",
880 mgmt->sa, tid, ba_policy,
882 #endif /* CONFIG_MAC80211_HT_DEBUG */
885 /* determine default buffer size */
887 struct ieee80211_supported_band *sband;
889 sband = local->hw.wiphy->bands[conf->channel->band];
890 buf_size = IEEE80211_MIN_AMPDU_BUF;
891 buf_size = buf_size << sband->ht_cap.ampdu_factor;
895 /* examine state machine */
896 spin_lock_bh(&sta->lock);
898 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
899 #ifdef CONFIG_MAC80211_HT_DEBUG
901 printk(KERN_DEBUG "unexpected AddBA Req from "
904 #endif /* CONFIG_MAC80211_HT_DEBUG */
908 /* prepare A-MPDU MLME for Rx aggregation */
909 sta->ampdu_mlme.tid_rx[tid] =
910 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
911 if (!sta->ampdu_mlme.tid_rx[tid]) {
912 #ifdef CONFIG_MAC80211_HT_DEBUG
914 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
920 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
921 sta_rx_agg_session_timer_expired;
922 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
923 (unsigned long)&sta->timer_to_tid[tid];
924 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
926 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
928 /* prepare reordering buffer */
929 tid_agg_rx->reorder_buf =
930 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
931 if (!tid_agg_rx->reorder_buf) {
932 #ifdef CONFIG_MAC80211_HT_DEBUG
934 printk(KERN_ERR "can not allocate reordering buffer "
937 kfree(sta->ampdu_mlme.tid_rx[tid]);
940 memset(tid_agg_rx->reorder_buf, 0,
941 buf_size * sizeof(struct sk_buff *));
943 if (local->ops->ampdu_action)
944 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
945 &sta->sta, tid, &start_seq_num);
946 #ifdef CONFIG_MAC80211_HT_DEBUG
947 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
948 #endif /* CONFIG_MAC80211_HT_DEBUG */
951 kfree(tid_agg_rx->reorder_buf);
953 sta->ampdu_mlme.tid_rx[tid] = NULL;
957 /* change state and send addba resp */
958 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
959 tid_agg_rx->dialog_token = dialog_token;
960 tid_agg_rx->ssn = start_seq_num;
961 tid_agg_rx->head_seq_num = start_seq_num;
962 tid_agg_rx->buf_size = buf_size;
963 tid_agg_rx->timeout = timeout;
964 tid_agg_rx->stored_mpdu_num = 0;
965 status = WLAN_STATUS_SUCCESS;
967 spin_unlock_bh(&sta->lock);
970 ieee80211_send_addba_resp(sta->sdata, sta->sta.addr, tid,
971 dialog_token, status, 1, buf_size, timeout);
974 void ieee80211_process_addba_resp(struct ieee80211_local *local,
975 struct sta_info *sta,
976 struct ieee80211_mgmt *mgmt,
979 struct ieee80211_hw *hw = &local->hw;
984 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
985 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
987 state = &sta->ampdu_mlme.tid_state_tx[tid];
989 spin_lock_bh(&sta->lock);
991 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
992 spin_unlock_bh(&sta->lock);
996 if (mgmt->u.action.u.addba_resp.dialog_token !=
997 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
998 spin_unlock_bh(&sta->lock);
999 #ifdef CONFIG_MAC80211_HT_DEBUG
1000 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1001 #endif /* CONFIG_MAC80211_HT_DEBUG */
1005 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
1006 #ifdef CONFIG_MAC80211_HT_DEBUG
1007 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1008 #endif /* CONFIG_MAC80211_HT_DEBUG */
1009 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1010 == WLAN_STATUS_SUCCESS) {
1011 *state |= HT_ADDBA_RECEIVED_MSK;
1012 sta->ampdu_mlme.addba_req_num[tid] = 0;
1014 if (*state == HT_AGG_STATE_OPERATIONAL)
1015 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1017 spin_unlock_bh(&sta->lock);
1019 sta->ampdu_mlme.addba_req_num[tid]++;
1020 /* this will allow the state check in stop_BA_session */
1021 *state = HT_AGG_STATE_OPERATIONAL;
1022 spin_unlock_bh(&sta->lock);
1023 ieee80211_stop_tx_ba_session(hw, sta->sta.addr, tid,
1024 WLAN_BACK_INITIATOR);
1028 void ieee80211_process_delba(struct ieee80211_sub_if_data *sdata,
1029 struct sta_info *sta,
1030 struct ieee80211_mgmt *mgmt, size_t len)
1032 struct ieee80211_local *local = sdata->local;
1036 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1037 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1038 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1040 #ifdef CONFIG_MAC80211_HT_DEBUG
1041 if (net_ratelimit())
1042 printk(KERN_DEBUG "delba from %pM (%s) tid %d reason code %d\n",
1043 mgmt->sa, initiator ? "initiator" : "recipient", tid,
1044 mgmt->u.action.u.delba.reason_code);
1045 #endif /* CONFIG_MAC80211_HT_DEBUG */
1047 if (initiator == WLAN_BACK_INITIATOR)
1048 ieee80211_sta_stop_rx_ba_session(sdata, sta->sta.addr, tid,
1049 WLAN_BACK_INITIATOR, 0);
1050 else { /* WLAN_BACK_RECIPIENT */
1051 spin_lock_bh(&sta->lock);
1052 sta->ampdu_mlme.tid_state_tx[tid] =
1053 HT_AGG_STATE_OPERATIONAL;
1054 spin_unlock_bh(&sta->lock);
1055 ieee80211_stop_tx_ba_session(&local->hw, sta->sta.addr, tid,
1056 WLAN_BACK_RECIPIENT);