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_ht_cap *ht_cap_ie,
24 struct ieee80211_sta_ht_cap *ht_cap)
29 memset(ht_cap, 0, sizeof(*ht_cap));
32 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
34 ht_cap->ht_supported = true;
35 ht_cap->cap = le16_to_cpu(ht_cap_ie->cap_info);
36 ht_cap->ampdu_factor =
37 ampdu_info & IEEE80211_HT_AMPDU_PARM_FACTOR;
38 ht_cap->ampdu_density =
39 (ampdu_info & IEEE80211_HT_AMPDU_PARM_DENSITY) >> 2;
40 memcpy(&ht_cap->mcs, &ht_cap_ie->mcs, sizeof(ht_cap->mcs));
42 ht_cap->ht_supported = false;
45 void ieee80211_ht_info_ie_to_ht_bss_info(
46 struct ieee80211_ht_info *ht_add_info_ie,
47 struct ieee80211_ht_bss_info *bss_info)
51 memset(bss_info, 0, sizeof(*bss_info));
55 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
57 bss_info->primary_channel = ht_add_info_ie->control_chan;
58 bss_info->bss_cap = ht_add_info_ie->ht_param;
59 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
64 * ieee80211_handle_ht should be called only after the operating band
65 * has been determined as ht configuration depends on the hw's
66 * HT abilities for a specific band.
68 u32 ieee80211_handle_ht(struct ieee80211_local *local,
69 struct ieee80211_sta_ht_cap *req_ht_cap,
70 struct ieee80211_ht_bss_info *req_bss_cap)
72 struct ieee80211_conf *conf = &local->hw.conf;
73 struct ieee80211_supported_band *sband;
74 struct ieee80211_sta_ht_cap ht_cap;
75 struct ieee80211_ht_bss_info ht_bss_conf;
80 bool enable_ht = true;
82 sband = local->hw.wiphy->bands[conf->channel->band];
84 memset(&ht_cap, 0, sizeof(ht_cap));
85 memset(&ht_bss_conf, 0, sizeof(struct ieee80211_ht_bss_info));
87 /* HT is not supported */
88 if (!sband->ht_cap.ht_supported)
93 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)
94 changed |= BSS_CHANGED_HT;
95 conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
96 conf->ht_cap.ht_supported = false;
101 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE))
102 changed |= BSS_CHANGED_HT;
104 conf->flags |= IEEE80211_CONF_SUPPORT_HT_MODE;
105 ht_cap.ht_supported = true;
107 ht_cap.cap = req_ht_cap->cap & sband->ht_cap.cap;
108 ht_cap.cap &= ~IEEE80211_HT_CAP_SM_PS;
109 ht_cap.cap |= sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS;
111 ht_bss_conf.primary_channel = req_bss_cap->primary_channel;
112 ht_bss_conf.bss_cap = req_bss_cap->bss_cap;
113 ht_bss_conf.bss_op_mode = req_bss_cap->bss_op_mode;
115 ht_cap.ampdu_factor = req_ht_cap->ampdu_factor;
116 ht_cap.ampdu_density = req_ht_cap->ampdu_density;
118 /* own MCS TX capabilities */
119 tx_mcs_set_cap = sband->ht_cap.mcs.tx_params;
122 * configure supported Tx MCS according to requested MCS
123 * (based in most cases on Rx capabilities of peer) and self
124 * Tx MCS capabilities (as defined by low level driver HW
128 /* can we TX with MCS rates? */
129 if (!(tx_mcs_set_cap & IEEE80211_HT_MCS_TX_DEFINED))
132 /* Counting from 0, therefore +1 */
133 if (tx_mcs_set_cap & IEEE80211_HT_MCS_TX_RX_DIFF)
135 ((tx_mcs_set_cap & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
136 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
138 max_tx_streams = IEEE80211_HT_MCS_TX_MAX_STREAMS;
141 * 802.11n D5.0 20.3.5 / 20.6 says:
142 * - indices 0 to 7 and 32 are single spatial stream
143 * - 8 to 31 are multiple spatial streams using equal modulation
144 * [8..15 for two streams, 16..23 for three and 24..31 for four]
145 * - remainder are multiple spatial streams using unequal modulation
147 for (i = 0; i < max_tx_streams; i++)
148 ht_cap.mcs.rx_mask[i] =
149 sband->ht_cap.mcs.rx_mask[i] &
150 req_ht_cap->mcs.rx_mask[i];
152 if (tx_mcs_set_cap & IEEE80211_HT_MCS_TX_UNEQUAL_MODULATION)
153 for (i = IEEE80211_HT_MCS_UNEQUAL_MODULATION_START_BYTE;
154 i < IEEE80211_HT_MCS_MASK_LEN; i++)
155 ht_cap.mcs.rx_mask[i] =
156 sband->ht_cap.mcs.rx_mask[i] &
157 req_ht_cap->mcs.rx_mask[i];
159 /* handle MCS rate 32 too */
160 if (sband->ht_cap.mcs.rx_mask[32/8] &
161 req_ht_cap->mcs.rx_mask[32/8] & 1)
162 ht_cap.mcs.rx_mask[32/8] |= 1;
165 /* if bss configuration changed store the new one */
166 if (memcmp(&conf->ht_cap, &ht_cap, sizeof(ht_cap)) ||
167 memcmp(&conf->ht_bss_conf, &ht_bss_conf, sizeof(ht_bss_conf))) {
168 changed |= BSS_CHANGED_HT;
169 memcpy(&conf->ht_cap, &ht_cap, sizeof(ht_cap));
170 memcpy(&conf->ht_bss_conf, &ht_bss_conf, sizeof(ht_bss_conf));
176 static void ieee80211_send_addba_request(struct ieee80211_sub_if_data *sdata,
177 const u8 *da, u16 tid,
178 u8 dialog_token, u16 start_seq_num,
179 u16 agg_size, u16 timeout)
181 struct ieee80211_local *local = sdata->local;
182 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
184 struct ieee80211_mgmt *mgmt;
187 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
190 printk(KERN_ERR "%s: failed to allocate buffer "
191 "for addba request frame\n", sdata->dev->name);
194 skb_reserve(skb, local->hw.extra_tx_headroom);
195 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
197 memcpy(mgmt->da, da, ETH_ALEN);
198 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
199 if (sdata->vif.type == NL80211_IFTYPE_AP)
200 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
202 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
204 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
205 IEEE80211_STYPE_ACTION);
207 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
209 mgmt->u.action.category = WLAN_CATEGORY_BACK;
210 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
212 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
213 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
214 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
215 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
217 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
219 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
220 mgmt->u.action.u.addba_req.start_seq_num =
221 cpu_to_le16(start_seq_num << 4);
223 ieee80211_tx_skb(sdata, skb, 0);
226 static void ieee80211_send_addba_resp(struct ieee80211_sub_if_data *sdata, u8 *da, u16 tid,
227 u8 dialog_token, u16 status, u16 policy,
228 u16 buf_size, u16 timeout)
230 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
231 struct ieee80211_local *local = sdata->local;
233 struct ieee80211_mgmt *mgmt;
236 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
239 printk(KERN_DEBUG "%s: failed to allocate buffer "
240 "for addba resp frame\n", sdata->dev->name);
244 skb_reserve(skb, local->hw.extra_tx_headroom);
245 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
247 memcpy(mgmt->da, da, ETH_ALEN);
248 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
249 if (sdata->vif.type == NL80211_IFTYPE_AP)
250 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
252 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
253 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
254 IEEE80211_STYPE_ACTION);
256 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
257 mgmt->u.action.category = WLAN_CATEGORY_BACK;
258 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
259 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
261 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
262 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
263 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
265 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
266 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
267 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
269 ieee80211_tx_skb(sdata, skb, 0);
272 static void ieee80211_send_delba(struct ieee80211_sub_if_data *sdata,
273 const u8 *da, u16 tid,
274 u16 initiator, u16 reason_code)
276 struct ieee80211_local *local = sdata->local;
277 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
279 struct ieee80211_mgmt *mgmt;
282 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
285 printk(KERN_ERR "%s: failed to allocate buffer "
286 "for delba frame\n", sdata->dev->name);
290 skb_reserve(skb, local->hw.extra_tx_headroom);
291 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
293 memcpy(mgmt->da, da, ETH_ALEN);
294 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
295 if (sdata->vif.type == NL80211_IFTYPE_AP)
296 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
298 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
299 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
300 IEEE80211_STYPE_ACTION);
302 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
304 mgmt->u.action.category = WLAN_CATEGORY_BACK;
305 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
306 params = (u16)(initiator << 11); /* bit 11 initiator */
307 params |= (u16)(tid << 12); /* bit 15:12 TID number */
309 mgmt->u.action.u.delba.params = cpu_to_le16(params);
310 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
312 ieee80211_tx_skb(sdata, skb, 0);
315 void ieee80211_send_bar(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid, u16 ssn)
317 struct ieee80211_local *local = sdata->local;
319 struct ieee80211_bar *bar;
322 skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
324 printk(KERN_ERR "%s: failed to allocate buffer for "
325 "bar frame\n", sdata->dev->name);
328 skb_reserve(skb, local->hw.extra_tx_headroom);
329 bar = (struct ieee80211_bar *)skb_put(skb, sizeof(*bar));
330 memset(bar, 0, sizeof(*bar));
331 bar->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
332 IEEE80211_STYPE_BACK_REQ);
333 memcpy(bar->ra, ra, ETH_ALEN);
334 memcpy(bar->ta, sdata->dev->dev_addr, ETH_ALEN);
335 bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
336 bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
337 bar_control |= (u16)(tid << 12);
338 bar->control = cpu_to_le16(bar_control);
339 bar->start_seq_num = cpu_to_le16(ssn);
341 ieee80211_tx_skb(sdata, skb, 0);
344 void ieee80211_sta_stop_rx_ba_session(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid,
345 u16 initiator, u16 reason)
347 struct ieee80211_local *local = sdata->local;
348 struct ieee80211_hw *hw = &local->hw;
349 struct sta_info *sta;
354 sta = sta_info_get(local, ra);
360 /* check if TID is in operational state */
361 spin_lock_bh(&sta->lock);
362 if (sta->ampdu_mlme.tid_state_rx[tid]
363 != HT_AGG_STATE_OPERATIONAL) {
364 spin_unlock_bh(&sta->lock);
368 sta->ampdu_mlme.tid_state_rx[tid] =
369 HT_AGG_STATE_REQ_STOP_BA_MSK |
370 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
371 spin_unlock_bh(&sta->lock);
373 /* stop HW Rx aggregation. ampdu_action existence
374 * already verified in session init so we add the BUG_ON */
375 BUG_ON(!local->ops->ampdu_action);
377 #ifdef CONFIG_MAC80211_HT_DEBUG
378 printk(KERN_DEBUG "Rx BA session stop requested for %pM tid %u\n",
380 #endif /* CONFIG_MAC80211_HT_DEBUG */
382 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
383 &sta->sta, tid, NULL);
385 printk(KERN_DEBUG "HW problem - can not stop rx "
386 "aggregation for tid %d\n", tid);
388 /* shutdown timer has not expired */
389 if (initiator != WLAN_BACK_TIMER)
390 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
392 /* check if this is a self generated aggregation halt */
393 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
394 ieee80211_send_delba(sdata, ra, tid, 0, reason);
396 /* free the reordering buffer */
397 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
398 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
399 /* release the reordered frames */
400 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
401 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
402 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
406 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
407 kfree(sta->ampdu_mlme.tid_rx[tid]);
408 sta->ampdu_mlme.tid_rx[tid] = NULL;
409 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
416 * After sending add Block Ack request we activated a timer until
417 * add Block Ack response will arrive from the recipient.
418 * If this timer expires sta_addba_resp_timer_expired will be executed.
420 static void sta_addba_resp_timer_expired(unsigned long data)
422 /* not an elegant detour, but there is no choice as the timer passes
423 * only one argument, and both sta_info and TID are needed, so init
424 * flow in sta_info_create gives the TID as data, while the timer_to_id
425 * array gives the sta through container_of */
426 u16 tid = *(u8 *)data;
427 struct sta_info *temp_sta = container_of((void *)data,
428 struct sta_info, timer_to_tid[tid]);
430 struct ieee80211_local *local = temp_sta->local;
431 struct ieee80211_hw *hw = &local->hw;
432 struct sta_info *sta;
437 sta = sta_info_get(local, temp_sta->sta.addr);
443 state = &sta->ampdu_mlme.tid_state_tx[tid];
444 /* check if the TID waits for addBA response */
445 spin_lock_bh(&sta->lock);
446 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
447 spin_unlock_bh(&sta->lock);
448 *state = HT_AGG_STATE_IDLE;
449 #ifdef CONFIG_MAC80211_HT_DEBUG
450 printk(KERN_DEBUG "timer expired on tid %d but we are not "
451 "expecting addBA response there", tid);
453 goto timer_expired_exit;
456 #ifdef CONFIG_MAC80211_HT_DEBUG
457 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
460 /* go through the state check in stop_BA_session */
461 *state = HT_AGG_STATE_OPERATIONAL;
462 spin_unlock_bh(&sta->lock);
463 ieee80211_stop_tx_ba_session(hw, temp_sta->sta.addr, tid,
464 WLAN_BACK_INITIATOR);
470 void ieee80211_sta_tear_down_BA_sessions(struct ieee80211_sub_if_data *sdata, u8 *addr)
472 struct ieee80211_local *local = sdata->local;
475 for (i = 0; i < STA_TID_NUM; i++) {
476 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
477 WLAN_BACK_INITIATOR);
478 ieee80211_sta_stop_rx_ba_session(sdata, addr, i,
480 WLAN_REASON_QSTA_LEAVE_QBSS);
484 int ieee80211_start_tx_ba_session(struct ieee80211_hw *hw, u8 *ra, u16 tid)
486 struct ieee80211_local *local = hw_to_local(hw);
487 struct sta_info *sta;
488 struct ieee80211_sub_if_data *sdata;
493 if (tid >= STA_TID_NUM)
496 #ifdef CONFIG_MAC80211_HT_DEBUG
497 printk(KERN_DEBUG "Open BA session requested for %pM tid %u\n",
499 #endif /* CONFIG_MAC80211_HT_DEBUG */
503 sta = sta_info_get(local, ra);
505 #ifdef CONFIG_MAC80211_HT_DEBUG
506 printk(KERN_DEBUG "Could not find the station\n");
512 spin_lock_bh(&sta->lock);
514 /* we have tried too many times, receiver does not want A-MPDU */
515 if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_MAX_RETRIES) {
520 state = &sta->ampdu_mlme.tid_state_tx[tid];
521 /* check if the TID is not in aggregation flow already */
522 if (*state != HT_AGG_STATE_IDLE) {
523 #ifdef CONFIG_MAC80211_HT_DEBUG
524 printk(KERN_DEBUG "BA request denied - session is not "
525 "idle on tid %u\n", tid);
526 #endif /* CONFIG_MAC80211_HT_DEBUG */
531 /* prepare A-MPDU MLME for Tx aggregation */
532 sta->ampdu_mlme.tid_tx[tid] =
533 kmalloc(sizeof(struct tid_ampdu_tx), GFP_ATOMIC);
534 if (!sta->ampdu_mlme.tid_tx[tid]) {
535 #ifdef CONFIG_MAC80211_HT_DEBUG
537 printk(KERN_ERR "allocate tx mlme to tid %d failed\n",
544 sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.function =
545 sta_addba_resp_timer_expired;
546 sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.data =
547 (unsigned long)&sta->timer_to_tid[tid];
548 init_timer(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
550 /* create a new queue for this aggregation */
551 ret = ieee80211_ht_agg_queue_add(local, sta, tid);
553 /* case no queue is available to aggregation
554 * don't switch to aggregation */
556 #ifdef CONFIG_MAC80211_HT_DEBUG
557 printk(KERN_DEBUG "BA request denied - queue unavailable for"
559 #endif /* CONFIG_MAC80211_HT_DEBUG */
560 goto err_unlock_queue;
564 /* Ok, the Addba frame hasn't been sent yet, but if the driver calls the
565 * call back right away, it must see that the flow has begun */
566 *state |= HT_ADDBA_REQUESTED_MSK;
568 /* This is slightly racy because the queue isn't stopped */
569 start_seq_num = sta->tid_seq[tid];
571 if (local->ops->ampdu_action)
572 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_TX_START,
573 &sta->sta, tid, &start_seq_num);
576 /* No need to requeue the packets in the agg queue, since we
577 * held the tx lock: no packet could be enqueued to the newly
579 ieee80211_ht_agg_queue_remove(local, sta, tid, 0);
580 #ifdef CONFIG_MAC80211_HT_DEBUG
581 printk(KERN_DEBUG "BA request denied - HW unavailable for"
583 #endif /* CONFIG_MAC80211_HT_DEBUG */
584 *state = HT_AGG_STATE_IDLE;
585 goto err_unlock_queue;
588 /* Will put all the packets in the new SW queue */
589 ieee80211_requeue(local, ieee802_1d_to_ac[tid]);
590 spin_unlock_bh(&sta->lock);
592 /* send an addBA request */
593 sta->ampdu_mlme.dialog_token_allocator++;
594 sta->ampdu_mlme.tid_tx[tid]->dialog_token =
595 sta->ampdu_mlme.dialog_token_allocator;
596 sta->ampdu_mlme.tid_tx[tid]->ssn = start_seq_num;
599 ieee80211_send_addba_request(sta->sdata, ra, tid,
600 sta->ampdu_mlme.tid_tx[tid]->dialog_token,
601 sta->ampdu_mlme.tid_tx[tid]->ssn,
603 /* activate the timer for the recipient's addBA response */
604 sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.expires =
605 jiffies + ADDBA_RESP_INTERVAL;
606 add_timer(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
607 #ifdef CONFIG_MAC80211_HT_DEBUG
608 printk(KERN_DEBUG "activated addBA response timer on tid %d\n", tid);
613 kfree(sta->ampdu_mlme.tid_tx[tid]);
614 sta->ampdu_mlme.tid_tx[tid] = NULL;
617 spin_unlock_bh(&sta->lock);
622 EXPORT_SYMBOL(ieee80211_start_tx_ba_session);
624 int ieee80211_stop_tx_ba_session(struct ieee80211_hw *hw,
626 enum ieee80211_back_parties initiator)
628 struct ieee80211_local *local = hw_to_local(hw);
629 struct sta_info *sta;
633 if (tid >= STA_TID_NUM)
637 sta = sta_info_get(local, ra);
643 /* check if the TID is in aggregation */
644 state = &sta->ampdu_mlme.tid_state_tx[tid];
645 spin_lock_bh(&sta->lock);
647 if (*state != HT_AGG_STATE_OPERATIONAL) {
652 #ifdef CONFIG_MAC80211_HT_DEBUG
653 printk(KERN_DEBUG "Tx BA session stop requested for %pM tid %u\n",
655 #endif /* CONFIG_MAC80211_HT_DEBUG */
657 ieee80211_stop_queue(hw, sta->tid_to_tx_q[tid]);
659 *state = HT_AGG_STATE_REQ_STOP_BA_MSK |
660 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
662 if (local->ops->ampdu_action)
663 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_TX_STOP,
664 &sta->sta, tid, NULL);
666 /* case HW denied going back to legacy */
668 WARN_ON(ret != -EBUSY);
669 *state = HT_AGG_STATE_OPERATIONAL;
670 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
675 spin_unlock_bh(&sta->lock);
679 EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);
681 void ieee80211_start_tx_ba_cb(struct ieee80211_hw *hw, u8 *ra, u16 tid)
683 struct ieee80211_local *local = hw_to_local(hw);
684 struct sta_info *sta;
687 if (tid >= STA_TID_NUM) {
688 #ifdef CONFIG_MAC80211_HT_DEBUG
689 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
696 sta = sta_info_get(local, ra);
699 #ifdef CONFIG_MAC80211_HT_DEBUG
700 printk(KERN_DEBUG "Could not find station: %pM\n", ra);
705 state = &sta->ampdu_mlme.tid_state_tx[tid];
706 spin_lock_bh(&sta->lock);
708 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
709 #ifdef CONFIG_MAC80211_HT_DEBUG
710 printk(KERN_DEBUG "addBA was not requested yet, state is %d\n",
713 spin_unlock_bh(&sta->lock);
718 WARN_ON_ONCE(*state & HT_ADDBA_DRV_READY_MSK);
720 *state |= HT_ADDBA_DRV_READY_MSK;
722 if (*state == HT_AGG_STATE_OPERATIONAL) {
723 #ifdef CONFIG_MAC80211_HT_DEBUG
724 printk(KERN_DEBUG "Aggregation is on for tid %d \n", tid);
726 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
728 spin_unlock_bh(&sta->lock);
731 EXPORT_SYMBOL(ieee80211_start_tx_ba_cb);
733 void ieee80211_stop_tx_ba_cb(struct ieee80211_hw *hw, u8 *ra, u8 tid)
735 struct ieee80211_local *local = hw_to_local(hw);
736 struct sta_info *sta;
740 if (tid >= STA_TID_NUM) {
741 #ifdef CONFIG_MAC80211_HT_DEBUG
742 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
748 #ifdef CONFIG_MAC80211_HT_DEBUG
749 printk(KERN_DEBUG "Stopping Tx BA session for %pM tid %d\n",
751 #endif /* CONFIG_MAC80211_HT_DEBUG */
754 sta = sta_info_get(local, ra);
756 #ifdef CONFIG_MAC80211_HT_DEBUG
757 printk(KERN_DEBUG "Could not find station: %pM\n", ra);
762 state = &sta->ampdu_mlme.tid_state_tx[tid];
764 /* NOTE: no need to use sta->lock in this state check, as
765 * ieee80211_stop_tx_ba_session will let only one stop call to
766 * pass through per sta/tid
768 if ((*state & HT_AGG_STATE_REQ_STOP_BA_MSK) == 0) {
769 #ifdef CONFIG_MAC80211_HT_DEBUG
770 printk(KERN_DEBUG "unexpected callback to A-MPDU stop\n");
776 if (*state & HT_AGG_STATE_INITIATOR_MSK)
777 ieee80211_send_delba(sta->sdata, ra, tid,
778 WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);
780 agg_queue = sta->tid_to_tx_q[tid];
782 ieee80211_ht_agg_queue_remove(local, sta, tid, 1);
784 /* We just requeued the all the frames that were in the
785 * removed queue, and since we might miss a softirq we do
786 * netif_schedule_queue. ieee80211_wake_queue is not used
787 * here as this queue is not necessarily stopped
789 netif_schedule_queue(netdev_get_tx_queue(local->mdev, agg_queue));
790 spin_lock_bh(&sta->lock);
791 *state = HT_AGG_STATE_IDLE;
792 sta->ampdu_mlme.addba_req_num[tid] = 0;
793 kfree(sta->ampdu_mlme.tid_tx[tid]);
794 sta->ampdu_mlme.tid_tx[tid] = NULL;
795 spin_unlock_bh(&sta->lock);
799 EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb);
801 void ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_hw *hw,
802 const u8 *ra, u16 tid)
804 struct ieee80211_local *local = hw_to_local(hw);
805 struct ieee80211_ra_tid *ra_tid;
806 struct sk_buff *skb = dev_alloc_skb(0);
808 if (unlikely(!skb)) {
809 #ifdef CONFIG_MAC80211_HT_DEBUG
811 printk(KERN_WARNING "%s: Not enough memory, "
812 "dropping start BA session", skb->dev->name);
816 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
817 memcpy(&ra_tid->ra, ra, ETH_ALEN);
820 skb->pkt_type = IEEE80211_ADDBA_MSG;
821 skb_queue_tail(&local->skb_queue, skb);
822 tasklet_schedule(&local->tasklet);
824 EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);
826 void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_hw *hw,
827 const u8 *ra, u16 tid)
829 struct ieee80211_local *local = hw_to_local(hw);
830 struct ieee80211_ra_tid *ra_tid;
831 struct sk_buff *skb = dev_alloc_skb(0);
833 if (unlikely(!skb)) {
834 #ifdef CONFIG_MAC80211_HT_DEBUG
836 printk(KERN_WARNING "%s: Not enough memory, "
837 "dropping stop BA session", skb->dev->name);
841 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
842 memcpy(&ra_tid->ra, ra, ETH_ALEN);
845 skb->pkt_type = IEEE80211_DELBA_MSG;
846 skb_queue_tail(&local->skb_queue, skb);
847 tasklet_schedule(&local->tasklet);
849 EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);
852 * After accepting the AddBA Request we activated a timer,
853 * resetting it after each frame that arrives from the originator.
854 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
856 static void sta_rx_agg_session_timer_expired(unsigned long data)
858 /* not an elegant detour, but there is no choice as the timer passes
859 * only one argument, and various sta_info are needed here, so init
860 * flow in sta_info_create gives the TID as data, while the timer_to_id
861 * array gives the sta through container_of */
862 u8 *ptid = (u8 *)data;
863 u8 *timer_to_id = ptid - *ptid;
864 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
867 #ifdef CONFIG_MAC80211_HT_DEBUG
868 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
870 ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->sta.addr,
871 (u16)*ptid, WLAN_BACK_TIMER,
872 WLAN_REASON_QSTA_TIMEOUT);
875 void ieee80211_process_addba_request(struct ieee80211_local *local,
876 struct sta_info *sta,
877 struct ieee80211_mgmt *mgmt,
880 struct ieee80211_hw *hw = &local->hw;
881 struct ieee80211_conf *conf = &hw->conf;
882 struct tid_ampdu_rx *tid_agg_rx;
883 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
885 int ret = -EOPNOTSUPP;
887 /* extract session parameters from addba request frame */
888 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
889 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
891 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
893 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
894 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
895 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
896 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
898 status = WLAN_STATUS_REQUEST_DECLINED;
900 /* sanity check for incoming parameters:
901 * check if configuration can support the BA policy
902 * and if buffer size does not exceeds max value */
903 if (((ba_policy != 1)
904 && (!(conf->ht_cap.cap & IEEE80211_HT_CAP_DELAY_BA)))
905 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
906 status = WLAN_STATUS_INVALID_QOS_PARAM;
907 #ifdef CONFIG_MAC80211_HT_DEBUG
909 printk(KERN_DEBUG "AddBA Req with bad params from "
910 "%pM on tid %u. policy %d, buffer size %d\n",
911 mgmt->sa, tid, ba_policy,
913 #endif /* CONFIG_MAC80211_HT_DEBUG */
916 /* determine default buffer size */
918 struct ieee80211_supported_band *sband;
920 sband = local->hw.wiphy->bands[conf->channel->band];
921 buf_size = IEEE80211_MIN_AMPDU_BUF;
922 buf_size = buf_size << sband->ht_cap.ampdu_factor;
926 /* examine state machine */
927 spin_lock_bh(&sta->lock);
929 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
930 #ifdef CONFIG_MAC80211_HT_DEBUG
932 printk(KERN_DEBUG "unexpected AddBA Req from "
935 #endif /* CONFIG_MAC80211_HT_DEBUG */
939 /* prepare A-MPDU MLME for Rx aggregation */
940 sta->ampdu_mlme.tid_rx[tid] =
941 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
942 if (!sta->ampdu_mlme.tid_rx[tid]) {
943 #ifdef CONFIG_MAC80211_HT_DEBUG
945 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
951 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
952 sta_rx_agg_session_timer_expired;
953 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
954 (unsigned long)&sta->timer_to_tid[tid];
955 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
957 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
959 /* prepare reordering buffer */
960 tid_agg_rx->reorder_buf =
961 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
962 if (!tid_agg_rx->reorder_buf) {
963 #ifdef CONFIG_MAC80211_HT_DEBUG
965 printk(KERN_ERR "can not allocate reordering buffer "
968 kfree(sta->ampdu_mlme.tid_rx[tid]);
971 memset(tid_agg_rx->reorder_buf, 0,
972 buf_size * sizeof(struct sk_buff *));
974 if (local->ops->ampdu_action)
975 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
976 &sta->sta, tid, &start_seq_num);
977 #ifdef CONFIG_MAC80211_HT_DEBUG
978 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
979 #endif /* CONFIG_MAC80211_HT_DEBUG */
982 kfree(tid_agg_rx->reorder_buf);
984 sta->ampdu_mlme.tid_rx[tid] = NULL;
988 /* change state and send addba resp */
989 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
990 tid_agg_rx->dialog_token = dialog_token;
991 tid_agg_rx->ssn = start_seq_num;
992 tid_agg_rx->head_seq_num = start_seq_num;
993 tid_agg_rx->buf_size = buf_size;
994 tid_agg_rx->timeout = timeout;
995 tid_agg_rx->stored_mpdu_num = 0;
996 status = WLAN_STATUS_SUCCESS;
998 spin_unlock_bh(&sta->lock);
1001 ieee80211_send_addba_resp(sta->sdata, sta->sta.addr, tid,
1002 dialog_token, status, 1, buf_size, timeout);
1005 void ieee80211_process_addba_resp(struct ieee80211_local *local,
1006 struct sta_info *sta,
1007 struct ieee80211_mgmt *mgmt,
1010 struct ieee80211_hw *hw = &local->hw;
1015 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1016 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1018 state = &sta->ampdu_mlme.tid_state_tx[tid];
1020 spin_lock_bh(&sta->lock);
1022 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1023 spin_unlock_bh(&sta->lock);
1027 if (mgmt->u.action.u.addba_resp.dialog_token !=
1028 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
1029 spin_unlock_bh(&sta->lock);
1030 #ifdef CONFIG_MAC80211_HT_DEBUG
1031 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1032 #endif /* CONFIG_MAC80211_HT_DEBUG */
1036 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
1037 #ifdef CONFIG_MAC80211_HT_DEBUG
1038 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1039 #endif /* CONFIG_MAC80211_HT_DEBUG */
1040 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1041 == WLAN_STATUS_SUCCESS) {
1042 *state |= HT_ADDBA_RECEIVED_MSK;
1043 sta->ampdu_mlme.addba_req_num[tid] = 0;
1045 if (*state == HT_AGG_STATE_OPERATIONAL)
1046 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1048 spin_unlock_bh(&sta->lock);
1050 sta->ampdu_mlme.addba_req_num[tid]++;
1051 /* this will allow the state check in stop_BA_session */
1052 *state = HT_AGG_STATE_OPERATIONAL;
1053 spin_unlock_bh(&sta->lock);
1054 ieee80211_stop_tx_ba_session(hw, sta->sta.addr, tid,
1055 WLAN_BACK_INITIATOR);
1059 void ieee80211_process_delba(struct ieee80211_sub_if_data *sdata,
1060 struct sta_info *sta,
1061 struct ieee80211_mgmt *mgmt, size_t len)
1063 struct ieee80211_local *local = sdata->local;
1067 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1068 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1069 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1071 #ifdef CONFIG_MAC80211_HT_DEBUG
1072 if (net_ratelimit())
1073 printk(KERN_DEBUG "delba from %pM (%s) tid %d reason code %d\n",
1074 mgmt->sa, initiator ? "initiator" : "recipient", tid,
1075 mgmt->u.action.u.delba.reason_code);
1076 #endif /* CONFIG_MAC80211_HT_DEBUG */
1078 if (initiator == WLAN_BACK_INITIATOR)
1079 ieee80211_sta_stop_rx_ba_session(sdata, sta->sta.addr, tid,
1080 WLAN_BACK_INITIATOR, 0);
1081 else { /* WLAN_BACK_RECIPIENT */
1082 spin_lock_bh(&sta->lock);
1083 sta->ampdu_mlme.tid_state_tx[tid] =
1084 HT_AGG_STATE_OPERATIONAL;
1085 spin_unlock_bh(&sta->lock);
1086 ieee80211_stop_tx_ba_session(&local->hw, sta->sta.addr, tid,
1087 WLAN_BACK_RECIPIENT);