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 = 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))
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;
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)
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))
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;
126 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
127 channel_type = NL80211_CHAN_HT40MINUS;
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;
139 /* channel_type change automatically detected */
140 ieee80211_hw_config(local, 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;
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;
166 struct ieee80211_mgmt *mgmt;
169 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
172 printk(KERN_ERR "%s: failed to allocate buffer "
173 "for addba request frame\n", sdata->dev->name);
176 skb_reserve(skb, local->hw.extra_tx_headroom);
177 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 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);
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;
215 struct ieee80211_mgmt *mgmt;
218 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
221 printk(KERN_DEBUG "%s: failed to allocate buffer "
222 "for addba resp frame\n", sdata->dev->name);
226 skb_reserve(skb, local->hw.extra_tx_headroom);
227 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 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);
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;
261 struct ieee80211_mgmt *mgmt;
264 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
267 printk(KERN_ERR "%s: failed to allocate buffer "
268 "for delba frame\n", sdata->dev->name);
272 skb_reserve(skb, local->hw.extra_tx_headroom);
273 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 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);
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;
301 struct ieee80211_bar *bar;
304 skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
306 printk(KERN_ERR "%s: failed to allocate buffer for "
307 "bar frame\n", sdata->dev->name);
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;
336 sta = sta_info_get(local, ra);
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);
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",
362 #endif /* CONFIG_MAC80211_HT_DEBUG */
364 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
365 &sta->sta, tid, NULL);
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;
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;
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;
419 sta = sta_info_get(local, temp_sta->sta.addr);
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);
435 goto timer_expired_exit;
438 #ifdef CONFIG_MAC80211_HT_DEBUG
439 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
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);
452 void ieee80211_sta_tear_down_BA_sessions(struct ieee80211_sub_if_data *sdata, u8 *addr)
454 struct ieee80211_local *local = sdata->local;
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,
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;
475 if ((tid >= STA_TID_NUM) || !(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
478 #ifdef CONFIG_MAC80211_HT_DEBUG
479 printk(KERN_DEBUG "Open BA session requested for %pM tid %u\n",
481 #endif /* CONFIG_MAC80211_HT_DEBUG */
485 sta = sta_info_get(local, ra);
487 #ifdef CONFIG_MAC80211_HT_DEBUG
488 printk(KERN_DEBUG "Could not find the station\n");
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) {
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 */
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
519 printk(KERN_ERR "allocate tx mlme to tid %d failed\n",
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 */
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;
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);
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
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"
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,
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);
599 kfree(sta->ampdu_mlme.tid_tx[tid]);
600 sta->ampdu_mlme.tid_tx[tid] = NULL;
603 spin_unlock_bh(&sta->lock);
608 EXPORT_SYMBOL(ieee80211_start_tx_ba_session);
610 int ieee80211_stop_tx_ba_session(struct ieee80211_hw *hw,
612 enum ieee80211_back_parties initiator)
614 struct ieee80211_local *local = hw_to_local(hw);
615 struct sta_info *sta;
619 if (tid >= STA_TID_NUM)
623 sta = sta_info_get(local, ra);
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) {
638 #ifdef CONFIG_MAC80211_HT_DEBUG
639 printk(KERN_DEBUG "Tx BA session stop requested for %pM tid %u\n",
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 */
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]);
663 spin_unlock_bh(&sta->lock);
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;
675 if (tid >= STA_TID_NUM) {
676 #ifdef CONFIG_MAC80211_HT_DEBUG
677 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
684 sta = sta_info_get(local, ra);
687 #ifdef CONFIG_MAC80211_HT_DEBUG
688 printk(KERN_DEBUG "Could not find station: %pM\n", ra);
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",
701 spin_unlock_bh(&sta->lock);
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);
714 if (hw->ampdu_queues)
715 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
717 spin_unlock_bh(&sta->lock);
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;
729 if (tid >= STA_TID_NUM) {
730 #ifdef CONFIG_MAC80211_HT_DEBUG
731 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
737 #ifdef CONFIG_MAC80211_HT_DEBUG
738 printk(KERN_DEBUG "Stopping Tx BA session for %pM tid %d\n",
740 #endif /* CONFIG_MAC80211_HT_DEBUG */
743 sta = sta_info_get(local, ra);
745 #ifdef CONFIG_MAC80211_HT_DEBUG
746 printk(KERN_DEBUG "Could not find station: %pM\n", ra);
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");
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,
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);
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
802 printk(KERN_WARNING "%s: Not enough memory, "
803 "dropping start BA session", skb->dev->name);
807 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
808 memcpy(&ra_tid->ra, ra, ETH_ALEN);
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
827 printk(KERN_WARNING "%s: Not enough memory, "
828 "dropping stop BA session", skb->dev->name);
832 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
833 memcpy(&ra_tid->ra, ra, ETH_ALEN);
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,
858 #ifdef CONFIG_MAC80211_HT_DEBUG
859 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
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,
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;
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);
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
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,
905 #endif /* CONFIG_MAC80211_HT_DEBUG */
908 /* determine default buffer size */
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
924 printk(KERN_DEBUG "unexpected AddBA Req from "
927 #endif /* CONFIG_MAC80211_HT_DEBUG */
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
937 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
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 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
954 if (!tid_agg_rx->reorder_buf) {
955 #ifdef CONFIG_MAC80211_HT_DEBUG
957 printk(KERN_ERR "can not allocate reordering buffer "
960 kfree(sta->ampdu_mlme.tid_rx[tid]);
963 memset(tid_agg_rx->reorder_buf, 0,
964 buf_size * sizeof(struct sk_buff *));
966 if (local->ops->ampdu_action)
967 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
968 &sta->sta, tid, &start_seq_num);
969 #ifdef CONFIG_MAC80211_HT_DEBUG
970 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
971 #endif /* CONFIG_MAC80211_HT_DEBUG */
974 kfree(tid_agg_rx->reorder_buf);
976 sta->ampdu_mlme.tid_rx[tid] = NULL;
980 /* change state and send addba resp */
981 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
982 tid_agg_rx->dialog_token = dialog_token;
983 tid_agg_rx->ssn = start_seq_num;
984 tid_agg_rx->head_seq_num = start_seq_num;
985 tid_agg_rx->buf_size = buf_size;
986 tid_agg_rx->timeout = timeout;
987 tid_agg_rx->stored_mpdu_num = 0;
988 status = WLAN_STATUS_SUCCESS;
990 spin_unlock_bh(&sta->lock);
993 ieee80211_send_addba_resp(sta->sdata, sta->sta.addr, tid,
994 dialog_token, status, 1, buf_size, timeout);
997 void ieee80211_process_addba_resp(struct ieee80211_local *local,
998 struct sta_info *sta,
999 struct ieee80211_mgmt *mgmt,
1002 struct ieee80211_hw *hw = &local->hw;
1004 u16 tid, start_seq_num;
1007 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1008 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1010 state = &sta->ampdu_mlme.tid_state_tx[tid];
1012 spin_lock_bh(&sta->lock);
1014 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1015 spin_unlock_bh(&sta->lock);
1019 if (mgmt->u.action.u.addba_resp.dialog_token !=
1020 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
1021 spin_unlock_bh(&sta->lock);
1022 #ifdef CONFIG_MAC80211_HT_DEBUG
1023 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1024 #endif /* CONFIG_MAC80211_HT_DEBUG */
1028 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
1029 #ifdef CONFIG_MAC80211_HT_DEBUG
1030 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1031 #endif /* CONFIG_MAC80211_HT_DEBUG */
1032 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1033 == WLAN_STATUS_SUCCESS) {
1034 *state |= HT_ADDBA_RECEIVED_MSK;
1035 sta->ampdu_mlme.addba_req_num[tid] = 0;
1037 if (*state == HT_AGG_STATE_OPERATIONAL &&
1038 local->hw.ampdu_queues)
1039 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1041 if (local->ops->ampdu_action) {
1042 (void)local->ops->ampdu_action(hw,
1043 IEEE80211_AMPDU_TX_RESUME,
1044 &sta->sta, tid, &start_seq_num);
1046 #ifdef CONFIG_MAC80211_HT_DEBUG
1047 printk(KERN_DEBUG "Resuming TX aggregation for tid %d\n", tid);
1048 #endif /* CONFIG_MAC80211_HT_DEBUG */
1049 spin_unlock_bh(&sta->lock);
1051 sta->ampdu_mlme.addba_req_num[tid]++;
1052 /* this will allow the state check in stop_BA_session */
1053 *state = HT_AGG_STATE_OPERATIONAL;
1054 spin_unlock_bh(&sta->lock);
1055 ieee80211_stop_tx_ba_session(hw, sta->sta.addr, tid,
1056 WLAN_BACK_INITIATOR);
1060 void ieee80211_process_delba(struct ieee80211_sub_if_data *sdata,
1061 struct sta_info *sta,
1062 struct ieee80211_mgmt *mgmt, size_t len)
1064 struct ieee80211_local *local = sdata->local;
1068 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1069 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1070 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1072 #ifdef CONFIG_MAC80211_HT_DEBUG
1073 if (net_ratelimit())
1074 printk(KERN_DEBUG "delba from %pM (%s) tid %d reason code %d\n",
1075 mgmt->sa, initiator ? "initiator" : "recipient", tid,
1076 mgmt->u.action.u.delba.reason_code);
1077 #endif /* CONFIG_MAC80211_HT_DEBUG */
1079 if (initiator == WLAN_BACK_INITIATOR)
1080 ieee80211_sta_stop_rx_ba_session(sdata, sta->sta.addr, tid,
1081 WLAN_BACK_INITIATOR, 0);
1082 else { /* WLAN_BACK_RECIPIENT */
1083 spin_lock_bh(&sta->lock);
1084 sta->ampdu_mlme.tid_state_tx[tid] =
1085 HT_AGG_STATE_OPERATIONAL;
1086 spin_unlock_bh(&sta->lock);
1087 ieee80211_stop_tx_ba_session(&local->hw, sta->sta.addr, tid,
1088 WLAN_BACK_RECIPIENT);