agg-tx.c 19.8 KB
Newer Older
J
Johannes Berg 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * HT handling
 *
 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
 * Copyright 2002-2005, Instant802 Networks, Inc.
 * Copyright 2005-2006, Devicescape Software, Inc.
 * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
 * Copyright 2007-2009, Intel Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/ieee80211.h>
17
#include <linux/slab.h>
J
Johannes Berg 已提交
18 19
#include <net/mac80211.h>
#include "ieee80211_i.h"
20
#include "driver-ops.h"
J
Johannes Berg 已提交
21 22
#include "wme.h"

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/**
 * DOC: TX aggregation
 *
 * Aggregation on the TX side requires setting the hardware flag
 * %IEEE80211_HW_AMPDU_AGGREGATION as well as, if present, the @ampdu_queues
 * hardware parameter to the number of hardware AMPDU queues. If there are no
 * hardware queues then the driver will (currently) have to do all frame
 * buffering.
 *
 * When TX aggregation is started by some subsystem (usually the rate control
 * algorithm would be appropriate) by calling the
 * ieee80211_start_tx_ba_session() function, the driver will be notified via
 * its @ampdu_action function, with the %IEEE80211_AMPDU_TX_START action.
 *
 * In response to that, the driver is later required to call the
 * ieee80211_start_tx_ba_cb() (or ieee80211_start_tx_ba_cb_irqsafe())
 * function, which will start the aggregation session.
 *
 * Similarly, when the aggregation session is stopped by
 * ieee80211_stop_tx_ba_session(), the driver's @ampdu_action function will
 * be called with the action %IEEE80211_AMPDU_TX_STOP. In this case, the
 * call must not fail, and the driver must later call ieee80211_stop_tx_ba_cb()
 * (or ieee80211_stop_tx_ba_cb_irqsafe()).
 */

J
Johannes Berg 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61
static void ieee80211_send_addba_request(struct ieee80211_sub_if_data *sdata,
					 const u8 *da, u16 tid,
					 u8 dialog_token, u16 start_seq_num,
					 u16 agg_size, u16 timeout)
{
	struct ieee80211_local *local = sdata->local;
	struct sk_buff *skb;
	struct ieee80211_mgmt *mgmt;
	u16 capab;

	skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);

	if (!skb) {
		printk(KERN_ERR "%s: failed to allocate buffer "
62
				"for addba request frame\n", sdata->name);
J
Johannes Berg 已提交
63 64 65 66 67 68
		return;
	}
	skb_reserve(skb, local->hw.extra_tx_headroom);
	mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
	memset(mgmt, 0, 24);
	memcpy(mgmt->da, da, ETH_ALEN);
69
	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
70 71
	if (sdata->vif.type == NL80211_IFTYPE_AP ||
	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
72
		memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
73 74
	else if (sdata->vif.type == NL80211_IFTYPE_STATION)
		memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
J
Johannes Berg 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
					  IEEE80211_STYPE_ACTION);

	skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));

	mgmt->u.action.category = WLAN_CATEGORY_BACK;
	mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;

	mgmt->u.action.u.addba_req.dialog_token = dialog_token;
	capab = (u16)(1 << 1);		/* bit 1 aggregation policy */
	capab |= (u16)(tid << 2); 	/* bit 5:2 TID number */
	capab |= (u16)(agg_size << 6);	/* bit 15:6 max size of aggergation */

	mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);

	mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
	mgmt->u.action.u.addba_req.start_seq_num =
					cpu_to_le16(start_seq_num << 4);

95
	ieee80211_tx_skb(sdata, skb);
J
Johannes Berg 已提交
96 97 98 99 100 101 102 103 104 105 106 107
}

void ieee80211_send_bar(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid, u16 ssn)
{
	struct ieee80211_local *local = sdata->local;
	struct sk_buff *skb;
	struct ieee80211_bar *bar;
	u16 bar_control = 0;

	skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
	if (!skb) {
		printk(KERN_ERR "%s: failed to allocate buffer for "
108
			"bar frame\n", sdata->name);
J
Johannes Berg 已提交
109 110 111 112 113 114 115 116
		return;
	}
	skb_reserve(skb, local->hw.extra_tx_headroom);
	bar = (struct ieee80211_bar *)skb_put(skb, sizeof(*bar));
	memset(bar, 0, sizeof(*bar));
	bar->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
					 IEEE80211_STYPE_BACK_REQ);
	memcpy(bar->ra, ra, ETH_ALEN);
