xref: /linux/net/mac80211/agg-tx.c (revision dfecb0c5af3b07ebfa84be63a7a21bfc9e29a872)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * HT handling
4  *
5  * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
6  * Copyright 2002-2005, Instant802 Networks, Inc.
7  * Copyright 2005-2006, Devicescape Software, Inc.
8  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
9  * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
10  * Copyright 2007-2010, Intel Corporation
11  * Copyright(c) 2015-2017 Intel Deutschland GmbH
12  * Copyright (C) 2018-2026 Intel Corporation
13  */
14 
15 #include <linux/ieee80211.h>
16 #include <linux/slab.h>
17 #include <linux/export.h>
18 #include <net/mac80211.h>
19 #include "ieee80211_i.h"
20 #include "driver-ops.h"
21 #include "wme.h"
22 
23 /**
24  * DOC: TX A-MPDU aggregation
25  *
26  * Aggregation on the TX side requires setting the hardware flag
27  * %IEEE80211_HW_AMPDU_AGGREGATION. The driver will then be handed
28  * packets with a flag indicating A-MPDU aggregation. The driver
29  * or device is responsible for actually aggregating the frames,
30  * as well as deciding how many and which to aggregate.
31  *
32  * When TX aggregation is started by some subsystem (usually the rate
33  * control algorithm would be appropriate) by calling the
34  * ieee80211_start_tx_ba_session() function, the driver will be
35  * notified via its @ampdu_action function, with the
36  * %IEEE80211_AMPDU_TX_START action.
37  *
38  * In response to that, the driver is later required to call the
39  * ieee80211_start_tx_ba_cb_irqsafe() function, which will really
40  * start the aggregation session after the peer has also responded.
41  * If the peer responds negatively, the session will be stopped
42  * again right away. Note that it is possible for the aggregation
43  * session to be stopped before the driver has indicated that it
44  * is done setting it up, in which case it must not indicate the
45  * setup completion.
46  *
47  * Also note that, since we also need to wait for a response from
48  * the peer, the driver is notified of the completion of the
49  * handshake by the %IEEE80211_AMPDU_TX_OPERATIONAL action to the
50  * @ampdu_action callback.
51  *
52  * Similarly, when the aggregation session is stopped by the peer
53  * or something calling ieee80211_stop_tx_ba_session(), the driver's
54  * @ampdu_action function will be called with the action
55  * %IEEE80211_AMPDU_TX_STOP. In this case, the call must not fail,
56  * and the driver must later call ieee80211_stop_tx_ba_cb_irqsafe().
57  * Note that the sta can get destroyed before the BA tear down is
58  * complete.
59  */
60 
61 static void ieee80211_send_addba_request(struct sta_info *sta, u16 tid,
62 					 u8 dialog_token, u16 start_seq_num,
63 					 u16 agg_size, u16 timeout, bool ndp)
64 {
65 	struct ieee80211_sub_if_data *sdata = sta->sdata;
66 	struct ieee80211_local *local = sdata->local;
67 	struct sk_buff *skb;
68 	struct ieee80211_mgmt *mgmt;
69 	u16 capab;
70 
71 	skb = dev_alloc_skb(IEEE80211_MIN_ACTION_SIZE(addba_req) +
72 			    2 + sizeof(struct ieee80211_addba_ext_ie) +
73 			    local->hw.extra_tx_headroom);
74 	if (!skb)
75 		return;
76 
77 	skb_reserve(skb, local->hw.extra_tx_headroom);
78 	mgmt = ieee80211_mgmt_ba(skb, sta->sta.addr, sdata);
79 
80 	skb_put(skb, 2 + sizeof(mgmt->u.action.addba_req));
81 
82 	mgmt->u.action.category = WLAN_CATEGORY_BACK;
83 	mgmt->u.action.action_code = ndp ?
84 		WLAN_ACTION_NDP_ADDBA_REQ : WLAN_ACTION_ADDBA_REQ;
85 
86 	mgmt->u.action.addba_req.dialog_token = dialog_token;
87 	capab = IEEE80211_ADDBA_PARAM_AMSDU_MASK;
88 	capab |= IEEE80211_ADDBA_PARAM_POLICY_MASK;
89 	capab |= u16_encode_bits(tid, IEEE80211_ADDBA_PARAM_TID_MASK);
90 	capab |= u16_encode_bits(agg_size, IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK);
91 
92 	mgmt->u.action.addba_req.capab = cpu_to_le16(capab);
93 
94 	mgmt->u.action.addba_req.timeout = cpu_to_le16(timeout);
95 	mgmt->u.action.addba_req.start_seq_num =
96 					cpu_to_le16(start_seq_num << 4);
97 
98 	if (sta->sta.deflink.he_cap.has_he)
99 		ieee80211_add_addbaext(skb, 0, agg_size);
100 
101 	ieee80211_tx_skb_tid(sdata, skb, tid, -1);
102 }
103 
104 void ieee80211_send_bar(struct ieee80211_vif *vif, u8 *ra, u16 tid, u16 ssn)
105 {
106 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
107 	struct ieee80211_local *local = sdata->local;
108 	struct sk_buff *skb;
109 	struct ieee80211_bar *bar;
110 	u16 bar_control = 0;
111 
112 	skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
113 	if (!skb)
114 		return;
115 
116 	skb_reserve(skb, local->hw.extra_tx_headroom);
117 	bar = skb_put_zero(skb, sizeof(*bar));
118 	bar->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
119 					 IEEE80211_STYPE_BACK_REQ);
120 	memcpy(bar->ra, ra, ETH_ALEN);
121 	memcpy(bar->ta, sdata->vif.addr, ETH_ALEN);
122 	bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
123 	bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
124 	bar_control |= (u16)(tid << IEEE80211_BAR_CTRL_TID_INFO_SHIFT);
125 	bar->control = cpu_to_le16(bar_control);
126 	bar->start_seq_num = cpu_to_le16(ssn);
127 
128 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
129 					IEEE80211_TX_CTL_REQ_TX_STATUS;
130 	ieee80211_tx_skb_tid(sdata, skb, tid, -1);
131 }
132 EXPORT_SYMBOL(ieee80211_send_bar);
133 
134 void ieee80211_assign_tid_tx(struct sta_info *sta, int tid,
135 			     struct tid_ampdu_tx *tid_tx)
136 {
137 	lockdep_assert_wiphy(sta->local->hw.wiphy);
138 	lockdep_assert_held(&sta->lock);
139 	rcu_assign_pointer(sta->ampdu_mlme.tid_tx[tid], tid_tx);
140 }
141 
142 /*
143  * When multiple aggregation sessions on multiple stations
144  * are being created/destroyed simultaneously, we need to
145  * refcount the global queue stop caused by that in order
146  * to not get into a situation where one of the aggregation
147  * setup or teardown re-enables queues before the other is
148  * ready to handle that.
149  *
150  * These two functions take care of this issue by keeping
151  * a global "agg_queue_stop" refcount.
152  */
153 static void __acquires(agg_queue)
154 ieee80211_stop_queue_agg(struct ieee80211_sub_if_data *sdata, int tid)
155 {
156 	int queue = sdata->vif.hw_queue[ieee80211_ac_from_tid(tid)];
157 
158 	/* we do refcounting here, so don't use the queue reason refcounting */
159 
160 	if (atomic_inc_return(&sdata->local->agg_queue_stop[queue]) == 1)
161 		ieee80211_stop_queue_by_reason(
162 			&sdata->local->hw, queue,
163 			IEEE80211_QUEUE_STOP_REASON_AGGREGATION,
164 			false);
165 	__acquire(agg_queue);
166 }
167 
168 static void __releases(agg_queue)
169 ieee80211_wake_queue_agg(struct ieee80211_sub_if_data *sdata, int tid)
170 {
171 	int queue = sdata->vif.hw_queue[ieee80211_ac_from_tid(tid)];
172 
173 	if (atomic_dec_return(&sdata->local->agg_queue_stop[queue]) == 0)
174 		ieee80211_wake_queue_by_reason(
175 			&sdata->local->hw, queue,
176 			IEEE80211_QUEUE_STOP_REASON_AGGREGATION,
177 			false);
178 	__release(agg_queue);
179 }
180 
181 static void
182 ieee80211_agg_stop_txq(struct sta_info *sta, int tid)
183 {
184 	struct ieee80211_txq *txq = sta->sta.txq[tid];
185 	struct ieee80211_sub_if_data *sdata;
186 	struct fq *fq;
187 	struct txq_info *txqi;
188 
189 	if (!txq)
190 		return;
191 
192 	txqi = to_txq_info(txq);
193 	sdata = vif_to_sdata(txq->vif);
194 	fq = &sdata->local->fq;
195 
196 	/* Lock here to protect against further seqno updates on dequeue */
197 	spin_lock_bh(&fq->lock);
198 	set_bit(IEEE80211_TXQ_STOP, &txqi->flags);
199 	spin_unlock_bh(&fq->lock);
200 }
201 
202 static void
203 ieee80211_agg_start_txq(struct sta_info *sta, int tid, bool enable)
204 {
205 	struct ieee80211_txq *txq = sta->sta.txq[tid];
206 	struct txq_info *txqi;
207 
208 	lockdep_assert_wiphy(sta->local->hw.wiphy);
209 
210 	if (!txq)
211 		return;
212 
213 	txqi = to_txq_info(txq);
214 
215 	if (enable)
216 		set_bit(IEEE80211_TXQ_AMPDU, &txqi->flags);
217 	else
218 		clear_bit(IEEE80211_TXQ_AMPDU, &txqi->flags);
219 
220 	clear_bit(IEEE80211_TXQ_STOP, &txqi->flags);
221 	local_bh_disable();
222 	rcu_read_lock();
223 	schedule_and_wake_txq(sta->sdata->local, txqi);
224 	rcu_read_unlock();
225 	local_bh_enable();
226 }
227 
228 /*
229  * splice packets from the STA's pending to the local pending,
230  * requires a call to ieee80211_agg_splice_finish later
231  */
232 static void __acquires(agg_queue)
233 ieee80211_agg_splice_packets(struct ieee80211_sub_if_data *sdata,
234 			     struct tid_ampdu_tx *tid_tx, u16 tid)
235 {
236 	struct ieee80211_local *local = sdata->local;
237 	int queue = sdata->vif.hw_queue[ieee80211_ac_from_tid(tid)];
238 	unsigned long flags;
239 
240 	ieee80211_stop_queue_agg(sdata, tid);
241 
242 	if (WARN(!tid_tx,
243 		 "TID %d gone but expected when splicing aggregates from the pending queue\n",
244 		 tid))
245 		return;
246 
247 	if (!skb_queue_empty(&tid_tx->pending)) {
248 		spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
249 		/* copy over remaining packets */
250 		skb_queue_splice_tail_init(&tid_tx->pending,
251 					   &local->pending[queue]);
252 		spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
253 	}
254 }
255 
256 static void __releases(agg_queue)
257 ieee80211_agg_splice_finish(struct ieee80211_sub_if_data *sdata, u16 tid)
258 {
259 	ieee80211_wake_queue_agg(sdata, tid);
260 }
261 
262 static void ieee80211_remove_tid_tx(struct sta_info *sta, int tid)
263 {
264 	struct tid_ampdu_tx *tid_tx;
265 
266 	lockdep_assert_wiphy(sta->local->hw.wiphy);
267 	lockdep_assert_held(&sta->lock);
268 
269 	tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
270 
271 	/*
272 	 * When we get here, the TX path will not be lockless any more wrt.
273 	 * aggregation, since the OPERATIONAL bit has long been cleared.
274 	 * Thus it will block on getting the lock, if it occurs. So if we
275 	 * stop the queue now, we will not get any more packets, and any
276 	 * that might be being processed will wait for us here, thereby
277 	 * guaranteeing that no packets go to the tid_tx pending queue any
278 	 * more.
279 	 */
280 
281 	ieee80211_agg_splice_packets(sta->sdata, tid_tx, tid);
282 
283 	/* future packets must not find the tid_tx struct any more */
284 	ieee80211_assign_tid_tx(sta, tid, NULL);
285 
286 	ieee80211_agg_splice_finish(sta->sdata, tid);
287 
288 	kfree_rcu(tid_tx, rcu_head);
289 }
290 
291 int __ieee80211_stop_tx_ba_session(struct sta_info *sta, u16 tid,
292 				   enum ieee80211_agg_stop_reason reason)
293 {
294 	struct ieee80211_local *local = sta->local;
295 	struct tid_ampdu_tx *tid_tx;
296 	struct ieee80211_ampdu_params params = {
297 		.sta = &sta->sta,
298 		.tid = tid,
299 		.buf_size = 0,
300 		.amsdu = false,
301 		.timeout = 0,
302 		.ssn = 0,
303 	};
304 	int ret;
305 
306 	lockdep_assert_wiphy(sta->local->hw.wiphy);
307 
308 	switch (reason) {
309 	case AGG_STOP_DECLINED:
310 	case AGG_STOP_LOCAL_REQUEST:
311 	case AGG_STOP_PEER_REQUEST:
312 		params.action = IEEE80211_AMPDU_TX_STOP_CONT;
313 		break;
314 	case AGG_STOP_DESTROY_STA:
315 		params.action = IEEE80211_AMPDU_TX_STOP_FLUSH;
316 		break;
317 	default:
318 		WARN_ON_ONCE(1);
319 		return -EINVAL;
320 	}
321 
322 	spin_lock_bh(&sta->lock);
323 
324 	/* free struct pending for start, if present */
325 	tid_tx = sta->ampdu_mlme.tid_start_tx[tid];
326 	kfree(tid_tx);
327 	sta->ampdu_mlme.tid_start_tx[tid] = NULL;
328 
329 	tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
330 	if (!tid_tx) {
331 		spin_unlock_bh(&sta->lock);
332 		return -ENOENT;
333 	}
334 
335 	/*
336 	 * if we're already stopping ignore any new requests to stop
337 	 * unless we're destroying it in which case notify the driver
338 	 */
339 	if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
340 		spin_unlock_bh(&sta->lock);
341 		if (reason != AGG_STOP_DESTROY_STA)
342 			return -EALREADY;
343 		params.action = IEEE80211_AMPDU_TX_STOP_FLUSH_CONT;
344 		ret = drv_ampdu_action(local, sta->sdata, &params);
345 		WARN_ON_ONCE(ret);
346 		return 0;
347 	}
348 
349 	if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
350 		/* not even started yet! */
351 		ieee80211_assign_tid_tx(sta, tid, NULL);
352 		spin_unlock_bh(&sta->lock);
353 		kfree_rcu(tid_tx, rcu_head);
354 		return 0;
355 	}
356 
357 	set_bit(HT_AGG_STATE_STOPPING, &tid_tx->state);
358 
359 	ieee80211_agg_stop_txq(sta, tid);
360 
361 	spin_unlock_bh(&sta->lock);
362 
363 	ht_dbg(sta->sdata, "Tx BA session stop requested for %pM tid %u\n",
364 	       sta->sta.addr, tid);
365 
366 	timer_delete_sync(&tid_tx->addba_resp_timer);
367 	timer_delete_sync(&tid_tx->session_timer);
368 
369 	/*
370 	 * After this packets are no longer handed right through
371 	 * to the driver but are put onto tid_tx->pending instead,
372 	 * with locking to ensure proper access.
373 	 */
374 	clear_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state);
375 
376 	/*
377 	 * There might be a few packets being processed right now (on
378 	 * another CPU) that have already gotten past the aggregation
379 	 * check when it was still OPERATIONAL and consequently have
380 	 * IEEE80211_TX_CTL_AMPDU set. In that case, this code might
381 	 * call into the driver at the same time or even before the
382 	 * TX paths calls into it, which could confuse the driver.
383 	 *
384 	 * Wait for all currently running TX paths to finish before
385 	 * telling the driver. New packets will not go through since
386 	 * the aggregation session is no longer OPERATIONAL.
387 	 */
388 	if (!local->in_reconfig)
389 		synchronize_net();
390 
391 	tid_tx->stop_initiator = reason == AGG_STOP_PEER_REQUEST ?
392 					WLAN_BACK_RECIPIENT :
393 					WLAN_BACK_INITIATOR;
394 	tid_tx->tx_stop = reason == AGG_STOP_LOCAL_REQUEST;
395 
396 	ret = drv_ampdu_action(local, sta->sdata, &params);
397 
398 	/* HW shall not deny going back to legacy */
399 	if (WARN_ON(ret)) {
400 		/*
401 		 * We may have pending packets get stuck in this case...
402 		 * Not bothering with a workaround for now.
403 		 */
404 	}
405 
406 	/*
407 	 * In the case of AGG_STOP_DESTROY_STA, the driver won't
408 	 * necessarily call ieee80211_stop_tx_ba_cb(), so this may
409 	 * seem like we can leave the tid_tx data pending forever.
410 	 * This is true, in a way, but "forever" is only until the
411 	 * station struct is actually destroyed. In the meantime,
412 	 * leaving it around ensures that we don't transmit packets
413 	 * to the driver on this TID which might confuse it.
414 	 */
415 
416 	return 0;
417 }
418 
419 /*
420  * After sending add Block Ack request we activated a timer until
421  * add Block Ack response will arrive from the recipient.
422  * If this timer expires sta_addba_resp_timer_expired will be executed.
423  */
424 static void sta_addba_resp_timer_expired(struct timer_list *t)
425 {
426 	struct tid_ampdu_tx *tid_tx = timer_container_of(tid_tx, t,
427 							 addba_resp_timer);
428 	struct sta_info *sta = tid_tx->sta;
429 	u8 tid = tid_tx->tid;
430 
431 	/* check if the TID waits for addBA response */
432 	if (test_bit(HT_AGG_STATE_RESPONSE_RECEIVED, &tid_tx->state)) {
433 		ht_dbg(sta->sdata,
434 		       "timer expired on %pM tid %d not expecting addBA response\n",
435 		       sta->sta.addr, tid);
436 		return;
437 	}
438 
439 	ht_dbg(sta->sdata, "addBA response timer expired on %pM tid %d\n",
440 	       sta->sta.addr, tid);
441 
442 	ieee80211_stop_tx_ba_session(&sta->sta, tid);
443 }
444 
445 static void ieee80211_send_addba_with_timeout(struct sta_info *sta,
446 					      struct tid_ampdu_tx *tid_tx)
447 {
448 	struct ieee80211_sub_if_data *sdata = sta->sdata;
449 	struct ieee80211_local *local = sta->local;
450 	u8 tid = tid_tx->tid;
451 	u16 buf_size;
452 
453 	if (WARN_ON_ONCE(test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state) ||
454 			 test_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state)))
455 		return;
456 
457 	lockdep_assert_wiphy(sta->local->hw.wiphy);
458 
459 	/* activate the timer for the recipient's addBA response */
460 	mod_timer(&tid_tx->addba_resp_timer, jiffies + ADDBA_RESP_INTERVAL);
461 	ht_dbg(sdata, "activated addBA response timer on %pM tid %d\n",
462 	       sta->sta.addr, tid);
463 
464 	spin_lock_bh(&sta->lock);
465 	sta->ampdu_mlme.last_addba_req_time[tid] = jiffies;
466 	sta->ampdu_mlme.addba_req_num[tid]++;
467 	spin_unlock_bh(&sta->lock);
468 
469 	if (sta->sta.valid_links ||
470 	    sta->sta.deflink.eht_cap.has_eht ||
471 	    ieee80211_hw_check(&local->hw, STRICT)) {
472 		buf_size = local->hw.max_tx_aggregation_subframes;
473 	} else if (sta->sta.deflink.he_cap.has_he) {
474 		buf_size = min_t(u16, local->hw.max_tx_aggregation_subframes,
475 				 IEEE80211_MAX_AMPDU_BUF_HE);
476 	} else {
477 		/*
478 		 * We really should use what the driver told us it will
479 		 * transmit as the maximum, but certain APs (e.g. the
480 		 * LinkSys WRT120N with FW v1.0.07 build 002 Jun 18 2012)
481 		 * will crash when we use a lower number.
482 		 */
483 		buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
484 	}
485 
486 	/* send AddBA request */
487 	ieee80211_send_addba_request(sta, tid, tid_tx->dialog_token,
488 				     tid_tx->ssn, buf_size, tid_tx->timeout,
489 				     tid_tx->ndp);
490 
491 	WARN_ON(test_and_set_bit(HT_AGG_STATE_SENT_ADDBA, &tid_tx->state));
492 }
493 
494 void ieee80211_tx_ba_session_handle_start(struct sta_info *sta, int tid)
495 {
496 	struct tid_ampdu_tx *tid_tx;
497 	struct ieee80211_local *local = sta->local;
498 	struct ieee80211_sub_if_data *sdata = sta->sdata;
499 	struct ieee80211_ampdu_params params = {
500 		.sta = &sta->sta,
501 		.action = IEEE80211_AMPDU_TX_START,
502 		.tid = tid,
503 		.buf_size = 0,
504 		.amsdu = false,
505 		.timeout = 0,
506 	};
507 	int ret;
508 
509 	tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
510 
511 	/*
512 	 * Start queuing up packets for this aggregation session.
513 	 * We're going to release them once the driver is OK with
514 	 * that.
515 	 */
516 	clear_bit(HT_AGG_STATE_WANT_START, &tid_tx->state);
517 
518 	/*
519 	 * Make sure no packets are being processed. This ensures that
520 	 * we have a valid starting sequence number and that in-flight
521 	 * packets have been flushed out and no packets for this TID
522 	 * will go into the driver during the ampdu_action call.
523 	 */
524 	synchronize_net();
525 
526 	tid_tx->ndp = ieee80211_s1g_use_ndp_ba(sdata, sta);
527 	params.ssn = sta->tid_seq[tid] >> 4;
528 	ret = drv_ampdu_action(local, sdata, &params);
529 	tid_tx->ssn = params.ssn;
530 	if (ret == IEEE80211_AMPDU_TX_START_DELAY_ADDBA) {
531 		return;
532 	} else if (ret == IEEE80211_AMPDU_TX_START_IMMEDIATE) {
533 		/*
534 		 * We didn't send the request yet, so don't need to check
535 		 * here if we already got a response, just mark as driver
536 		 * ready immediately.
537 		 */
538 		set_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state);
539 	} else if (ret) {
540 		ht_dbg(sdata,
541 		       "BA request denied - HW unavailable for %pM tid %d\n",
542 		       sta->sta.addr, tid);
543 		spin_lock_bh(&sta->lock);
544 		ieee80211_agg_splice_packets(sdata, tid_tx, tid);
545 		ieee80211_assign_tid_tx(sta, tid, NULL);
546 		ieee80211_agg_splice_finish(sdata, tid);
547 		spin_unlock_bh(&sta->lock);
548 
549 		ieee80211_agg_start_txq(sta, tid, false);
550 
551 		kfree_rcu(tid_tx, rcu_head);
552 		return;
553 	}
554 
555 	ieee80211_send_addba_with_timeout(sta, tid_tx);
556 }
557 
558 void ieee80211_refresh_tx_agg_session_timer(struct ieee80211_sta *pubsta,
559 					    u16 tid)
560 {
561 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
562 	struct tid_ampdu_tx *tid_tx;
563 
564 	if (WARN_ON_ONCE(tid >= IEEE80211_NUM_TIDS))
565 		return;
566 
567 	tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
568 	if (!tid_tx)
569 		return;
570 
571 	tid_tx->last_tx = jiffies;
572 }
573 EXPORT_SYMBOL(ieee80211_refresh_tx_agg_session_timer);
574 
575 /*
576  * After accepting the AddBA Response we activated a timer,
577  * resetting it after each frame that we send.
578  */
579 static void sta_tx_agg_session_timer_expired(struct timer_list *t)
580 {
581 	struct tid_ampdu_tx *tid_tx = timer_container_of(tid_tx, t,
582 							 session_timer);
583 	struct sta_info *sta = tid_tx->sta;
584 	u8 tid = tid_tx->tid;
585 	unsigned long timeout;
586 
587 	if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
588 		return;
589 	}
590 
591 	timeout = tid_tx->last_tx + TU_TO_JIFFIES(tid_tx->timeout);
592 	if (time_is_after_jiffies(timeout)) {
593 		mod_timer(&tid_tx->session_timer, timeout);
594 		return;
595 	}
596 
597 	ht_dbg(sta->sdata, "tx session timer expired on %pM tid %d\n",
598 	       sta->sta.addr, tid);
599 
600 	ieee80211_stop_tx_ba_session(&sta->sta, tid);
601 }
602 
603 int ieee80211_start_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid,
604 				  u16 timeout)
605 {
606 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
607 	struct ieee80211_sub_if_data *sdata = sta->sdata;
608 	struct ieee80211_local *local = sdata->local;
609 	struct tid_ampdu_tx *tid_tx;
610 	int ret = 0;
611 
612 	trace_api_start_tx_ba_session(pubsta, tid);
613 
614 	if (WARN(sta->reserved_tid == tid,
615 		 "Requested to start BA session on reserved tid=%d", tid))
616 		return -EINVAL;
617 
618 	if (!pubsta->valid_links &&
619 	    !pubsta->deflink.ht_cap.ht_supported &&
620 	    !pubsta->deflink.vht_cap.vht_supported &&
621 	    !pubsta->deflink.he_cap.has_he &&
622 	    !pubsta->deflink.eht_cap.has_eht &&
623 	    !pubsta->deflink.s1g_cap.s1g)
624 		return -EINVAL;
625 
626 	if (WARN_ON_ONCE(!local->ops->ampdu_action))
627 		return -EINVAL;
628 
629 	if ((tid >= IEEE80211_NUM_TIDS) ||
630 	    !ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) ||
631 	    ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW))
632 		return -EINVAL;
633 
634 	if (WARN_ON(tid >= IEEE80211_FIRST_TSPEC_TSID))
635 		return -EINVAL;
636 
637 	ht_dbg(sdata, "Open BA session requested for %pM tid %u\n",
638 	       pubsta->addr, tid);
639 
640 	if (sdata->vif.type != NL80211_IFTYPE_STATION &&
641 	    sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
642 	    sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
643 	    sdata->vif.type != NL80211_IFTYPE_AP &&
644 	    sdata->vif.type != NL80211_IFTYPE_ADHOC)
645 		return -EINVAL;
646 
647 	if (test_sta_flag(sta, WLAN_STA_BLOCK_BA)) {
648 		ht_dbg(sdata,
649 		       "BA sessions blocked - Denying BA session request %pM tid %d\n",
650 		       sta->sta.addr, tid);
651 		return -EINVAL;
652 	}
653 
654 	if (test_sta_flag(sta, WLAN_STA_MFP) &&
655 	    !test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
656 		ht_dbg(sdata,
657 		       "MFP STA not authorized - deny BA session request %pM tid %d\n",
658 		       sta->sta.addr, tid);
659 		return -EINVAL;
660 	}
661 
662 	/*
663 	 * 802.11n-2009 11.5.1.1: If the initiating STA is an HT STA, is a
664 	 * member of an IBSS, and has no other existing Block Ack agreement
665 	 * with the recipient STA, then the initiating STA shall transmit a
666 	 * Probe Request frame to the recipient STA and shall not transmit an
667 	 * ADDBA Request frame unless it receives a Probe Response frame
668 	 * from the recipient within dot11ADDBAFailureTimeout.
669 	 *
670 	 * The probe request mechanism for ADDBA is currently not implemented,
671 	 * but we only build up Block Ack session with HT STAs. This information
672 	 * is set when we receive a bss info from a probe response or a beacon.
673 	 */
674 	if (sta->sdata->vif.type == NL80211_IFTYPE_ADHOC &&
675 	    !sta->sta.deflink.ht_cap.ht_supported) {
676 		ht_dbg(sdata,
677 		       "BA request denied - IBSS STA %pM does not advertise HT support\n",
678 		       pubsta->addr);
679 		return -EINVAL;
680 	}
681 
682 	spin_lock_bh(&sta->lock);
683 
684 	/* we have tried too many times, receiver does not want A-MPDU */
685 	if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_MAX_RETRIES) {
686 		ret = -EBUSY;
687 		goto err_unlock_sta;
688 	}
689 
690 	/*
691 	 * if we have tried more than HT_AGG_BURST_RETRIES times we
692 	 * will spread our requests in time to avoid stalling connection
693 	 * for too long
694 	 */
695 	if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_BURST_RETRIES &&
696 	    time_before(jiffies, sta->ampdu_mlme.last_addba_req_time[tid] +
697 			HT_AGG_RETRIES_PERIOD)) {
698 		ht_dbg(sdata,
699 		       "BA request denied - %d failed requests on %pM tid %u\n",
700 		       sta->ampdu_mlme.addba_req_num[tid], sta->sta.addr, tid);
701 		ret = -EBUSY;
702 		goto err_unlock_sta;
703 	}
704 
705 	tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
706 	/* check if the TID is not in aggregation flow already */
707 	if (tid_tx || sta->ampdu_mlme.tid_start_tx[tid]) {
708 		ht_dbg(sdata,
709 		       "BA request denied - session is not idle on %pM tid %u\n",
710 		       sta->sta.addr, tid);
711 		ret = -EAGAIN;
712 		goto err_unlock_sta;
713 	}
714 
715 	/* prepare A-MPDU MLME for Tx aggregation */
716 	tid_tx = kzalloc_obj(struct tid_ampdu_tx, GFP_ATOMIC);
717 	if (!tid_tx) {
718 		ret = -ENOMEM;
719 		goto err_unlock_sta;
720 	}
721 
722 	skb_queue_head_init(&tid_tx->pending);
723 	__set_bit(HT_AGG_STATE_WANT_START, &tid_tx->state);
724 
725 	tid_tx->timeout = timeout;
726 	tid_tx->sta = sta;
727 	tid_tx->tid = tid;
728 
729 	/* response timer */
730 	timer_setup(&tid_tx->addba_resp_timer, sta_addba_resp_timer_expired, 0);
731 
732 	/* tx timer */
733 	timer_setup(&tid_tx->session_timer,
734 		    sta_tx_agg_session_timer_expired, TIMER_DEFERRABLE);
735 
736 	/* assign a dialog token */
737 	sta->ampdu_mlme.dialog_token_allocator++;
738 	tid_tx->dialog_token = sta->ampdu_mlme.dialog_token_allocator;
739 
740 	/*
741 	 * Finally, assign it to the start array; the work item will
742 	 * collect it and move it to the normal array.
743 	 */
744 	sta->ampdu_mlme.tid_start_tx[tid] = tid_tx;
745 
746 	wiphy_work_queue(local->hw.wiphy, &sta->ampdu_mlme.work);
747 
748 	/* this flow continues off the work */
749  err_unlock_sta:
750 	spin_unlock_bh(&sta->lock);
751 	return ret;
752 }
753 EXPORT_SYMBOL(ieee80211_start_tx_ba_session);
754 
755 static void ieee80211_agg_tx_operational(struct ieee80211_local *local,
756 					 struct sta_info *sta, u16 tid)
757 {
758 	struct tid_ampdu_tx *tid_tx;
759 	struct ieee80211_ampdu_params params = {
760 		.sta = &sta->sta,
761 		.action = IEEE80211_AMPDU_TX_OPERATIONAL,
762 		.tid = tid,
763 		.timeout = 0,
764 		.ssn = 0,
765 	};
766 
767 	lockdep_assert_wiphy(sta->local->hw.wiphy);
768 
769 	tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
770 	params.buf_size = tid_tx->buf_size;
771 	params.amsdu = tid_tx->amsdu;
772 
773 	ht_dbg(sta->sdata, "Aggregation is on for %pM tid %d\n",
774 	       sta->sta.addr, tid);
775 
776 	drv_ampdu_action(local, sta->sdata, &params);
777 
778 	/*
779 	 * synchronize with TX path, while splicing the TX path
780 	 * should block so it won't put more packets onto pending.
781 	 */
782 	spin_lock_bh(&sta->lock);
783 
784 	ieee80211_agg_splice_packets(sta->sdata, tid_tx, tid);
785 	/*
786 	 * Now mark as operational. This will be visible
787 	 * in the TX path, and lets it go lock-free in
788 	 * the common case.
789 	 */
790 	set_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state);
791 	ieee80211_agg_splice_finish(sta->sdata, tid);
792 
793 	spin_unlock_bh(&sta->lock);
794 
795 	ieee80211_agg_start_txq(sta, tid, true);
796 }
797 
798 void ieee80211_start_tx_ba_cb(struct sta_info *sta, int tid,
799 			      struct tid_ampdu_tx *tid_tx)
800 {
801 	struct ieee80211_sub_if_data *sdata = sta->sdata;
802 	struct ieee80211_local *local = sdata->local;
803 
804 	lockdep_assert_wiphy(sta->local->hw.wiphy);
805 
806 	if (WARN_ON(test_and_set_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state)))
807 		return;
808 
809 	if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state) ||
810 	    test_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state))
811 		return;
812 
813 	if (!test_bit(HT_AGG_STATE_SENT_ADDBA, &tid_tx->state)) {
814 		ieee80211_send_addba_with_timeout(sta, tid_tx);
815 		/* RESPONSE_RECEIVED state would trigger the flow again */
816 		return;
817 	}
818 
819 	if (test_bit(HT_AGG_STATE_RESPONSE_RECEIVED, &tid_tx->state))
820 		ieee80211_agg_tx_operational(local, sta, tid);
821 }
822 
823 static struct tid_ampdu_tx *
824 ieee80211_lookup_tid_tx(struct ieee80211_sub_if_data *sdata,
825 			const u8 *ra, u16 tid, struct sta_info **sta)
826 {
827 	struct tid_ampdu_tx *tid_tx;
828 
829 	if (tid >= IEEE80211_NUM_TIDS) {
830 		ht_dbg(sdata, "Bad TID value: tid = %d (>= %d)\n",
831 		       tid, IEEE80211_NUM_TIDS);
832 		return NULL;
833 	}
834 
835 	*sta = sta_info_get_bss(sdata, ra);
836 	if (!*sta) {
837 		ht_dbg(sdata, "Could not find station: %pM\n", ra);
838 		return NULL;
839 	}
840 
841 	tid_tx = rcu_dereference((*sta)->ampdu_mlme.tid_tx[tid]);
842 
843 	if (WARN_ON(!tid_tx))
844 		ht_dbg(sdata, "addBA was not requested!\n");
845 
846 	return tid_tx;
847 }
848 
849 void ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
850 				      const u8 *ra, u16 tid)
851 {
852 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
853 	struct ieee80211_local *local = sdata->local;
854 	struct sta_info *sta;
855 	struct tid_ampdu_tx *tid_tx;
856 
857 	trace_api_start_tx_ba_cb(sdata, ra, tid);
858 
859 	rcu_read_lock();
860 	tid_tx = ieee80211_lookup_tid_tx(sdata, ra, tid, &sta);
861 	if (!tid_tx)
862 		goto out;
863 
864 	set_bit(HT_AGG_STATE_START_CB, &tid_tx->state);
865 	wiphy_work_queue(local->hw.wiphy, &sta->ampdu_mlme.work);
866  out:
867 	rcu_read_unlock();
868 }
869 EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);
870 
871 int ieee80211_stop_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid)
872 {
873 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
874 	struct ieee80211_sub_if_data *sdata = sta->sdata;
875 	struct ieee80211_local *local = sdata->local;
876 	struct tid_ampdu_tx *tid_tx;
877 	int ret = 0;
878 
879 	trace_api_stop_tx_ba_session(pubsta, tid);
880 
881 	if (!local->ops->ampdu_action)
882 		return -EINVAL;
883 
884 	if (tid >= IEEE80211_NUM_TIDS)
885 		return -EINVAL;
886 
887 	spin_lock_bh(&sta->lock);
888 	tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
889 
890 	if (!tid_tx) {
891 		ret = -ENOENT;
892 		goto unlock;
893 	}
894 
895 	WARN(sta->reserved_tid == tid,
896 	     "Requested to stop BA session on reserved tid=%d", tid);
897 
898 	if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
899 		/* already in progress stopping it */
900 		ret = 0;
901 		goto unlock;
902 	}
903 
904 	set_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state);
905 	wiphy_work_queue(local->hw.wiphy, &sta->ampdu_mlme.work);
906 
907  unlock:
908 	spin_unlock_bh(&sta->lock);
909 	return ret;
910 }
911 EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);
912 
913 void ieee80211_stop_tx_ba_cb(struct sta_info *sta, int tid,
914 			     struct tid_ampdu_tx *tid_tx)
915 {
916 	struct ieee80211_sub_if_data *sdata = sta->sdata;
917 	bool send_delba = false;
918 	bool start_txq = false;
919 
920 	ht_dbg(sdata, "Stopping Tx BA session for %pM tid %d\n",
921 	       sta->sta.addr, tid);
922 
923 	spin_lock_bh(&sta->lock);
924 
925 	if (!test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
926 		ht_dbg(sdata,
927 		       "unexpected callback to A-MPDU stop for %pM tid %d\n",
928 		       sta->sta.addr, tid);
929 		goto unlock_sta;
930 	}
931 
932 	if (tid_tx->stop_initiator == WLAN_BACK_INITIATOR && tid_tx->tx_stop)
933 		send_delba = true;
934 
935 	ieee80211_remove_tid_tx(sta, tid);
936 	start_txq = true;
937 
938  unlock_sta:
939 	spin_unlock_bh(&sta->lock);
940 
941 	if (start_txq)
942 		ieee80211_agg_start_txq(sta, tid, false);
943 
944 	if (send_delba)
945 		ieee80211_send_delba(sdata, sta->sta.addr, tid,
946 				     WLAN_BACK_INITIATOR,
947 				     WLAN_REASON_QSTA_NOT_USE,
948 				     tid_tx->ndp);
949 }
950 
951 void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
952 				     const u8 *ra, u16 tid)
953 {
954 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
955 	struct ieee80211_local *local = sdata->local;
956 	struct sta_info *sta;
957 	struct tid_ampdu_tx *tid_tx;
958 
959 	trace_api_stop_tx_ba_cb(sdata, ra, tid);
960 
961 	rcu_read_lock();
962 	tid_tx = ieee80211_lookup_tid_tx(sdata, ra, tid, &sta);
963 	if (!tid_tx)
964 		goto out;
965 
966 	set_bit(HT_AGG_STATE_STOP_CB, &tid_tx->state);
967 	wiphy_work_queue(local->hw.wiphy, &sta->ampdu_mlme.work);
968  out:
969 	rcu_read_unlock();
970 }
971 EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);
972 
973 
974 void ieee80211_process_addba_resp(struct ieee80211_local *local,
975 				  struct sta_info *sta,
976 				  struct ieee80211_mgmt *mgmt,
977 				  size_t len)
978 {
979 	struct tid_ampdu_tx *tid_tx;
980 	struct ieee80211_txq *txq;
981 	u16 capab, tid, buf_size;
982 	bool amsdu;
983 
984 	lockdep_assert_wiphy(sta->local->hw.wiphy);
985 
986 	capab = le16_to_cpu(mgmt->u.action.addba_resp.capab);
987 	amsdu = capab & IEEE80211_ADDBA_PARAM_AMSDU_MASK;
988 	tid = u16_get_bits(capab, IEEE80211_ADDBA_PARAM_TID_MASK);
989 	buf_size = u16_get_bits(capab, IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK);
990 
991 	ieee80211_retrieve_addba_ext_data(sta,
992 					  mgmt->u.action.addba_resp.variable,
993 					  len - offsetof(typeof(*mgmt),
994 							 u.action.addba_resp.variable),
995 					  &buf_size);
996 
997 	buf_size = min(buf_size, local->hw.max_tx_aggregation_subframes);
998 
999 	txq = sta->sta.txq[tid];
1000 	if (!amsdu && txq)
1001 		set_bit(IEEE80211_TXQ_NO_AMSDU, &to_txq_info(txq)->flags);
1002 
1003 	tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
1004 	if (!tid_tx)
1005 		return;
1006 
1007 	if (mgmt->u.action.addba_resp.dialog_token != tid_tx->dialog_token) {
1008 		ht_dbg(sta->sdata, "wrong addBA response token, %pM tid %d\n",
1009 		       sta->sta.addr, tid);
1010 		return;
1011 	}
1012 
1013 	timer_delete_sync(&tid_tx->addba_resp_timer);
1014 
1015 	ht_dbg(sta->sdata, "switched off addBA timer for %pM tid %d\n",
1016 	       sta->sta.addr, tid);
1017 
1018 	/*
1019 	 * addba_resp_timer may have fired before we got here, and
1020 	 * caused WANT_STOP to be set. If the stop then was already
1021 	 * processed further, STOPPING might be set.
1022 	 */
1023 	if (test_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state) ||
1024 	    test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
1025 		ht_dbg(sta->sdata,
1026 		       "got addBA resp for %pM tid %d but we already gave up\n",
1027 		       sta->sta.addr, tid);
1028 		return;
1029 	}
1030 
1031 	/*
1032 	 * IEEE 802.11-2007 7.3.1.14:
1033 	 * In an ADDBA Response frame, when the Status Code field
1034 	 * is set to 0, the Buffer Size subfield is set to a value
1035 	 * of at least 1.
1036 	 */
1037 	if (le16_to_cpu(mgmt->u.action.addba_resp.status)
1038 			== WLAN_STATUS_SUCCESS && buf_size) {
1039 		if (test_and_set_bit(HT_AGG_STATE_RESPONSE_RECEIVED,
1040 				     &tid_tx->state)) {
1041 			/* ignore duplicate response */
1042 			return;
1043 		}
1044 
1045 		tid_tx->buf_size = buf_size;
1046 		tid_tx->amsdu = amsdu;
1047 
1048 		if (test_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state))
1049 			ieee80211_agg_tx_operational(local, sta, tid);
1050 
1051 		sta->ampdu_mlme.addba_req_num[tid] = 0;
1052 
1053 		tid_tx->timeout =
1054 			le16_to_cpu(mgmt->u.action.addba_resp.timeout);
1055 
1056 		if (tid_tx->timeout) {
1057 			mod_timer(&tid_tx->session_timer,
1058 				  TU_TO_EXP_TIME(tid_tx->timeout));
1059 			tid_tx->last_tx = jiffies;
1060 		}
1061 
1062 	} else {
1063 		__ieee80211_stop_tx_ba_session(sta, tid, AGG_STOP_DECLINED);
1064 	}
1065 }
1066