mac80211: remove bogus TX agg state assignment
[safe/jmp/linux-2.6] / net / mac80211 / agg-tx.c
1 /*
2  * HT handling
3  *
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
10  *
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.
14  */
15
16 #include <linux/ieee80211.h>
17 #include <net/mac80211.h>
18 #include "ieee80211_i.h"
19 #include "driver-ops.h"
20 #include "wme.h"
21
22 /**
23  * DOC: TX aggregation
24  *
25  * Aggregation on the TX side requires setting the hardware flag
26  * %IEEE80211_HW_AMPDU_AGGREGATION as well as, if present, the @ampdu_queues
27  * hardware parameter to the number of hardware AMPDU queues. If there are no
28  * hardware queues then the driver will (currently) have to do all frame
29  * buffering.
30  *
31  * When TX aggregation is started by some subsystem (usually the rate control
32  * algorithm would be appropriate) by calling the
33  * ieee80211_start_tx_ba_session() function, the driver will be notified via
34  * its @ampdu_action function, with the %IEEE80211_AMPDU_TX_START action.
35  *
36  * In response to that, the driver is later required to call the
37  * ieee80211_start_tx_ba_cb() (or ieee80211_start_tx_ba_cb_irqsafe())
38  * function, which will start the aggregation session.
39  *
40  * Similarly, when the aggregation session is stopped by
41  * ieee80211_stop_tx_ba_session(), the driver's @ampdu_action function will
42  * be called with the action %IEEE80211_AMPDU_TX_STOP. In this case, the
43  * call must not fail, and the driver must later call ieee80211_stop_tx_ba_cb()
44  * (or ieee80211_stop_tx_ba_cb_irqsafe()).
45  */
46
47 static void ieee80211_send_addba_request(struct ieee80211_sub_if_data *sdata,
48                                          const u8 *da, u16 tid,
49                                          u8 dialog_token, u16 start_seq_num,
50                                          u16 agg_size, u16 timeout)
51 {
52         struct ieee80211_local *local = sdata->local;
53         struct sk_buff *skb;
54         struct ieee80211_mgmt *mgmt;
55         u16 capab;
56
57         skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
58
59         if (!skb) {
60                 printk(KERN_ERR "%s: failed to allocate buffer "
61                                 "for addba request frame\n", sdata->name);
62                 return;
63         }
64         skb_reserve(skb, local->hw.extra_tx_headroom);
65         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
66         memset(mgmt, 0, 24);
67         memcpy(mgmt->da, da, ETH_ALEN);
68         memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
69         if (sdata->vif.type == NL80211_IFTYPE_AP ||
70             sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
71                 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
72         else if (sdata->vif.type == NL80211_IFTYPE_STATION)
73                 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
74
75         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
76                                           IEEE80211_STYPE_ACTION);
77
78         skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
79
80         mgmt->u.action.category = WLAN_CATEGORY_BACK;
81         mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
82
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 */
87
88         mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
89
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);
93
94         ieee80211_tx_skb(sdata, skb);
95 }
96
97 void ieee80211_send_bar(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid, u16 ssn)
98 {
99         struct ieee80211_local *local = sdata->local;
100         struct sk_buff *skb;
101         struct ieee80211_bar *bar;
102         u16 bar_control = 0;
103
104         skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
105         if (!skb) {
106                 printk(KERN_ERR "%s: failed to allocate buffer for "
107                         "bar frame\n", sdata->name);
108                 return;
109         }
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->vif.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);
122
123         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
124         ieee80211_tx_skb(sdata, skb);
125 }
126
127 int ___ieee80211_stop_tx_ba_session(struct sta_info *sta, u16 tid,
128                                     enum ieee80211_back_parties initiator)
129 {
130         struct ieee80211_local *local = sta->local;
131         int ret;
132         u8 *state;
133
134 #ifdef CONFIG_MAC80211_HT_DEBUG
135         printk(KERN_DEBUG "Tx BA session stop requested for %pM tid %u\n",
136                sta->sta.addr, tid);
137 #endif /* CONFIG_MAC80211_HT_DEBUG */
138
139         state = &sta->ampdu_mlme.tid_state_tx[tid];
140
141         if (*state == HT_AGG_STATE_OPERATIONAL)
142                 sta->ampdu_mlme.addba_req_num[tid] = 0;
143
144         *state = HT_AGG_STATE_REQ_STOP_BA_MSK |
145                 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
146
147         ret = drv_ampdu_action(local, sta->sdata,
148                                IEEE80211_AMPDU_TX_STOP,
149                                &sta->sta, tid, NULL);
150
151         /* HW shall not deny going back to legacy */
152         if (WARN_ON(ret)) {
153                 /*
154                  * We may have pending packets get stuck in this case...
155                  * Not bothering with a workaround for now.
156                  */
157         }
158
159         return ret;
160 }
161
162 /*
163  * After sending add Block Ack request we activated a timer until
164  * add Block Ack response will arrive from the recipient.
165  * If this timer expires sta_addba_resp_timer_expired will be executed.
166  */
167 static void sta_addba_resp_timer_expired(unsigned long data)
168 {
169         /* not an elegant detour, but there is no choice as the timer passes
170          * only one argument, and both sta_info and TID are needed, so init
171          * flow in sta_info_create gives the TID as data, while the timer_to_id
172          * array gives the sta through container_of */
173         u16 tid = *(u8 *)data;
174         struct sta_info *sta = container_of((void *)data,
175                 struct sta_info, timer_to_tid[tid]);
176         u8 *state;
177
178         state = &sta->ampdu_mlme.tid_state_tx[tid];
179
180         /* check if the TID waits for addBA response */
181         spin_lock_bh(&sta->lock);
182         if ((*state & (HT_ADDBA_REQUESTED_MSK | HT_ADDBA_RECEIVED_MSK |
183                        HT_AGG_STATE_REQ_STOP_BA_MSK)) !=
184                                                 HT_ADDBA_REQUESTED_MSK) {
185                 spin_unlock_bh(&sta->lock);
186 #ifdef CONFIG_MAC80211_HT_DEBUG
187                 printk(KERN_DEBUG "timer expired on tid %d but we are not "
188                                 "(or no longer) expecting addBA response there",
189                         tid);
190 #endif
191                 return;
192         }
193
194 #ifdef CONFIG_MAC80211_HT_DEBUG
195         printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
196 #endif
197
198         ___ieee80211_stop_tx_ba_session(sta, tid, WLAN_BACK_INITIATOR);
199         spin_unlock_bh(&sta->lock);
200 }
201
202 static inline int ieee80211_ac_from_tid(int tid)
203 {
204         return ieee802_1d_to_ac[tid & 7];
205 }
206
207 int ieee80211_start_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid)
208 {
209         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
210         struct ieee80211_sub_if_data *sdata = sta->sdata;
211         struct ieee80211_local *local = sdata->local;
212         u8 *state;
213         int ret = 0;
214         u16 start_seq_num;
215
216         if (WARN_ON(!local->ops->ampdu_action))
217                 return -EINVAL;
218
219         if ((tid >= STA_TID_NUM) ||
220             !(local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION))
221                 return -EINVAL;
222
223 #ifdef CONFIG_MAC80211_HT_DEBUG
224         printk(KERN_DEBUG "Open BA session requested for %pM tid %u\n",
225                pubsta->addr, tid);
226 #endif /* CONFIG_MAC80211_HT_DEBUG */
227
228         /*
229          * The aggregation code is not prepared to handle
230          * anything but STA/AP due to the BSSID handling.
231          * IBSS could work in the code but isn't supported
232          * by drivers or the standard.
233          */
234         if (sdata->vif.type != NL80211_IFTYPE_STATION &&
235             sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
236             sdata->vif.type != NL80211_IFTYPE_AP)
237                 return -EINVAL;
238
239         if (test_sta_flags(sta, WLAN_STA_DISASSOC)) {
240 #ifdef CONFIG_MAC80211_HT_DEBUG
241                 printk(KERN_DEBUG "Disassociation is in progress. "
242                        "Denying BA session request\n");
243 #endif
244                 return -EINVAL;
245         }
246
247         if (test_sta_flags(sta, WLAN_STA_SUSPEND)) {
248 #ifdef CONFIG_MAC80211_HT_DEBUG
249                 printk(KERN_DEBUG "Suspend in progress. "
250                        "Denying BA session request\n");
251 #endif
252                 return -EINVAL;
253         }
254
255         spin_lock_bh(&sta->lock);
256         spin_lock(&local->ampdu_lock);
257
258         /* we have tried too many times, receiver does not want A-MPDU */
259         if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_MAX_RETRIES) {
260                 ret = -EBUSY;
261                 goto err_unlock_sta;
262         }
263
264         state = &sta->ampdu_mlme.tid_state_tx[tid];
265         /* check if the TID is not in aggregation flow already */
266         if (*state != HT_AGG_STATE_IDLE) {
267 #ifdef CONFIG_MAC80211_HT_DEBUG
268                 printk(KERN_DEBUG "BA request denied - session is not "
269                                  "idle on tid %u\n", tid);
270 #endif /* CONFIG_MAC80211_HT_DEBUG */
271                 ret = -EAGAIN;
272                 goto err_unlock_sta;
273         }
274
275         /*
276          * While we're asking the driver about the aggregation,
277          * stop the AC queue so that we don't have to worry
278          * about frames that came in while we were doing that,
279          * which would require us to put them to the AC pending
280          * afterwards which just makes the code more complex.
281          */
282         ieee80211_stop_queue_by_reason(
283                 &local->hw, ieee80211_ac_from_tid(tid),
284                 IEEE80211_QUEUE_STOP_REASON_AGGREGATION);
285
286         /* prepare A-MPDU MLME for Tx aggregation */
287         sta->ampdu_mlme.tid_tx[tid] =
288                         kmalloc(sizeof(struct tid_ampdu_tx), GFP_ATOMIC);
289         if (!sta->ampdu_mlme.tid_tx[tid]) {
290 #ifdef CONFIG_MAC80211_HT_DEBUG
291                 if (net_ratelimit())
292                         printk(KERN_ERR "allocate tx mlme to tid %d failed\n",
293                                         tid);
294 #endif
295                 ret = -ENOMEM;
296                 goto err_wake_queue;
297         }
298
299         skb_queue_head_init(&sta->ampdu_mlme.tid_tx[tid]->pending);
300
301         /* Tx timer */
302         sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.function =
303                         sta_addba_resp_timer_expired;
304         sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.data =
305                         (unsigned long)&sta->timer_to_tid[tid];
306         init_timer(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
307
308         /* Ok, the Addba frame hasn't been sent yet, but if the driver calls the
309          * call back right away, it must see that the flow has begun */
310         *state |= HT_ADDBA_REQUESTED_MSK;
311
312         start_seq_num = sta->tid_seq[tid] >> 4;
313
314         ret = drv_ampdu_action(local, sdata, IEEE80211_AMPDU_TX_START,
315                                pubsta, tid, &start_seq_num);
316
317         if (ret) {
318 #ifdef CONFIG_MAC80211_HT_DEBUG
319                 printk(KERN_DEBUG "BA request denied - HW unavailable for"
320                                         " tid %d\n", tid);
321 #endif /* CONFIG_MAC80211_HT_DEBUG */
322                 *state = HT_AGG_STATE_IDLE;
323                 goto err_free;
324         }
325
326         /* Driver vetoed or OKed, but we can take packets again now */
327         ieee80211_wake_queue_by_reason(
328                 &local->hw, ieee80211_ac_from_tid(tid),
329                 IEEE80211_QUEUE_STOP_REASON_AGGREGATION);
330
331         spin_unlock(&local->ampdu_lock);
332         spin_unlock_bh(&sta->lock);
333
334         /* send an addBA request */
335         sta->ampdu_mlme.dialog_token_allocator++;
336         sta->ampdu_mlme.tid_tx[tid]->dialog_token =
337                         sta->ampdu_mlme.dialog_token_allocator;
338         sta->ampdu_mlme.tid_tx[tid]->ssn = start_seq_num;
339
340         ieee80211_send_addba_request(sdata, pubsta->addr, tid,
341                          sta->ampdu_mlme.tid_tx[tid]->dialog_token,
342                          sta->ampdu_mlme.tid_tx[tid]->ssn,
343                          0x40, 5000);
344         sta->ampdu_mlme.addba_req_num[tid]++;
345         /* activate the timer for the recipient's addBA response */
346         sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.expires =
347                                 jiffies + ADDBA_RESP_INTERVAL;
348         add_timer(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
349 #ifdef CONFIG_MAC80211_HT_DEBUG
350         printk(KERN_DEBUG "activated addBA response timer on tid %d\n", tid);
351 #endif
352         return 0;
353
354  err_free:
355         kfree(sta->ampdu_mlme.tid_tx[tid]);
356         sta->ampdu_mlme.tid_tx[tid] = NULL;
357  err_wake_queue:
358         ieee80211_wake_queue_by_reason(
359                 &local->hw, ieee80211_ac_from_tid(tid),
360                 IEEE80211_QUEUE_STOP_REASON_AGGREGATION);
361  err_unlock_sta:
362         spin_unlock(&local->ampdu_lock);
363         spin_unlock_bh(&sta->lock);
364         return ret;
365 }
366 EXPORT_SYMBOL(ieee80211_start_tx_ba_session);
367
368 /*
369  * splice packets from the STA's pending to the local pending,
370  * requires a call to ieee80211_agg_splice_finish and holding
371  * local->ampdu_lock across both calls.
372  */
373 static void ieee80211_agg_splice_packets(struct ieee80211_local *local,
374                                          struct sta_info *sta, u16 tid)
375 {
376         unsigned long flags;
377         u16 queue = ieee80211_ac_from_tid(tid);
378
379         ieee80211_stop_queue_by_reason(
380                 &local->hw, queue,
381                 IEEE80211_QUEUE_STOP_REASON_AGGREGATION);
382
383         if (!(sta->ampdu_mlme.tid_state_tx[tid] & HT_ADDBA_REQUESTED_MSK))
384                 return;
385
386         if (WARN(!sta->ampdu_mlme.tid_tx[tid],
387                  "TID %d gone but expected when splicing aggregates from"
388                  "the pending queue\n", tid))
389                 return;
390
391         if (!skb_queue_empty(&sta->ampdu_mlme.tid_tx[tid]->pending)) {
392                 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
393                 /* copy over remaining packets */
394                 skb_queue_splice_tail_init(
395                         &sta->ampdu_mlme.tid_tx[tid]->pending,
396                         &local->pending[queue]);
397                 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
398         }
399 }
400
401 static void ieee80211_agg_splice_finish(struct ieee80211_local *local,
402                                         struct sta_info *sta, u16 tid)
403 {
404         u16 queue = ieee80211_ac_from_tid(tid);
405
406         ieee80211_wake_queue_by_reason(
407                 &local->hw, queue,
408                 IEEE80211_QUEUE_STOP_REASON_AGGREGATION);
409 }
410
411 /* caller must hold sta->lock */
412 static void ieee80211_agg_tx_operational(struct ieee80211_local *local,
413                                          struct sta_info *sta, u16 tid)
414 {
415 #ifdef CONFIG_MAC80211_HT_DEBUG
416         printk(KERN_DEBUG "Aggregation is on for tid %d \n", tid);
417 #endif
418
419         spin_lock(&local->ampdu_lock);
420         ieee80211_agg_splice_packets(local, sta, tid);
421         /*
422          * NB: we rely on sta->lock being taken in the TX
423          * processing here when adding to the pending queue,
424          * otherwise we could only change the state of the
425          * session to OPERATIONAL _here_.
426          */
427         ieee80211_agg_splice_finish(local, sta, tid);
428         spin_unlock(&local->ampdu_lock);
429
430         drv_ampdu_action(local, sta->sdata,
431                          IEEE80211_AMPDU_TX_OPERATIONAL,
432                          &sta->sta, tid, NULL);
433 }
434
435 void ieee80211_start_tx_ba_cb(struct ieee80211_vif *vif, u8 *ra, u16 tid)
436 {
437         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
438         struct ieee80211_local *local = sdata->local;
439         struct sta_info *sta;
440         u8 *state;
441
442         if (tid >= STA_TID_NUM) {
443 #ifdef CONFIG_MAC80211_HT_DEBUG
444                 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
445                                 tid, STA_TID_NUM);
446 #endif
447                 return;
448         }
449
450         rcu_read_lock();
451         sta = sta_info_get(sdata, ra);
452         if (!sta) {
453                 rcu_read_unlock();
454 #ifdef CONFIG_MAC80211_HT_DEBUG
455                 printk(KERN_DEBUG "Could not find station: %pM\n", ra);
456 #endif
457                 return;
458         }
459
460         state = &sta->ampdu_mlme.tid_state_tx[tid];
461         spin_lock_bh(&sta->lock);
462
463         if (WARN_ON(!(*state & HT_ADDBA_REQUESTED_MSK))) {
464 #ifdef CONFIG_MAC80211_HT_DEBUG
465                 printk(KERN_DEBUG "addBA was not requested yet, state is %d\n",
466                                 *state);
467 #endif
468                 spin_unlock_bh(&sta->lock);
469                 rcu_read_unlock();
470                 return;
471         }
472
473         if (WARN_ON(*state & HT_ADDBA_DRV_READY_MSK))
474                 goto out;
475
476         *state |= HT_ADDBA_DRV_READY_MSK;
477
478         if (*state == HT_AGG_STATE_OPERATIONAL)
479                 ieee80211_agg_tx_operational(local, sta, tid);
480
481  out:
482         spin_unlock_bh(&sta->lock);
483         rcu_read_unlock();
484 }
485 EXPORT_SYMBOL(ieee80211_start_tx_ba_cb);
486
487 void ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
488                                       const u8 *ra, u16 tid)
489 {
490         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
491         struct ieee80211_local *local = sdata->local;
492         struct ieee80211_ra_tid *ra_tid;
493         struct sk_buff *skb = dev_alloc_skb(0);
494
495         if (unlikely(!skb)) {
496 #ifdef CONFIG_MAC80211_HT_DEBUG
497                 if (net_ratelimit())
498                         printk(KERN_WARNING "%s: Not enough memory, "
499                                "dropping start BA session", sdata->name);
500 #endif
501                 return;
502         }
503         ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
504         memcpy(&ra_tid->ra, ra, ETH_ALEN);
505         ra_tid->tid = tid;
506         ra_tid->vif = vif;
507
508         skb->pkt_type = IEEE80211_ADDBA_MSG;
509         skb_queue_tail(&local->skb_queue, skb);
510         tasklet_schedule(&local->tasklet);
511 }
512 EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);
513
514 int __ieee80211_stop_tx_ba_session(struct sta_info *sta, u16 tid,
515                                    enum ieee80211_back_parties initiator)
516 {
517         u8 *state;
518         int ret;
519
520         /* check if the TID is in aggregation */
521         state = &sta->ampdu_mlme.tid_state_tx[tid];
522         spin_lock_bh(&sta->lock);
523
524         if (*state != HT_AGG_STATE_OPERATIONAL) {
525                 ret = -ENOENT;
526                 goto unlock;
527         }
528
529         ret = ___ieee80211_stop_tx_ba_session(sta, tid, initiator);
530
531  unlock:
532         spin_unlock_bh(&sta->lock);
533         return ret;
534 }
535
536 int ieee80211_stop_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid,
537                                  enum ieee80211_back_parties initiator)
538 {
539         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
540         struct ieee80211_sub_if_data *sdata = sta->sdata;
541         struct ieee80211_local *local = sdata->local;
542
543         if (!local->ops->ampdu_action)
544                 return -EINVAL;
545
546         if (tid >= STA_TID_NUM)
547                 return -EINVAL;
548
549         return __ieee80211_stop_tx_ba_session(sta, tid, initiator);
550 }
551 EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);
552
553 void ieee80211_stop_tx_ba_cb(struct ieee80211_vif *vif, u8 *ra, u8 tid)
554 {
555         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
556         struct ieee80211_local *local = sdata->local;
557         struct sta_info *sta;
558         u8 *state;
559
560         if (tid >= STA_TID_NUM) {
561 #ifdef CONFIG_MAC80211_HT_DEBUG
562                 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
563                                 tid, STA_TID_NUM);
564 #endif
565                 return;
566         }
567
568 #ifdef CONFIG_MAC80211_HT_DEBUG
569         printk(KERN_DEBUG "Stopping Tx BA session for %pM tid %d\n",
570                ra, tid);
571 #endif /* CONFIG_MAC80211_HT_DEBUG */
572
573         rcu_read_lock();
574         sta = sta_info_get(sdata, ra);
575         if (!sta) {
576 #ifdef CONFIG_MAC80211_HT_DEBUG
577                 printk(KERN_DEBUG "Could not find station: %pM\n", ra);
578 #endif
579                 rcu_read_unlock();
580                 return;
581         }
582         state = &sta->ampdu_mlme.tid_state_tx[tid];
583
584         /* NOTE: no need to use sta->lock in this state check, as
585          * ieee80211_stop_tx_ba_session will let only one stop call to
586          * pass through per sta/tid
587          */
588         if ((*state & HT_AGG_STATE_REQ_STOP_BA_MSK) == 0) {
589 #ifdef CONFIG_MAC80211_HT_DEBUG
590                 printk(KERN_DEBUG "unexpected callback to A-MPDU stop\n");
591 #endif
592                 rcu_read_unlock();
593                 return;
594         }
595
596         if (*state & HT_AGG_STATE_INITIATOR_MSK)
597                 ieee80211_send_delba(sta->sdata, ra, tid,
598                         WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);
599
600         spin_lock_bh(&sta->lock);
601         spin_lock(&local->ampdu_lock);
602
603         ieee80211_agg_splice_packets(local, sta, tid);
604
605         *state = HT_AGG_STATE_IDLE;
606         /* from now on packets are no longer put onto sta->pending */
607         kfree(sta->ampdu_mlme.tid_tx[tid]);
608         sta->ampdu_mlme.tid_tx[tid] = NULL;
609
610         ieee80211_agg_splice_finish(local, sta, tid);
611
612         spin_unlock(&local->ampdu_lock);
613         spin_unlock_bh(&sta->lock);
614
615         rcu_read_unlock();
616 }
617 EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb);
618
619 void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
620                                      const u8 *ra, u16 tid)
621 {
622         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
623         struct ieee80211_local *local = sdata->local;
624         struct ieee80211_ra_tid *ra_tid;
625         struct sk_buff *skb = dev_alloc_skb(0);
626
627         if (unlikely(!skb)) {
628 #ifdef CONFIG_MAC80211_HT_DEBUG
629                 if (net_ratelimit())
630                         printk(KERN_WARNING "%s: Not enough memory, "
631                                "dropping stop BA session", sdata->name);
632 #endif
633                 return;
634         }
635         ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
636         memcpy(&ra_tid->ra, ra, ETH_ALEN);
637         ra_tid->tid = tid;
638         ra_tid->vif = vif;
639
640         skb->pkt_type = IEEE80211_DELBA_MSG;
641         skb_queue_tail(&local->skb_queue, skb);
642         tasklet_schedule(&local->tasklet);
643 }
644 EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);
645
646
647 void ieee80211_process_addba_resp(struct ieee80211_local *local,
648                                   struct sta_info *sta,
649                                   struct ieee80211_mgmt *mgmt,
650                                   size_t len)
651 {
652         u16 capab, tid;
653         u8 *state;
654
655         capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
656         tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
657
658         state = &sta->ampdu_mlme.tid_state_tx[tid];
659
660         spin_lock_bh(&sta->lock);
661
662         if (!(*state & HT_ADDBA_REQUESTED_MSK))
663                 goto out;
664
665         if (mgmt->u.action.u.addba_resp.dialog_token !=
666                 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
667 #ifdef CONFIG_MAC80211_HT_DEBUG
668                 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
669 #endif /* CONFIG_MAC80211_HT_DEBUG */
670                 goto out;
671         }
672
673         del_timer(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
674
675 #ifdef CONFIG_MAC80211_HT_DEBUG
676         printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
677 #endif /* CONFIG_MAC80211_HT_DEBUG */
678
679         if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
680                         == WLAN_STATUS_SUCCESS) {
681                 u8 curstate = *state;
682
683                 *state |= HT_ADDBA_RECEIVED_MSK;
684
685                 if (*state != curstate && *state == HT_AGG_STATE_OPERATIONAL)
686                         ieee80211_agg_tx_operational(local, sta, tid);
687
688                 sta->ampdu_mlme.addba_req_num[tid] = 0;
689         } else {
690                 ___ieee80211_stop_tx_ba_session(sta, tid, WLAN_BACK_INITIATOR);
691         }
692
693  out:
694         spin_unlock_bh(&sta->lock);
695 }