117
	memcpy(bar->ta, sdata->vif.addr, ETH_ALEN);
J
Johannes Berg 已提交
118 119 120 121 122 123
	bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
	bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
	bar_control |= (u16)(tid << 12);
	bar->control = cpu_to_le16(bar_control);
	bar->start_seq_num = cpu_to_le16(ssn);

124 125
	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
	ieee80211_tx_skb(sdata, skb);
J
Johannes Berg 已提交
126 127
}

128 129 130 131 132 133 134 135 136 137 138
static void kfree_tid_tx(struct rcu_head *rcu_head)
{
	struct tid_ampdu_tx *tid_tx =
	    container_of(rcu_head, struct tid_ampdu_tx, rcu_head);

	kfree(tid_tx);
}

static int ___ieee80211_stop_tx_ba_session(
		struct sta_info *sta, u16 tid,
		enum ieee80211_back_parties initiator)
139
{
140
	struct ieee80211_local *local = sta->local;
141
	struct tid_ampdu_tx *tid_tx = sta->ampdu_mlme.tid_tx[tid];
142
	int ret;
143 144 145 146 147

	lockdep_assert_held(&sta->lock);

	if (WARN_ON(!tid_tx))
		return -ENOENT;
148

149 150 151 152 153
#ifdef CONFIG_MAC80211_HT_DEBUG
	printk(KERN_DEBUG "Tx BA session stop requested for %pM tid %u\n",
	       sta->sta.addr, tid);
#endif /* CONFIG_MAC80211_HT_DEBUG */

154
	set_bit(HT_AGG_STATE_STOPPING, &tid_tx->state);
155

156 157 158 159 160 161
	/*
	 * After this packets are no longer handed right through
	 * to the driver but are put onto tid_tx->pending instead,
	 * with locking to ensure proper access.
	 */
	clear_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state);
162

163
	tid_tx->stop_initiator = initiator;
164

J
Johannes Berg 已提交
165
	ret = drv_ampdu_action(local, sta->sdata,
166
			       IEEE80211_AMPDU_TX_STOP,
167
			       &sta->sta, tid, NULL);
168 169 170

	/* HW shall not deny going back to legacy */
	if (WARN_ON(ret)) {
171 172 173 174
		/*
		 * We may have pending packets get stuck in this case...
		 * Not bothering with a workaround for now.
		 */
175 176 177 178 179
	}

	return ret;
}

J
Johannes Berg 已提交
180 181 182 183 184 185 186 187 188 189 190 191
/*
 * After sending add Block Ack request we activated a timer until
 * add Block Ack response will arrive from the recipient.
 * If this timer expires sta_addba_resp_timer_expired will be executed.
 */
static void sta_addba_resp_timer_expired(unsigned long data)
{
	/* not an elegant detour, but there is no choice as the timer passes
	 * only one argument, and both sta_info and TID are needed, so init
	 * flow in sta_info_create gives the TID as data, while the timer_to_id
	 * array gives the sta through container_of */
	u16 tid = *(u8 *)data;
192
	struct sta_info *sta = container_of((void *)data,
J
Johannes Berg 已提交
193
		struct sta_info, timer_to_tid[tid]);
194
	struct tid_ampdu_tx *tid_tx;
195

J
Johannes Berg 已提交
196 197
	/* check if the TID waits for addBA response */
	spin_lock_bh(&sta->lock);
198 199 200
	tid_tx = sta->ampdu_mlme.tid_tx[tid];
	if (!tid_tx ||
	    test_bit(HT_AGG_STATE_RESPONSE_RECEIVED, &tid_tx->state)) {
J
Johannes Berg 已提交
201 202 203
		spin_unlock_bh(&sta->lock);
#ifdef CONFIG_MAC80211_HT_DEBUG
		printk(KERN_DEBUG "timer expired on tid %d but we are not "
J
Johannes Berg 已提交
204
				"(or no longer) expecting addBA response there\n",
205
			tid);
J
Johannes Berg 已提交
206
#endif
207
		return;
J
Johannes Berg 已提交
208 209 210 211 212 213
	}

#ifdef CONFIG_MAC80211_HT_DEBUG
	printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
#endif

214
	___ieee80211_stop_tx_ba_session(sta, tid, WLAN_BACK_INITIATOR);
J
Johannes Berg 已提交
215 216 217
	spin_unlock_bh(&sta->lock);
}

