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-2009, 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/mac80211.h>
18 #include "ieee80211_i.h"
24 * Aggregation on the TX side requires setting the hardware flag
25 * %IEEE80211_HW_AMPDU_AGGREGATION as well as, if present, the @ampdu_queues
26 * hardware parameter to the number of hardware AMPDU queues. If there are no
27 * hardware queues then the driver will (currently) have to do all frame
30 * When TX aggregation is started by some subsystem (usually the rate control
31 * algorithm would be appropriate) by calling the
32 * ieee80211_start_tx_ba_session() function, the driver will be notified via
33 * its @ampdu_action function, with the %IEEE80211_AMPDU_TX_START action.
35 * In response to that, the driver is later required to call the
36 * ieee80211_start_tx_ba_cb() (or ieee80211_start_tx_ba_cb_irqsafe())
37 * function, which will start the aggregation session.
39 * Similarly, when the aggregation session is stopped by
40 * ieee80211_stop_tx_ba_session(), the driver's @ampdu_action function will
41 * be called with the action %IEEE80211_AMPDU_TX_STOP. In this case, the
42 * call must not fail, and the driver must later call ieee80211_stop_tx_ba_cb()
43 * (or ieee80211_stop_tx_ba_cb_irqsafe()).
46 static void ieee80211_send_addba_request(struct ieee80211_sub_if_data *sdata,
47 const u8 *da, u16 tid,
48 u8 dialog_token, u16 start_seq_num,
49 u16 agg_size, u16 timeout)
51 struct ieee80211_local *local = sdata->local;
52 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
54 struct ieee80211_mgmt *mgmt;
57 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
60 printk(KERN_ERR "%s: failed to allocate buffer "
61 "for addba request frame\n", sdata->dev->name);
64 skb_reserve(skb, local->hw.extra_tx_headroom);
65 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
67 memcpy(mgmt->da, da, ETH_ALEN);
68 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
69 if (sdata->vif.type == NL80211_IFTYPE_AP ||
70 sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
71 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
73 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
75 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
76 IEEE80211_STYPE_ACTION);
78 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
80 mgmt->u.action.category = WLAN_CATEGORY_BACK;
81 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
83 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
84 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
85 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
86 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
88 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
90 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
91 mgmt->u.action.u.addba_req.start_seq_num =
92 cpu_to_le16(start_seq_num << 4);
94 ieee80211_tx_skb(sdata, skb, 1);
97 void ieee80211_send_bar(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid, u16 ssn)
99 struct ieee80211_local *local = sdata->local;
101 struct ieee80211_bar *bar;
104 skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
106 printk(KERN_ERR "%s: failed to allocate buffer for "
107 "bar frame\n", sdata->dev->name);
110 skb_reserve(skb, local->hw.extra_tx_headroom);
111 bar = (struct ieee80211_bar *)skb_put(skb, sizeof(*bar));
112 memset(bar, 0, sizeof(*bar));
113 bar->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
114 IEEE80211_STYPE_BACK_REQ);
115 memcpy(bar->ra, ra, ETH_ALEN);
116 memcpy(bar->ta, sdata->dev->dev_addr, ETH_ALEN);
117 bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
118 bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
119 bar_control |= (u16)(tid << 12);
120 bar->control = cpu_to_le16(bar_control);
121 bar->start_seq_num = cpu_to_le16(ssn);
123 ieee80211_tx_skb(sdata, skb, 0);
127 * After sending add Block Ack request we activated a timer until
128 * add Block Ack response will arrive from the recipient.
129 * If this timer expires sta_addba_resp_timer_expired will be executed.
131 static void sta_addba_resp_timer_expired(unsigned long data)
133 /* not an elegant detour, but there is no choice as the timer passes
134 * only one argument, and both sta_info and TID are needed, so init
135 * flow in sta_info_create gives the TID as data, while the timer_to_id
136 * array gives the sta through container_of */
137 u16 tid = *(u8 *)data;
138 struct sta_info *temp_sta = container_of((void *)data,
139 struct sta_info, timer_to_tid[tid]);
141 struct ieee80211_local *local = temp_sta->local;
142 struct ieee80211_hw *hw = &local->hw;
143 struct sta_info *sta;
148 sta = sta_info_get(local, temp_sta->sta.addr);
154 state = &sta->ampdu_mlme.tid_state_tx[tid];
155 /* check if the TID waits for addBA response */
156 spin_lock_bh(&sta->lock);
157 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
158 spin_unlock_bh(&sta->lock);
159 *state = HT_AGG_STATE_IDLE;
160 #ifdef CONFIG_MAC80211_HT_DEBUG
161 printk(KERN_DEBUG "timer expired on tid %d but we are not "
162 "expecting addBA response there", tid);
164 goto timer_expired_exit;
167 #ifdef CONFIG_MAC80211_HT_DEBUG
168 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
171 /* go through the state check in stop_BA_session */
172 *state = HT_AGG_STATE_OPERATIONAL;
173 spin_unlock_bh(&sta->lock);
174 ieee80211_stop_tx_ba_session(hw, temp_sta->sta.addr, tid,
175 WLAN_BACK_INITIATOR);
181 int ieee80211_start_tx_ba_session(struct ieee80211_hw *hw, u8 *ra, u16 tid)
183 struct ieee80211_local *local = hw_to_local(hw);
184 struct sta_info *sta;
185 struct ieee80211_sub_if_data *sdata;
190 if ((tid >= STA_TID_NUM) || !(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
193 #ifdef CONFIG_MAC80211_HT_DEBUG
194 printk(KERN_DEBUG "Open BA session requested for %pM tid %u\n",
196 #endif /* CONFIG_MAC80211_HT_DEBUG */
200 sta = sta_info_get(local, ra);
202 #ifdef CONFIG_MAC80211_HT_DEBUG
203 printk(KERN_DEBUG "Could not find the station\n");
210 * The aggregation code is not prepared to handle
211 * anything but STA/AP due to the BSSID handling.
212 * IBSS could work in the code but isn't supported
213 * by drivers or the standard.
215 if (sta->sdata->vif.type != NL80211_IFTYPE_STATION &&
216 sta->sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
217 sta->sdata->vif.type != NL80211_IFTYPE_AP) {
222 spin_lock_bh(&sta->lock);
224 /* we have tried too many times, receiver does not want A-MPDU */
225 if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_MAX_RETRIES) {
230 state = &sta->ampdu_mlme.tid_state_tx[tid];
231 /* check if the TID is not in aggregation flow already */
232 if (*state != HT_AGG_STATE_IDLE) {
233 #ifdef CONFIG_MAC80211_HT_DEBUG
234 printk(KERN_DEBUG "BA request denied - session is not "
235 "idle on tid %u\n", tid);
236 #endif /* CONFIG_MAC80211_HT_DEBUG */
241 /* prepare A-MPDU MLME for Tx aggregation */
242 sta->ampdu_mlme.tid_tx[tid] =
243 kmalloc(sizeof(struct tid_ampdu_tx), GFP_ATOMIC);
244 if (!sta->ampdu_mlme.tid_tx[tid]) {
245 #ifdef CONFIG_MAC80211_HT_DEBUG
247 printk(KERN_ERR "allocate tx mlme to tid %d failed\n",
254 sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.function =
255 sta_addba_resp_timer_expired;
256 sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.data =
257 (unsigned long)&sta->timer_to_tid[tid];
258 init_timer(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
260 if (hw->ampdu_queues) {
261 /* create a new queue for this aggregation */
262 ret = ieee80211_ht_agg_queue_add(local, sta, tid);
264 /* case no queue is available to aggregation
265 * don't switch to aggregation */
267 #ifdef CONFIG_MAC80211_HT_DEBUG
268 printk(KERN_DEBUG "BA request denied - "
269 "queue unavailable for tid %d\n", tid);
270 #endif /* CONFIG_MAC80211_HT_DEBUG */
271 goto err_unlock_queue;
276 /* Ok, the Addba frame hasn't been sent yet, but if the driver calls the
277 * call back right away, it must see that the flow has begun */
278 *state |= HT_ADDBA_REQUESTED_MSK;
280 /* This is slightly racy because the queue isn't stopped */
281 start_seq_num = sta->tid_seq[tid];
283 if (local->ops->ampdu_action)
284 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_TX_START,
285 &sta->sta, tid, &start_seq_num);
288 /* No need to requeue the packets in the agg queue, since we
289 * held the tx lock: no packet could be enqueued to the newly
291 if (hw->ampdu_queues)
292 ieee80211_ht_agg_queue_remove(local, sta, tid, 0);
293 #ifdef CONFIG_MAC80211_HT_DEBUG
294 printk(KERN_DEBUG "BA request denied - HW unavailable for"
296 #endif /* CONFIG_MAC80211_HT_DEBUG */
297 *state = HT_AGG_STATE_IDLE;
298 goto err_unlock_queue;
301 /* Will put all the packets in the new SW queue */
302 if (hw->ampdu_queues)
303 ieee80211_requeue(local, ieee802_1d_to_ac[tid]);
304 spin_unlock_bh(&sta->lock);
306 /* send an addBA request */
307 sta->ampdu_mlme.dialog_token_allocator++;
308 sta->ampdu_mlme.tid_tx[tid]->dialog_token =
309 sta->ampdu_mlme.dialog_token_allocator;
310 sta->ampdu_mlme.tid_tx[tid]->ssn = start_seq_num;
313 ieee80211_send_addba_request(sta->sdata, ra, tid,
314 sta->ampdu_mlme.tid_tx[tid]->dialog_token,
315 sta->ampdu_mlme.tid_tx[tid]->ssn,
317 /* activate the timer for the recipient's addBA response */
318 sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.expires =
319 jiffies + ADDBA_RESP_INTERVAL;
320 add_timer(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
321 #ifdef CONFIG_MAC80211_HT_DEBUG
322 printk(KERN_DEBUG "activated addBA response timer on tid %d\n", tid);
327 kfree(sta->ampdu_mlme.tid_tx[tid]);
328 sta->ampdu_mlme.tid_tx[tid] = NULL;
331 spin_unlock_bh(&sta->lock);
336 EXPORT_SYMBOL(ieee80211_start_tx_ba_session);
338 void ieee80211_start_tx_ba_cb(struct ieee80211_hw *hw, u8 *ra, u16 tid)
340 struct ieee80211_local *local = hw_to_local(hw);
341 struct sta_info *sta;
344 if (tid >= STA_TID_NUM) {
345 #ifdef CONFIG_MAC80211_HT_DEBUG
346 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
353 sta = sta_info_get(local, ra);
356 #ifdef CONFIG_MAC80211_HT_DEBUG
357 printk(KERN_DEBUG "Could not find station: %pM\n", ra);
362 state = &sta->ampdu_mlme.tid_state_tx[tid];
363 spin_lock_bh(&sta->lock);
365 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
366 #ifdef CONFIG_MAC80211_HT_DEBUG
367 printk(KERN_DEBUG "addBA was not requested yet, state is %d\n",
370 spin_unlock_bh(&sta->lock);
375 WARN_ON_ONCE(*state & HT_ADDBA_DRV_READY_MSK);
377 *state |= HT_ADDBA_DRV_READY_MSK;
379 if (*state == HT_AGG_STATE_OPERATIONAL) {
380 #ifdef CONFIG_MAC80211_HT_DEBUG
381 printk(KERN_DEBUG "Aggregation is on for tid %d \n", tid);
383 if (hw->ampdu_queues)
384 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
386 spin_unlock_bh(&sta->lock);
389 EXPORT_SYMBOL(ieee80211_start_tx_ba_cb);
391 void ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_hw *hw,
392 const u8 *ra, u16 tid)
394 struct ieee80211_local *local = hw_to_local(hw);
395 struct ieee80211_ra_tid *ra_tid;
396 struct sk_buff *skb = dev_alloc_skb(0);
398 if (unlikely(!skb)) {
399 #ifdef CONFIG_MAC80211_HT_DEBUG
401 printk(KERN_WARNING "%s: Not enough memory, "
402 "dropping start BA session", skb->dev->name);
406 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
407 memcpy(&ra_tid->ra, ra, ETH_ALEN);
410 skb->pkt_type = IEEE80211_ADDBA_MSG;
411 skb_queue_tail(&local->skb_queue, skb);
412 tasklet_schedule(&local->tasklet);
414 EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);
417 int ieee80211_stop_tx_ba_session(struct ieee80211_hw *hw,
419 enum ieee80211_back_parties initiator)
421 struct ieee80211_local *local = hw_to_local(hw);
422 struct sta_info *sta;
426 if (tid >= STA_TID_NUM)
430 sta = sta_info_get(local, ra);
436 /* check if the TID is in aggregation */
437 state = &sta->ampdu_mlme.tid_state_tx[tid];
438 spin_lock_bh(&sta->lock);
440 if (*state != HT_AGG_STATE_OPERATIONAL) {
445 #ifdef CONFIG_MAC80211_HT_DEBUG
446 printk(KERN_DEBUG "Tx BA session stop requested for %pM tid %u\n",
448 #endif /* CONFIG_MAC80211_HT_DEBUG */
450 if (hw->ampdu_queues)
451 ieee80211_stop_queue(hw, sta->tid_to_tx_q[tid]);
453 *state = HT_AGG_STATE_REQ_STOP_BA_MSK |
454 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
456 if (local->ops->ampdu_action)
457 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_TX_STOP,
458 &sta->sta, tid, NULL);
460 /* HW shall not deny going back to legacy */
462 *state = HT_AGG_STATE_OPERATIONAL;
463 if (hw->ampdu_queues)
464 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
469 spin_unlock_bh(&sta->lock);
473 EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);
475 void ieee80211_stop_tx_ba_cb(struct ieee80211_hw *hw, u8 *ra, u8 tid)
477 struct ieee80211_local *local = hw_to_local(hw);
478 struct sta_info *sta;
482 if (tid >= STA_TID_NUM) {
483 #ifdef CONFIG_MAC80211_HT_DEBUG
484 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
490 #ifdef CONFIG_MAC80211_HT_DEBUG
491 printk(KERN_DEBUG "Stopping Tx BA session for %pM tid %d\n",
493 #endif /* CONFIG_MAC80211_HT_DEBUG */
496 sta = sta_info_get(local, ra);
498 #ifdef CONFIG_MAC80211_HT_DEBUG
499 printk(KERN_DEBUG "Could not find station: %pM\n", ra);
504 state = &sta->ampdu_mlme.tid_state_tx[tid];
506 /* NOTE: no need to use sta->lock in this state check, as
507 * ieee80211_stop_tx_ba_session will let only one stop call to
508 * pass through per sta/tid
510 if ((*state & HT_AGG_STATE_REQ_STOP_BA_MSK) == 0) {
511 #ifdef CONFIG_MAC80211_HT_DEBUG
512 printk(KERN_DEBUG "unexpected callback to A-MPDU stop\n");
518 if (*state & HT_AGG_STATE_INITIATOR_MSK)
519 ieee80211_send_delba(sta->sdata, ra, tid,
520 WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);
522 if (hw->ampdu_queues) {
523 agg_queue = sta->tid_to_tx_q[tid];
524 ieee80211_ht_agg_queue_remove(local, sta, tid, 1);
526 /* We just requeued the all the frames that were in the
527 * removed queue, and since we might miss a softirq we do
528 * netif_schedule_queue. ieee80211_wake_queue is not used
529 * here as this queue is not necessarily stopped
531 netif_schedule_queue(netdev_get_tx_queue(local->mdev,
534 spin_lock_bh(&sta->lock);
535 *state = HT_AGG_STATE_IDLE;
536 sta->ampdu_mlme.addba_req_num[tid] = 0;
537 kfree(sta->ampdu_mlme.tid_tx[tid]);
538 sta->ampdu_mlme.tid_tx[tid] = NULL;
539 spin_unlock_bh(&sta->lock);
543 EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb);
545 void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_hw *hw,
546 const u8 *ra, u16 tid)
548 struct ieee80211_local *local = hw_to_local(hw);
549 struct ieee80211_ra_tid *ra_tid;
550 struct sk_buff *skb = dev_alloc_skb(0);
552 if (unlikely(!skb)) {
553 #ifdef CONFIG_MAC80211_HT_DEBUG
555 printk(KERN_WARNING "%s: Not enough memory, "
556 "dropping stop BA session", skb->dev->name);
560 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
561 memcpy(&ra_tid->ra, ra, ETH_ALEN);
564 skb->pkt_type = IEEE80211_DELBA_MSG;
565 skb_queue_tail(&local->skb_queue, skb);
566 tasklet_schedule(&local->tasklet);
568 EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);
571 void ieee80211_process_addba_resp(struct ieee80211_local *local,
572 struct sta_info *sta,
573 struct ieee80211_mgmt *mgmt,
576 struct ieee80211_hw *hw = &local->hw;
578 u16 tid, start_seq_num;
581 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
582 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
584 state = &sta->ampdu_mlme.tid_state_tx[tid];
586 spin_lock_bh(&sta->lock);
588 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
589 spin_unlock_bh(&sta->lock);
593 if (mgmt->u.action.u.addba_resp.dialog_token !=
594 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
595 spin_unlock_bh(&sta->lock);
596 #ifdef CONFIG_MAC80211_HT_DEBUG
597 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
598 #endif /* CONFIG_MAC80211_HT_DEBUG */
602 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
603 #ifdef CONFIG_MAC80211_HT_DEBUG
604 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
605 #endif /* CONFIG_MAC80211_HT_DEBUG */
606 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
607 == WLAN_STATUS_SUCCESS) {
608 *state |= HT_ADDBA_RECEIVED_MSK;
609 sta->ampdu_mlme.addba_req_num[tid] = 0;
611 if (*state == HT_AGG_STATE_OPERATIONAL &&
612 local->hw.ampdu_queues)
613 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
615 if (local->ops->ampdu_action) {
616 (void)local->ops->ampdu_action(hw,
617 IEEE80211_AMPDU_TX_RESUME,
618 &sta->sta, tid, &start_seq_num);
620 #ifdef CONFIG_MAC80211_HT_DEBUG
621 printk(KERN_DEBUG "Resuming TX aggregation for tid %d\n", tid);
622 #endif /* CONFIG_MAC80211_HT_DEBUG */
623 spin_unlock_bh(&sta->lock);
625 sta->ampdu_mlme.addba_req_num[tid]++;
626 /* this will allow the state check in stop_BA_session */
627 *state = HT_AGG_STATE_OPERATIONAL;
628 spin_unlock_bh(&sta->lock);
629 ieee80211_stop_tx_ba_session(hw, sta->sta.addr, tid,
630 WLAN_BACK_INITIATOR);