218 219 220 221 222
static inline int ieee80211_ac_from_tid(int tid)
{
	return ieee802_1d_to_ac[tid & 7];
}

223
int ieee80211_start_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid)
J
Johannes Berg 已提交
224
{
225 226 227
	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
	struct ieee80211_sub_if_data *sdata = sta->sdata;
	struct ieee80211_local *local = sdata->local;
228
	struct tid_ampdu_tx *tid_tx;
229
	int ret = 0;
230
	u16 start_seq_num;
J
Johannes Berg 已提交
231

J
Johannes Berg 已提交
232 233
	trace_api_start_tx_ba_session(pubsta, tid);

234 235 236
	if (WARN_ON(!local->ops->ampdu_action))
		return -EINVAL;

237 238
	if ((tid >= STA_TID_NUM) ||
	    !(local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION))
J
Johannes Berg 已提交
239 240 241 242
		return -EINVAL;

#ifdef CONFIG_MAC80211_HT_DEBUG
	printk(KERN_DEBUG "Open BA session requested for %pM tid %u\n",
243
	       pubsta->addr, tid);
J
Johannes Berg 已提交
244 245
#endif /* CONFIG_MAC80211_HT_DEBUG */

246 247 248 249 250 251
	/*
	 * The aggregation code is not prepared to handle
	 * anything but STA/AP due to the BSSID handling.
	 * IBSS could work in the code but isn't supported
	 * by drivers or the standard.
	 */
252 253 254 255
	if (sdata->vif.type != NL80211_IFTYPE_STATION &&
	    sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
	    sdata->vif.type != NL80211_IFTYPE_AP)
		return -EINVAL;
256

257
	if (test_sta_flags(sta, WLAN_STA_BLOCK_BA)) {
258
#ifdef CONFIG_MAC80211_HT_DEBUG
259
		printk(KERN_DEBUG "BA sessions blocked. "
260 261
		       "Denying BA session request\n");
#endif
262
		return -EINVAL;
263 264
	}

J
Johannes Berg 已提交
265
	spin_lock_bh(&sta->lock);
266
	spin_lock(&local->ampdu_lock);
J
Johannes Berg 已提交
267 268 269 270 271 272 273

	/* we have tried too many times, receiver does not want A-MPDU */
	if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_MAX_RETRIES) {
		ret = -EBUSY;
		goto err_unlock_sta;
	}

274
	tid_tx = sta->ampdu_mlme.tid_tx[tid];
J
Johannes Berg 已提交
275
	/* check if the TID is not in aggregation flow already */
276
	if (tid_tx) {
J
Johannes Berg 已提交
277 278 279 280 281 282 283 284
#ifdef CONFIG_MAC80211_HT_DEBUG
		printk(KERN_DEBUG "BA request denied - session is not "
				 "idle on tid %u\n", tid);
#endif /* CONFIG_MAC80211_HT_DEBUG */
		ret = -EAGAIN;
		goto err_unlock_sta;
	}

285 286 287 288 289 290 291 292 293 294 295
	/*
	 * While we're asking the driver about the aggregation,
	 * stop the AC queue so that we don't have to worry
	 * about frames that came in while we were doing that,
	 * which would require us to put them to the AC pending
	 * afterwards which just makes the code more complex.
	 */
	ieee80211_stop_queue_by_reason(
		&local->hw, ieee80211_ac_from_tid(tid),
		IEEE80211_QUEUE_STOP_REASON_AGGREGATION);

J
Johannes Berg 已提交
296
	/* prepare A-MPDU MLME for Tx aggregation */
297 298
	tid_tx = kzalloc(sizeof(struct tid_ampdu_tx), GFP_ATOMIC);
	if (!tid_tx) {
J
Johannes Berg 已提交
299 300 301 302 303 304
#ifdef CONFIG_MAC80211_HT_DEBUG
		if (net_ratelimit())
			printk(KERN_ERR "allocate tx mlme to tid %d failed\n",
					tid);
#endif
		ret = -ENOMEM;
305
		goto err_wake_queue;
J
Johannes Berg 已提交
306
	}
307

308
	skb_queue_head_init(&tid_tx->pending);
309

J
Johannes Berg 已提交
310
	/* Tx timer */
311 312 313
	tid_tx->addba_resp_timer.function = sta_addba_resp_timer_expired;
	tid_tx->addba_resp_timer.data = (unsigned long)&sta->timer_to_tid[tid];
	init_timer(&tid_tx->addba_resp_timer);
J
Johannes Berg 已提交
314

315
	start_seq_num = sta->tid_seq[tid] >> 4;
J
Johannes Berg 已提交
316

J
Johannes Berg 已提交
317
	ret = drv_ampdu_action(local, sdata, IEEE80211_AMPDU_TX_START,
318
			       pubsta, tid, &start_seq_num);
J
Johannes Berg 已提交
319 320 321 322 323
	if (ret) {
#ifdef CONFIG_MAC80211_HT_DEBUG
		printk(KERN_DEBUG "BA request denied - HW unavailable for"
					" tid %d\n", tid);
#endif /* CONFIG_MAC80211_HT_DEBUG */
324
		goto err_free;
J
Johannes Berg 已提交
325 326
	}

327 328
	rcu_assign_pointer(sta->ampdu_mlme.tid_tx[tid], tid_tx);

329 330 331 332 333 334
	/* Driver vetoed or OKed, but we can take packets again now */
	ieee80211_wake_queue_by_reason(
		&local->hw, ieee80211_ac_from_tid(tid),
		IEEE80211_QUEUE_STOP_REASON_AGGREGATION);

	spin_unlock(&local->ampdu_lock);
J
Johannes Berg 已提交
335

336 337 338 339 340 341 342
	/* activate the timer for the recipient's addBA response */
	tid_tx->addba_resp_timer.expires = jiffies + ADDBA_RESP_INTERVAL;
	add_timer(&tid_tx->addba_resp_timer);
#ifdef CONFIG_MAC80211_HT_DEBUG
	printk(KERN_DEBUG "activated addBA response timer on tid %d\n", tid);
#endif

343
	/* prepare tid data */
J
Johannes Berg 已提交
344
	sta->ampdu_mlme.dialog_token_allocator++;
345 346 347 348
	tid_tx->dialog_token = sta->ampdu_mlme.dialog_token_allocator;
	tid_tx->ssn = start_seq_num;

	sta->ampdu_mlme.addba_req_num[tid]++;
J
Johannes Berg 已提交
349

350 351 352
	spin_unlock_bh(&sta->lock);

	/* send AddBA request */
353
	ieee80211_send_addba_request(sdata, pubsta->addr, tid,
354
			 tid_tx->dialog_token, tid_tx->ssn,
J
Johannes Berg 已提交
355
			 0x40, 5000);
356
	return 0;
J
Johannes Berg 已提交
357

358
 err_free:
359
	kfree(tid_tx);
360
 err_wake_queue:
361 362 363
	ieee80211_wake_queue_by_reason(
		&local->hw, ieee80211_ac_from_tid(tid),
		IEEE80211_QUEUE_STOP_REASON_AGGREGATION);
364
 err_unlock_sta:
365
	spin_unlock(&local->ampdu_lock);
J
Johannes Berg 已提交
366 367 368 369 370
	spin_unlock_bh(&sta->lock);
	return ret;
}
EXPORT_SYMBOL(ieee80211_start_tx_ba_session);

371 372 373 374 375 376
/*
 * splice packets from the STA's pending to the local pending,
 * requires a call to ieee80211_agg_splice_finish and holding
 * local->ampdu_lock across both calls.
 */
static void ieee80211_agg_splice_packets(struct ieee80211_local *local,
377 378
					 struct tid_ampdu_tx *tid_tx,
					 u16 tid)
379 380 381 382 383 384 385 386
{
	unsigned long flags;
	u16 queue = ieee80211_ac_from_tid(tid);

	ieee80211_stop_queue_by_reason(
		&local->hw, queue,
		IEEE80211_QUEUE_STOP_REASON_AGGREGATION);

387 388
	if (WARN(!tid_tx, "TID %d gone but expected when splicing aggregates"
			  " from the pending queue\n", tid))
389 390
		return;

391
	if (!skb_queue_empty(&tid_tx->pending)) {
392 393
		spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
		/* copy over remaining packets */
394 395
		skb_queue_splice_tail_init(&tid_tx->pending,
					   &local->pending[queue]);
396 397 398 399
		spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
	}
}

400
static void ieee80211_agg_splice_finish(struct ieee80211_local *local, u16 tid)
401 402
{
	ieee80211_wake_queue_by_reason(
403
		&local->hw, ieee80211_ac_from_tid(tid),
404 405 406 407
		IEEE80211_QUEUE_STOP_REASON_AGGREGATION);
}

/* caller must hold sta->lock */
408 409 410
static void ieee80211_agg_tx_operational(struct ieee80211_local *local,
					 struct sta_info *sta, u16 tid)
{
411 412
	lockdep_assert_held(&sta->lock);

413
#ifdef CONFIG_MAC80211_HT_DEBUG
414
	printk(KERN_DEBUG "Aggregation is on for tid %d\n", tid);
415 416
#endif

417
	spin_lock(&local->ampdu_lock);
418
	ieee80211_agg_splice_packets(local, sta->ampdu_mlme.tid_tx[tid], tid);
419
	/*
420 421 422
	 * Now mark as operational. This will be visible
	 * in the TX path, and lets it go lock-free in
	 * the common case.
423
	 */
424 425
	set_bit(HT_AGG_STATE_OPERATIONAL, &sta->ampdu_mlme.tid_tx[tid]->state);
	ieee80211_agg_splice_finish(local, tid);
426
	spin_unlock(&local->ampdu_lock);
427

J
Johannes Berg 已提交
428
	drv_ampdu_action(local, sta->sdata,
429
			 IEEE80211_AMPDU_TX_OPERATIONAL,
430
			 &sta->sta, tid, NULL);
431 432
}

433
void ieee80211_start_tx_ba_cb(struct ieee80211_vif *vif, u8 *ra, u16 tid)
J
Johannes Berg 已提交
434
{
435 436
	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
	struct ieee80211_local *local = sdata->local;
J
Johannes Berg 已提交
437
	struct sta_info *sta;
438
	struct tid_ampdu_tx *tid_tx;
J
Johannes Berg 已提交
439

J
Johannes Berg 已提交
440 441
	trace_api_start_tx_ba_cb(sdata, ra, tid);

J
Johannes Berg 已提交
442 443 444 445 446 447 448 449 450
	if (tid >= STA_TID_NUM) {
#ifdef CONFIG_MAC80211_HT_DEBUG
		printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
				tid, STA_TID_NUM);
#endif
		return;
	}

	rcu_read_lock();
451
	sta = sta_info_get(sdata, ra);
J
Johannes Berg 已提交
452 453 454 455 456 457 458 459 460
	if (!sta) {
		rcu_read_unlock();
#ifdef CONFIG_MAC80211_HT_DEBUG
		printk(KERN_DEBUG "Could not find station: %pM\n", ra);
#endif
		return;
	}

	spin_lock_bh(&sta->lock);
461
	tid_tx = sta->ampdu_mlme.tid_tx[tid];
J
Johannes Berg 已提交
462

463
	if (WARN_ON(!tid_tx)) {
J
Johannes Berg 已提交
464
#ifdef CONFIG_MAC80211_HT_DEBUG
465
		printk(KERN_DEBUG "addBA was not requested!\n");
J
Johannes Berg 已提交
466 467 468 469 470 471
#endif
		spin_unlock_bh(&sta->lock);
		rcu_read_unlock();
		return;
	}

472
	if (WARN_ON(test_and_set_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state)))
473
		goto out;
J
Johannes Berg 已提交
474

475
	if (test_bit(HT_AGG_STATE_RESPONSE_RECEIVED, &tid_tx->state))
476
		ieee80211_agg_tx_operational(local, sta, tid);
477 478

 out:
J
Johannes Berg 已提交
479 480 481 482
	spin_unlock_bh(&sta->lock);
	rcu_read_unlock();
}

483
void ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
484 485
				      const u8 *ra, u16 tid)
{
486 487
	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
	struct ieee80211_local *local = sdata->local;
488 489 490 491 492 493 494
	struct ieee80211_ra_tid *ra_tid;
	struct sk_buff *skb = dev_alloc_skb(0);

	if (unlikely(!skb)) {
#ifdef CONFIG_MAC80211_HT_DEBUG
		if (net_ratelimit())
			printk(KERN_WARNING "%s: Not enough memory, "
495
			       "dropping start BA session", sdata->name);
496 497 498 499 500 501 502
#endif
		return;
	}
	ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
	memcpy(&ra_tid->ra, ra, ETH_ALEN);
	ra_tid->tid = tid;

503 504 505
	skb->pkt_type = IEEE80211_SDATA_QUEUE_AGG_START;
	skb_queue_tail(&sdata->skb_queue, skb);
	ieee80211_queue_work(&local->hw, &sdata->work);
506 507 508
}
EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);

509 510 511
int __ieee80211_stop_tx_ba_session(struct sta_info *sta, u16 tid,
				   enum ieee80211_back_parties initiator)
{
512
	struct tid_ampdu_tx *tid_tx;
513 514 515
	int ret;

	spin_lock_bh(&sta->lock);
516
	tid_tx = sta->ampdu_mlme.tid_tx[tid];
517

518 519
	/* check if the TID is in aggregation */
	if (!tid_tx || !test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
520 521 522 523 524 525 526 527 528 529
		ret = -ENOENT;
		goto unlock;
	}

	ret = ___ieee80211_stop_tx_ba_session(sta, tid, initiator);

 unlock:
	spin_unlock_bh(&sta->lock);
	return ret;
}
J
Johannes Berg 已提交
530

531
int ieee80211_stop_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid)
J
Johannes Berg 已提交
532
{
533 534 535
	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
	struct ieee80211_sub_if_data *sdata = sta->sdata;
	struct ieee80211_local *local = sdata->local;
J
Johannes Berg 已提交
536

537
	trace_api_stop_tx_ba_session(pubsta, tid);
J
Johannes Berg 已提交
538

J
Johannes Berg 已提交
539
	if (!local->ops->ampdu_action)
540 541
		return -EINVAL;

J
Johannes Berg 已提交
542 543 544
	if (tid >= STA_TID_NUM)
		return -EINVAL;

545
	return __ieee80211_stop_tx_ba_session(sta, tid, WLAN_BACK_INITIATOR);
J
Johannes Berg 已提交
546 547 548
}
EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);

549
void ieee80211_stop_tx_ba_cb(struct ieee80211_vif *vif, u8 *ra, u8 tid)
J
Johannes Berg 已提交
550
{
551 552
	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
	struct ieee80211_local *local = sdata->local;
J
Johannes Berg 已提交
553
	struct sta_info *sta;
554
	struct tid_ampdu_tx *tid_tx;
J
Johannes Berg 已提交
555

J
Johannes Berg 已提交
556 557
	trace_api_stop_tx_ba_cb(sdata, ra, tid);

J
Johannes Berg 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570 571
	if (tid >= STA_TID_NUM) {
#ifdef CONFIG_MAC80211_HT_DEBUG
		printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
				tid, STA_TID_NUM);
#endif
		return;
	}

#ifdef CONFIG_MAC80211_HT_DEBUG
	printk(KERN_DEBUG "Stopping Tx BA session for %pM tid %d\n",
	       ra, tid);
#endif /* CONFIG_MAC80211_HT_DEBUG */

	rcu_read_lock();
572
	sta = sta_info_get(sdata, ra);
J
Johannes Berg 已提交
573 574 575 576 577 578 579 580
	if (!sta) {
#ifdef CONFIG_MAC80211_HT_DEBUG
		printk(KERN_DEBUG "Could not find station: %pM\n", ra);
#endif
		rcu_read_unlock();
		return;
	}

581 582 583 584
	spin_lock_bh(&sta->lock);
	tid_tx = sta->ampdu_mlme.tid_tx[tid];

	if (!tid_tx || !test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
J
Johannes Berg 已提交
585 586 587
#ifdef CONFIG_MAC80211_HT_DEBUG
		printk(KERN_DEBUG "unexpected callback to A-MPDU stop\n");
#endif
588
		spin_unlock_bh(&sta->lock);
J
Johannes Berg 已提交
589 590 591 592
		rcu_read_unlock();
		return;
	}

593
	if (tid_tx->stop_initiator == WLAN_BACK_INITIATOR)
J
Johannes Berg 已提交
594 595 596
		ieee80211_send_delba(sta->sdata, ra, tid,
			WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);

597 598 599 600 601 602 603 604 605
	/*
	 * When we get here, the TX path will not be lockless any more wrt.
	 * aggregation, since the OPERATIONAL bit has long been cleared.
	 * Thus it will block on getting the lock, if it occurs. So if we
	 * stop the queue now, we will not get any more packets, and any
	 * that might be being processed will wait for us here, thereby
	 * guaranteeing that no packets go to the tid_tx pending queue any
	 * more.
	 */
J
Johannes Berg 已提交
606

607 608
	spin_lock(&local->ampdu_lock);
	ieee80211_agg_splice_packets(local, tid_tx, tid);
609

610 611
	/* future packets must not find the tid_tx struct any more */
	rcu_assign_pointer(sta->ampdu_mlme.tid_tx[tid], NULL);
612

613
	ieee80211_agg_splice_finish(local, tid);
614

615
	call_rcu(&tid_tx->rcu_head, kfree_tid_tx);
616
	spin_unlock(&local->ampdu_lock);
J
Johannes Berg 已提交
617

618
	spin_unlock_bh(&sta->lock);
J
Johannes Berg 已提交
619 620 621
	rcu_read_unlock();
}

622
void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
J
Johannes Berg 已提交
623 624
				     const u8 *ra, u16 tid)
{
625 626
	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
	struct ieee80211_local *local = sdata->local;
J
Johannes Berg 已提交
627 628 629 630 631 632 633
	struct ieee80211_ra_tid *ra_tid;
	struct sk_buff *skb = dev_alloc_skb(0);

	if (unlikely(!skb)) {
#ifdef CONFIG_MAC80211_HT_DEBUG
		if (net_ratelimit())
			printk(KERN_WARNING "%s: Not enough memory, "
634
			       "dropping stop BA session", sdata->name);
J
Johannes Berg 已提交
635 636 637 638 639 640 641
#endif
		return;
	}
	ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
	memcpy(&ra_tid->ra, ra, ETH_ALEN);
	ra_tid->tid = tid;

642 643 644
	skb->pkt_type = IEEE80211_SDATA_QUEUE_AGG_STOP;
	skb_queue_tail(&sdata->skb_queue, skb);
	ieee80211_queue_work(&local->hw, &sdata->work);
J
Johannes Berg 已提交
645 646 647
}
EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);

648

J
Johannes Berg 已提交
649 650 651 652 653
void ieee80211_process_addba_resp(struct ieee80211_local *local,
				  struct sta_info *sta,
				  struct ieee80211_mgmt *mgmt,
				  size_t len)
{
654
	struct tid_ampdu_tx *tid_tx;
655
	u16 capab, tid;
J
Johannes Berg 已提交
656 657 658 659 660 661

	capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
	tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;

	spin_lock_bh(&sta->lock);

662 663 664
	tid_tx = sta->ampdu_mlme.tid_tx[tid];

	if (!tid_tx)
665
		goto out;
J
Johannes Berg 已提交
666

667
	if (mgmt->u.action.u.addba_resp.dialog_token != tid_tx->dialog_token) {
J
Johannes Berg 已提交
668 669
#ifdef CONFIG_MAC80211_HT_DEBUG
		printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
670
#endif
671
		goto out;
J
Johannes Berg 已提交
672 673
	}

674
	del_timer(&tid_tx->addba_resp_timer);
675

J
Johannes Berg 已提交
676
#ifdef CONFIG_MAC80211_HT_DEBUG
677
	printk(KERN_DEBUG "switched off addBA timer for tid %d\n", tid);
678
#endif
J
Johannes Berg 已提交
679

J
Johannes Berg 已提交
680 681
	if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
			== WLAN_STATUS_SUCCESS) {
682 683 684 685 686
		if (test_and_set_bit(HT_AGG_STATE_RESPONSE_RECEIVED,
				     &tid_tx->state)) {
			/* ignore duplicate response */
			goto out;
		}
J
Johannes Berg 已提交
687

688
		if (test_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state))
689
			ieee80211_agg_tx_operational(local, sta, tid);
J
Johannes Berg 已提交
690

691
		sta->ampdu_mlme.addba_req_num[tid] = 0;
J
Johannes Berg 已提交
692
	} else {
693
		___ieee80211_stop_tx_ba_session(sta, tid, WLAN_BACK_INITIATOR);
J
Johannes Berg 已提交
694
	}
J
Johannes Berg 已提交
695 696

 out:
697
	spin_unlock_bh(&sta->lock);
J
Johannes Berg 已提交
698
}