xref: /openbmc/linux/net/mac80211/agg-tx.c (revision 5f8b7d4b2e9604d03ae06f1a2dd5a1f34c33e533)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b8695a8fSJohannes Berg /*
3b8695a8fSJohannes Berg  * HT handling
4b8695a8fSJohannes Berg  *
5b8695a8fSJohannes Berg  * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
6b8695a8fSJohannes Berg  * Copyright 2002-2005, Instant802 Networks, Inc.
7b8695a8fSJohannes Berg  * Copyright 2005-2006, Devicescape Software, Inc.
8b8695a8fSJohannes Berg  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
9b8695a8fSJohannes Berg  * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
10cfcdbde3SJohannes Berg  * Copyright 2007-2010, Intel Corporation
117a7c0a64SJohannes Berg  * Copyright(c) 2015-2017 Intel Deutschland GmbH
126f2db658SJohannes Berg  * Copyright (C) 2018 - 2023 Intel Corporation
13b8695a8fSJohannes Berg  */
14b8695a8fSJohannes Berg 
15b8695a8fSJohannes Berg #include <linux/ieee80211.h>
165a0e3ad6STejun Heo #include <linux/slab.h>
17bc3b2d7fSPaul Gortmaker #include <linux/export.h>
18b8695a8fSJohannes Berg #include <net/mac80211.h>
19b8695a8fSJohannes Berg #include "ieee80211_i.h"
2024487981SJohannes Berg #include "driver-ops.h"
21b8695a8fSJohannes Berg #include "wme.h"
22b8695a8fSJohannes Berg 
2386ab6c5aSJohannes Berg /**
2473a72a81SJohannes Berg  * DOC: TX A-MPDU aggregation
2586ab6c5aSJohannes Berg  *
2686ab6c5aSJohannes Berg  * Aggregation on the TX side requires setting the hardware flag
2773a72a81SJohannes Berg  * %IEEE80211_HW_AMPDU_AGGREGATION. The driver will then be handed
2873a72a81SJohannes Berg  * packets with a flag indicating A-MPDU aggregation. The driver
2973a72a81SJohannes Berg  * or device is responsible for actually aggregating the frames,
3073a72a81SJohannes Berg  * as well as deciding how many and which to aggregate.
3186ab6c5aSJohannes Berg  *
3273a72a81SJohannes Berg  * When TX aggregation is started by some subsystem (usually the rate
3373a72a81SJohannes Berg  * control algorithm would be appropriate) by calling the
3473a72a81SJohannes Berg  * ieee80211_start_tx_ba_session() function, the driver will be
3573a72a81SJohannes Berg  * notified via its @ampdu_action function, with the
3673a72a81SJohannes Berg  * %IEEE80211_AMPDU_TX_START action.
3786ab6c5aSJohannes Berg  *
3886ab6c5aSJohannes Berg  * In response to that, the driver is later required to call the
3973a72a81SJohannes Berg  * ieee80211_start_tx_ba_cb_irqsafe() function, which will really
4073a72a81SJohannes Berg  * start the aggregation session after the peer has also responded.
4173a72a81SJohannes Berg  * If the peer responds negatively, the session will be stopped
4273a72a81SJohannes Berg  * again right away. Note that it is possible for the aggregation
4373a72a81SJohannes Berg  * session to be stopped before the driver has indicated that it
4473a72a81SJohannes Berg  * is done setting it up, in which case it must not indicate the
4573a72a81SJohannes Berg  * setup completion.
4686ab6c5aSJohannes Berg  *
4773a72a81SJohannes Berg  * Also note that, since we also need to wait for a response from
4873a72a81SJohannes Berg  * the peer, the driver is notified of the completion of the
4973a72a81SJohannes Berg  * handshake by the %IEEE80211_AMPDU_TX_OPERATIONAL action to the
5073a72a81SJohannes Berg  * @ampdu_action callback.
5173a72a81SJohannes Berg  *
5273a72a81SJohannes Berg  * Similarly, when the aggregation session is stopped by the peer
5373a72a81SJohannes Berg  * or something calling ieee80211_stop_tx_ba_session(), the driver's
5473a72a81SJohannes Berg  * @ampdu_action function will be called with the action
5573a72a81SJohannes Berg  * %IEEE80211_AMPDU_TX_STOP. In this case, the call must not fail,
5673a72a81SJohannes Berg  * and the driver must later call ieee80211_stop_tx_ba_cb_irqsafe().
5742624d49SYogesh Ashok Powar  * Note that the sta can get destroyed before the BA tear down is
5842624d49SYogesh Ashok Powar  * complete.
5986ab6c5aSJohannes Berg  */
6086ab6c5aSJohannes Berg 
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)61b8695a8fSJohannes Berg static void ieee80211_send_addba_request(struct ieee80211_sub_if_data *sdata,
62b8695a8fSJohannes Berg 					 const u8 *da, u16 tid,
63b8695a8fSJohannes Berg 					 u8 dialog_token, u16 start_seq_num,
64b8695a8fSJohannes Berg 					 u16 agg_size, u16 timeout)
65b8695a8fSJohannes Berg {
66b8695a8fSJohannes Berg 	struct ieee80211_local *local = sdata->local;
67b8695a8fSJohannes Berg 	struct sk_buff *skb;
68b8695a8fSJohannes Berg 	struct ieee80211_mgmt *mgmt;
69b8695a8fSJohannes Berg 	u16 capab;
70b8695a8fSJohannes Berg 
71b8695a8fSJohannes Berg 	skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
72b8695a8fSJohannes Berg 
73d15b8459SJoe Perches 	if (!skb)
74b8695a8fSJohannes Berg 		return;
75d15b8459SJoe Perches 
76b8695a8fSJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
77b080db58SJohannes Berg 	mgmt = skb_put_zero(skb, 24);
78b8695a8fSJohannes Berg 	memcpy(mgmt->da, da, ETH_ALEN);
7947846c9bSJohannes Berg 	memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
808abd3f9bSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
81ae2772b3SThomas Pedersen 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
82ae2772b3SThomas Pedersen 	    sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
8347846c9bSJohannes Berg 		memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
8446900298SJohannes Berg 	else if (sdata->vif.type == NL80211_IFTYPE_STATION)
85f7ee3041SJohannes Berg 		memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN);
8613c40c54SAlexander Simon 	else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
8713c40c54SAlexander Simon 		memcpy(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN);
88b8695a8fSJohannes Berg 
89b8695a8fSJohannes Berg 	mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
90b8695a8fSJohannes Berg 					  IEEE80211_STYPE_ACTION);
91b8695a8fSJohannes Berg 
92b8695a8fSJohannes Berg 	skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
93b8695a8fSJohannes Berg 
94b8695a8fSJohannes Berg 	mgmt->u.action.category = WLAN_CATEGORY_BACK;
95b8695a8fSJohannes Berg 	mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
96b8695a8fSJohannes Berg 
97b8695a8fSJohannes Berg 	mgmt->u.action.u.addba_req.dialog_token = dialog_token;
98db8ebd06SJohannes Berg 	capab = IEEE80211_ADDBA_PARAM_AMSDU_MASK;
99db8ebd06SJohannes Berg 	capab |= IEEE80211_ADDBA_PARAM_POLICY_MASK;
100db8ebd06SJohannes Berg 	capab |= u16_encode_bits(tid, IEEE80211_ADDBA_PARAM_TID_MASK);
101db8ebd06SJohannes Berg 	capab |= u16_encode_bits(agg_size, IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK);
102b8695a8fSJohannes Berg 
103b8695a8fSJohannes Berg 	mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
104b8695a8fSJohannes Berg 
105b8695a8fSJohannes Berg 	mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
106b8695a8fSJohannes Berg 	mgmt->u.action.u.addba_req.start_seq_num =
107b8695a8fSJohannes Berg 					cpu_to_le16(start_seq_num << 4);
108b8695a8fSJohannes Berg 
109e1e68b14SJohannes Berg 	ieee80211_tx_skb_tid(sdata, skb, tid, -1);
110b8695a8fSJohannes Berg }
111b8695a8fSJohannes Berg 
ieee80211_send_bar(struct ieee80211_vif * vif,u8 * ra,u16 tid,u16 ssn)1128c771244SFelix Fietkau void ieee80211_send_bar(struct ieee80211_vif *vif, u8 *ra, u16 tid, u16 ssn)
113b8695a8fSJohannes Berg {
1148c771244SFelix Fietkau 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
115b8695a8fSJohannes Berg 	struct ieee80211_local *local = sdata->local;
116b8695a8fSJohannes Berg 	struct sk_buff *skb;
117b8695a8fSJohannes Berg 	struct ieee80211_bar *bar;
118b8695a8fSJohannes Berg 	u16 bar_control = 0;
119b8695a8fSJohannes Berg 
120b8695a8fSJohannes Berg 	skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
121d15b8459SJoe Perches 	if (!skb)
122b8695a8fSJohannes Berg 		return;
123d15b8459SJoe Perches 
124b8695a8fSJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
125b080db58SJohannes Berg 	bar = skb_put_zero(skb, sizeof(*bar));
126b8695a8fSJohannes Berg 	bar->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
127b8695a8fSJohannes Berg 					 IEEE80211_STYPE_BACK_REQ);
128b8695a8fSJohannes Berg 	memcpy(bar->ra, ra, ETH_ALEN);
12947846c9bSJohannes Berg 	memcpy(bar->ta, sdata->vif.addr, ETH_ALEN);
130b8695a8fSJohannes Berg 	bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
131b8695a8fSJohannes Berg 	bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
132c1407b6cSHelmut Schaa 	bar_control |= (u16)(tid << IEEE80211_BAR_CTRL_TID_INFO_SHIFT);
133b8695a8fSJohannes Berg 	bar->control = cpu_to_le16(bar_control);
134b8695a8fSJohannes Berg 	bar->start_seq_num = cpu_to_le16(ssn);
135b8695a8fSJohannes Berg 
1362f7916f8SChristian Lamparter 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1372f7916f8SChristian Lamparter 					IEEE80211_TX_CTL_REQ_TX_STATUS;
138e1e68b14SJohannes Berg 	ieee80211_tx_skb_tid(sdata, skb, tid, -1);
139b8695a8fSJohannes Berg }
1408c771244SFelix Fietkau EXPORT_SYMBOL(ieee80211_send_bar);
141b8695a8fSJohannes Berg 
ieee80211_assign_tid_tx(struct sta_info * sta,int tid,struct tid_ampdu_tx * tid_tx)142ec034b20SJohannes Berg void ieee80211_assign_tid_tx(struct sta_info *sta, int tid,
143ec034b20SJohannes Berg 			     struct tid_ampdu_tx *tid_tx)
144ec034b20SJohannes Berg {
145ec034b20SJohannes Berg 	lockdep_assert_held(&sta->ampdu_mlme.mtx);
146ec034b20SJohannes Berg 	lockdep_assert_held(&sta->lock);
147ec034b20SJohannes Berg 	rcu_assign_pointer(sta->ampdu_mlme.tid_tx[tid], tid_tx);
148ec034b20SJohannes Berg }
149ec034b20SJohannes Berg 
15030bf5f1fSJohannes Berg /*
15130bf5f1fSJohannes Berg  * When multiple aggregation sessions on multiple stations
15230bf5f1fSJohannes Berg  * are being created/destroyed simultaneously, we need to
15330bf5f1fSJohannes Berg  * refcount the global queue stop caused by that in order
15430bf5f1fSJohannes Berg  * to not get into a situation where one of the aggregation
15530bf5f1fSJohannes Berg  * setup or teardown re-enables queues before the other is
15630bf5f1fSJohannes Berg  * ready to handle that.
15730bf5f1fSJohannes Berg  *
15830bf5f1fSJohannes Berg  * These two functions take care of this issue by keeping
15930bf5f1fSJohannes Berg  * a global "agg_queue_stop" refcount.
16030bf5f1fSJohannes Berg  */
__acquires(agg_queue)16130bf5f1fSJohannes Berg static void __acquires(agg_queue)
16230bf5f1fSJohannes Berg ieee80211_stop_queue_agg(struct ieee80211_sub_if_data *sdata, int tid)
16330bf5f1fSJohannes Berg {
16430bf5f1fSJohannes Berg 	int queue = sdata->vif.hw_queue[ieee80211_ac_from_tid(tid)];
16530bf5f1fSJohannes Berg 
166cca07b00SLuciano Coelho 	/* we do refcounting here, so don't use the queue reason refcounting */
167cca07b00SLuciano Coelho 
16830bf5f1fSJohannes Berg 	if (atomic_inc_return(&sdata->local->agg_queue_stop[queue]) == 1)
16930bf5f1fSJohannes Berg 		ieee80211_stop_queue_by_reason(
17030bf5f1fSJohannes Berg 			&sdata->local->hw, queue,
171cca07b00SLuciano Coelho 			IEEE80211_QUEUE_STOP_REASON_AGGREGATION,
172cca07b00SLuciano Coelho 			false);
17330bf5f1fSJohannes Berg 	__acquire(agg_queue);
17430bf5f1fSJohannes Berg }
17530bf5f1fSJohannes Berg 
__releases(agg_queue)17630bf5f1fSJohannes Berg static void __releases(agg_queue)
17730bf5f1fSJohannes Berg ieee80211_wake_queue_agg(struct ieee80211_sub_if_data *sdata, int tid)
17830bf5f1fSJohannes Berg {
17930bf5f1fSJohannes Berg 	int queue = sdata->vif.hw_queue[ieee80211_ac_from_tid(tid)];
18030bf5f1fSJohannes Berg 
18130bf5f1fSJohannes Berg 	if (atomic_dec_return(&sdata->local->agg_queue_stop[queue]) == 0)
18230bf5f1fSJohannes Berg 		ieee80211_wake_queue_by_reason(
18330bf5f1fSJohannes Berg 			&sdata->local->hw, queue,
184cca07b00SLuciano Coelho 			IEEE80211_QUEUE_STOP_REASON_AGGREGATION,
185cca07b00SLuciano Coelho 			false);
18630bf5f1fSJohannes Berg 	__release(agg_queue);
18730bf5f1fSJohannes Berg }
18830bf5f1fSJohannes Berg 
189ba8c3d6fSFelix Fietkau static void
ieee80211_agg_stop_txq(struct sta_info * sta,int tid)190ba8c3d6fSFelix Fietkau ieee80211_agg_stop_txq(struct sta_info *sta, int tid)
191ba8c3d6fSFelix Fietkau {
192ba8c3d6fSFelix Fietkau 	struct ieee80211_txq *txq = sta->sta.txq[tid];
193fa962b92SMichal Kazior 	struct ieee80211_sub_if_data *sdata;
194fa962b92SMichal Kazior 	struct fq *fq;
195ba8c3d6fSFelix Fietkau 	struct txq_info *txqi;
196ba8c3d6fSFelix Fietkau 
197ba8c3d6fSFelix Fietkau 	if (!txq)
198ba8c3d6fSFelix Fietkau 		return;
199ba8c3d6fSFelix Fietkau 
200ba8c3d6fSFelix Fietkau 	txqi = to_txq_info(txq);
201fa962b92SMichal Kazior 	sdata = vif_to_sdata(txq->vif);
202fa962b92SMichal Kazior 	fq = &sdata->local->fq;
203ba8c3d6fSFelix Fietkau 
204ba8c3d6fSFelix Fietkau 	/* Lock here to protect against further seqno updates on dequeue */
205fa962b92SMichal Kazior 	spin_lock_bh(&fq->lock);
206ba8c3d6fSFelix Fietkau 	set_bit(IEEE80211_TXQ_STOP, &txqi->flags);
207fa962b92SMichal Kazior 	spin_unlock_bh(&fq->lock);
208ba8c3d6fSFelix Fietkau }
209ba8c3d6fSFelix Fietkau 
210ba8c3d6fSFelix Fietkau static void
ieee80211_agg_start_txq(struct sta_info * sta,int tid,bool enable)211ba8c3d6fSFelix Fietkau ieee80211_agg_start_txq(struct sta_info *sta, int tid, bool enable)
212ba8c3d6fSFelix Fietkau {
213ba8c3d6fSFelix Fietkau 	struct ieee80211_txq *txq = sta->sta.txq[tid];
214ba8c3d6fSFelix Fietkau 	struct txq_info *txqi;
215ba8c3d6fSFelix Fietkau 
21606c41bdaSJohannes Berg 	lockdep_assert_held(&sta->ampdu_mlme.mtx);
21706c41bdaSJohannes Berg 
218ba8c3d6fSFelix Fietkau 	if (!txq)
219ba8c3d6fSFelix Fietkau 		return;
220ba8c3d6fSFelix Fietkau 
221ba8c3d6fSFelix Fietkau 	txqi = to_txq_info(txq);
222ba8c3d6fSFelix Fietkau 
223ba8c3d6fSFelix Fietkau 	if (enable)
224ba8c3d6fSFelix Fietkau 		set_bit(IEEE80211_TXQ_AMPDU, &txqi->flags);
225ba8c3d6fSFelix Fietkau 	else
226ba8c3d6fSFelix Fietkau 		clear_bit(IEEE80211_TXQ_AMPDU, &txqi->flags);
227ba8c3d6fSFelix Fietkau 
228ba8c3d6fSFelix Fietkau 	clear_bit(IEEE80211_TXQ_STOP, &txqi->flags);
229979e1f08SJohannes Berg 	local_bh_disable();
230979e1f08SJohannes Berg 	rcu_read_lock();
23118667600SToke Høiland-Jørgensen 	schedule_and_wake_txq(sta->sdata->local, txqi);
232979e1f08SJohannes Berg 	rcu_read_unlock();
233979e1f08SJohannes Berg 	local_bh_enable();
234ba8c3d6fSFelix Fietkau }
235ba8c3d6fSFelix Fietkau 
23630bf5f1fSJohannes Berg /*
23730bf5f1fSJohannes Berg  * splice packets from the STA's pending to the local pending,
23830bf5f1fSJohannes Berg  * requires a call to ieee80211_agg_splice_finish later
23930bf5f1fSJohannes Berg  */
__acquires(agg_queue)24030bf5f1fSJohannes Berg static void __acquires(agg_queue)
24130bf5f1fSJohannes Berg ieee80211_agg_splice_packets(struct ieee80211_sub_if_data *sdata,
24230bf5f1fSJohannes Berg 			     struct tid_ampdu_tx *tid_tx, u16 tid)
24330bf5f1fSJohannes Berg {
24430bf5f1fSJohannes Berg 	struct ieee80211_local *local = sdata->local;
24530bf5f1fSJohannes Berg 	int queue = sdata->vif.hw_queue[ieee80211_ac_from_tid(tid)];
24630bf5f1fSJohannes Berg 	unsigned long flags;
24730bf5f1fSJohannes Berg 
24830bf5f1fSJohannes Berg 	ieee80211_stop_queue_agg(sdata, tid);
24930bf5f1fSJohannes Berg 
25030bf5f1fSJohannes Berg 	if (WARN(!tid_tx,
25130bf5f1fSJohannes Berg 		 "TID %d gone but expected when splicing aggregates from the pending queue\n",
25230bf5f1fSJohannes Berg 		 tid))
25330bf5f1fSJohannes Berg 		return;
25430bf5f1fSJohannes Berg 
25530bf5f1fSJohannes Berg 	if (!skb_queue_empty(&tid_tx->pending)) {
25630bf5f1fSJohannes Berg 		spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
25730bf5f1fSJohannes Berg 		/* copy over remaining packets */
25830bf5f1fSJohannes Berg 		skb_queue_splice_tail_init(&tid_tx->pending,
25930bf5f1fSJohannes Berg 					   &local->pending[queue]);
26030bf5f1fSJohannes Berg 		spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
26130bf5f1fSJohannes Berg 	}
26230bf5f1fSJohannes Berg }
26330bf5f1fSJohannes Berg 
__releases(agg_queue)26430bf5f1fSJohannes Berg static void __releases(agg_queue)
26530bf5f1fSJohannes Berg ieee80211_agg_splice_finish(struct ieee80211_sub_if_data *sdata, u16 tid)
26630bf5f1fSJohannes Berg {
26730bf5f1fSJohannes Berg 	ieee80211_wake_queue_agg(sdata, tid);
26830bf5f1fSJohannes Berg }
26930bf5f1fSJohannes Berg 
ieee80211_remove_tid_tx(struct sta_info * sta,int tid)27030bf5f1fSJohannes Berg static void ieee80211_remove_tid_tx(struct sta_info *sta, int tid)
27130bf5f1fSJohannes Berg {
27230bf5f1fSJohannes Berg 	struct tid_ampdu_tx *tid_tx;
27330bf5f1fSJohannes Berg 
27430bf5f1fSJohannes Berg 	lockdep_assert_held(&sta->ampdu_mlme.mtx);
27530bf5f1fSJohannes Berg 	lockdep_assert_held(&sta->lock);
27630bf5f1fSJohannes Berg 
27730bf5f1fSJohannes Berg 	tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
27830bf5f1fSJohannes Berg 
27930bf5f1fSJohannes Berg 	/*
28030bf5f1fSJohannes Berg 	 * When we get here, the TX path will not be lockless any more wrt.
28130bf5f1fSJohannes Berg 	 * aggregation, since the OPERATIONAL bit has long been cleared.
28230bf5f1fSJohannes Berg 	 * Thus it will block on getting the lock, if it occurs. So if we
28330bf5f1fSJohannes Berg 	 * stop the queue now, we will not get any more packets, and any
28430bf5f1fSJohannes Berg 	 * that might be being processed will wait for us here, thereby
28530bf5f1fSJohannes Berg 	 * guaranteeing that no packets go to the tid_tx pending queue any
28630bf5f1fSJohannes Berg 	 * more.
28730bf5f1fSJohannes Berg 	 */
28830bf5f1fSJohannes Berg 
28930bf5f1fSJohannes Berg 	ieee80211_agg_splice_packets(sta->sdata, tid_tx, tid);
29030bf5f1fSJohannes Berg 
29130bf5f1fSJohannes Berg 	/* future packets must not find the tid_tx struct any more */
29230bf5f1fSJohannes Berg 	ieee80211_assign_tid_tx(sta, tid, NULL);
29330bf5f1fSJohannes Berg 
29430bf5f1fSJohannes Berg 	ieee80211_agg_splice_finish(sta->sdata, tid);
29530bf5f1fSJohannes Berg 
29630bf5f1fSJohannes Berg 	kfree_rcu(tid_tx, rcu_head);
29730bf5f1fSJohannes Berg }
29830bf5f1fSJohannes Berg 
___ieee80211_stop_tx_ba_session(struct sta_info * sta,u16 tid,enum ieee80211_agg_stop_reason reason)29967c282c0SJohannes Berg int ___ieee80211_stop_tx_ba_session(struct sta_info *sta, u16 tid,
300c82c4a80SJohannes Berg 				    enum ieee80211_agg_stop_reason reason)
30123e6a7eaSJohannes Berg {
302849b7967SJohannes Berg 	struct ieee80211_local *local = sta->local;
30340b275b6SJohannes Berg 	struct tid_ampdu_tx *tid_tx;
30450ea05efSSara Sharon 	struct ieee80211_ampdu_params params = {
30550ea05efSSara Sharon 		.sta = &sta->sta,
30650ea05efSSara Sharon 		.tid = tid,
30750ea05efSSara Sharon 		.buf_size = 0,
30850ea05efSSara Sharon 		.amsdu = false,
30950ea05efSSara Sharon 		.timeout = 0,
31050ea05efSSara Sharon 		.ssn = 0,
31150ea05efSSara Sharon 	};
31223e6a7eaSJohannes Berg 	int ret;
313a622ab72SJohannes Berg 
314cfcdbde3SJohannes Berg 	lockdep_assert_held(&sta->ampdu_mlme.mtx);
315a622ab72SJohannes Berg 
31618b559d5SJohannes Berg 	switch (reason) {
31718b559d5SJohannes Berg 	case AGG_STOP_DECLINED:
31818b559d5SJohannes Berg 	case AGG_STOP_LOCAL_REQUEST:
31918b559d5SJohannes Berg 	case AGG_STOP_PEER_REQUEST:
32050ea05efSSara Sharon 		params.action = IEEE80211_AMPDU_TX_STOP_CONT;
32118b559d5SJohannes Berg 		break;
32218b559d5SJohannes Berg 	case AGG_STOP_DESTROY_STA:
32350ea05efSSara Sharon 		params.action = IEEE80211_AMPDU_TX_STOP_FLUSH;
32418b559d5SJohannes Berg 		break;
32518b559d5SJohannes Berg 	default:
32618b559d5SJohannes Berg 		WARN_ON_ONCE(1);
32718b559d5SJohannes Berg 		return -EINVAL;
32818b559d5SJohannes Berg 	}
32918b559d5SJohannes Berg 
330cfcdbde3SJohannes Berg 	spin_lock_bh(&sta->lock);
331cfcdbde3SJohannes Berg 
33233ddd81eSJohannes Berg 	/* free struct pending for start, if present */
33333ddd81eSJohannes Berg 	tid_tx = sta->ampdu_mlme.tid_start_tx[tid];
33433ddd81eSJohannes Berg 	kfree(tid_tx);
33533ddd81eSJohannes Berg 	sta->ampdu_mlme.tid_start_tx[tid] = NULL;
33633ddd81eSJohannes Berg 
33740b275b6SJohannes Berg 	tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
33840b275b6SJohannes Berg 	if (!tid_tx) {
33940b275b6SJohannes Berg 		spin_unlock_bh(&sta->lock);
34040b275b6SJohannes Berg 		return -ENOENT;
34140b275b6SJohannes Berg 	}
34240b275b6SJohannes Berg 
34318b559d5SJohannes Berg 	/*
34418b559d5SJohannes Berg 	 * if we're already stopping ignore any new requests to stop
34518b559d5SJohannes Berg 	 * unless we're destroying it in which case notify the driver
34618b559d5SJohannes Berg 	 */
34724f50a9dSJohannes Berg 	if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
34824f50a9dSJohannes Berg 		spin_unlock_bh(&sta->lock);
34918b559d5SJohannes Berg 		if (reason != AGG_STOP_DESTROY_STA)
35024f50a9dSJohannes Berg 			return -EALREADY;
35150ea05efSSara Sharon 		params.action = IEEE80211_AMPDU_TX_STOP_FLUSH_CONT;
35250ea05efSSara Sharon 		ret = drv_ampdu_action(local, sta->sdata, &params);
35318b559d5SJohannes Berg 		WARN_ON_ONCE(ret);
3548147dc7fSJohannes Berg 		return 0;
35524f50a9dSJohannes Berg 	}
35624f50a9dSJohannes Berg 
3570ab33703SJohannes Berg 	if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
3580ab33703SJohannes Berg 		/* not even started yet! */
359ec034b20SJohannes Berg 		ieee80211_assign_tid_tx(sta, tid, NULL);
360cfcdbde3SJohannes Berg 		spin_unlock_bh(&sta->lock);
3610744371aSLai Jiangshan 		kfree_rcu(tid_tx, rcu_head);
3620ab33703SJohannes Berg 		return 0;
3630ab33703SJohannes Berg 	}
3640ab33703SJohannes Berg 
36524f50a9dSJohannes Berg 	set_bit(HT_AGG_STATE_STOPPING, &tid_tx->state);
36624f50a9dSJohannes Berg 
3676157ca0dSIlan Peer 	ieee80211_agg_stop_txq(sta, tid);
3686157ca0dSIlan Peer 
369cfcdbde3SJohannes Berg 	spin_unlock_bh(&sta->lock);
370cfcdbde3SJohannes Berg 
371bdcbd8e0SJohannes Berg 	ht_dbg(sta->sdata, "Tx BA session stop requested for %pM tid %u\n",
372827d42c9SJohannes Berg 	       sta->sta.addr, tid);
373827d42c9SJohannes Berg 
37444271488SJohannes Berg 	del_timer_sync(&tid_tx->addba_resp_timer);
375285fa695SNikolay Martynov 	del_timer_sync(&tid_tx->session_timer);
37644271488SJohannes Berg 
377a622ab72SJohannes Berg 	/*
378a622ab72SJohannes Berg 	 * After this packets are no longer handed right through
379a622ab72SJohannes Berg 	 * to the driver but are put onto tid_tx->pending instead,
380a622ab72SJohannes Berg 	 * with locking to ensure proper access.
381a622ab72SJohannes Berg 	 */
382a622ab72SJohannes Berg 	clear_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state);
383736708bdSVasanthakumar Thiagarajan 
3842a1e0fd1SEmmanuel Grumbach 	/*
3852a1e0fd1SEmmanuel Grumbach 	 * There might be a few packets being processed right now (on
3862a1e0fd1SEmmanuel Grumbach 	 * another CPU) that have already gotten past the aggregation
3872a1e0fd1SEmmanuel Grumbach 	 * check when it was still OPERATIONAL and consequently have
3882a1e0fd1SEmmanuel Grumbach 	 * IEEE80211_TX_CTL_AMPDU set. In that case, this code might
3892a1e0fd1SEmmanuel Grumbach 	 * call into the driver at the same time or even before the
3902a1e0fd1SEmmanuel Grumbach 	 * TX paths calls into it, which could confuse the driver.
3912a1e0fd1SEmmanuel Grumbach 	 *
3922a1e0fd1SEmmanuel Grumbach 	 * Wait for all currently running TX paths to finish before
3932a1e0fd1SEmmanuel Grumbach 	 * telling the driver. New packets will not go through since
3942a1e0fd1SEmmanuel Grumbach 	 * the aggregation session is no longer OPERATIONAL.
3952a1e0fd1SEmmanuel Grumbach 	 */
3962316380fSSara Sharon 	if (!local->in_reconfig)
3972a1e0fd1SEmmanuel Grumbach 		synchronize_net();
3982a1e0fd1SEmmanuel Grumbach 
399c82c4a80SJohannes Berg 	tid_tx->stop_initiator = reason == AGG_STOP_PEER_REQUEST ?
400c82c4a80SJohannes Berg 					WLAN_BACK_RECIPIENT :
401c82c4a80SJohannes Berg 					WLAN_BACK_INITIATOR;
402c82c4a80SJohannes Berg 	tid_tx->tx_stop = reason == AGG_STOP_LOCAL_REQUEST;
40323e6a7eaSJohannes Berg 
40450ea05efSSara Sharon 	ret = drv_ampdu_action(local, sta->sdata, &params);
40523e6a7eaSJohannes Berg 
40623e6a7eaSJohannes Berg 	/* HW shall not deny going back to legacy */
40723e6a7eaSJohannes Berg 	if (WARN_ON(ret)) {
408cd8ffc80SJohannes Berg 		/*
409cd8ffc80SJohannes Berg 		 * We may have pending packets get stuck in this case...
410cd8ffc80SJohannes Berg 		 * Not bothering with a workaround for now.
411cd8ffc80SJohannes Berg 		 */
41223e6a7eaSJohannes Berg 	}
41323e6a7eaSJohannes Berg 
4148147dc7fSJohannes Berg 	/*
4158147dc7fSJohannes Berg 	 * In the case of AGG_STOP_DESTROY_STA, the driver won't
4168147dc7fSJohannes Berg 	 * necessarily call ieee80211_stop_tx_ba_cb(), so this may
4178147dc7fSJohannes Berg 	 * seem like we can leave the tid_tx data pending forever.
4188147dc7fSJohannes Berg 	 * This is true, in a way, but "forever" is only until the
4198147dc7fSJohannes Berg 	 * station struct is actually destroyed. In the meantime,
4208147dc7fSJohannes Berg 	 * leaving it around ensures that we don't transmit packets
4218147dc7fSJohannes Berg 	 * to the driver on this TID which might confuse it.
4228147dc7fSJohannes Berg 	 */
42318b559d5SJohannes Berg 
42418b559d5SJohannes Berg 	return 0;
42523e6a7eaSJohannes Berg }
42623e6a7eaSJohannes Berg 
427b8695a8fSJohannes Berg /*
428b8695a8fSJohannes Berg  * After sending add Block Ack request we activated a timer until
429b8695a8fSJohannes Berg  * add Block Ack response will arrive from the recipient.
430b8695a8fSJohannes Berg  * If this timer expires sta_addba_resp_timer_expired will be executed.
431b8695a8fSJohannes Berg  */
sta_addba_resp_timer_expired(struct timer_list * t)4327cca2acdSKees Cook static void sta_addba_resp_timer_expired(struct timer_list *t)
433b8695a8fSJohannes Berg {
434d559e303SJohannes Berg 	struct tid_ampdu_tx *tid_tx = from_timer(tid_tx, t, addba_resp_timer);
435d559e303SJohannes Berg 	struct sta_info *sta = tid_tx->sta;
436d559e303SJohannes Berg 	u8 tid = tid_tx->tid;
43723e6a7eaSJohannes Berg 
438b8695a8fSJohannes Berg 	/* check if the TID waits for addBA response */
439d559e303SJohannes Berg 	if (test_bit(HT_AGG_STATE_RESPONSE_RECEIVED, &tid_tx->state)) {
440bdcbd8e0SJohannes Berg 		ht_dbg(sta->sdata,
441d81b0fd0SSharon Dvir 		       "timer expired on %pM tid %d not expecting addBA response\n",
4420a214d3fSJohannes Berg 		       sta->sta.addr, tid);
44323e6a7eaSJohannes Berg 		return;
444b8695a8fSJohannes Berg 	}
445b8695a8fSJohannes Berg 
4460a214d3fSJohannes Berg 	ht_dbg(sta->sdata, "addBA response timer expired on %pM tid %d\n",
4470a214d3fSJohannes Berg 	       sta->sta.addr, tid);
448b8695a8fSJohannes Berg 
44983a5cbf7SJohannes Berg 	ieee80211_stop_tx_ba_session(&sta->sta, tid);
450b8695a8fSJohannes Berg }
451b8695a8fSJohannes Berg 
ieee80211_send_addba_with_timeout(struct sta_info * sta,struct tid_ampdu_tx * tid_tx)45231d8bb4eSMordechay Goodstein static void ieee80211_send_addba_with_timeout(struct sta_info *sta,
45331d8bb4eSMordechay Goodstein 					      struct tid_ampdu_tx *tid_tx)
45431d8bb4eSMordechay Goodstein {
45531d8bb4eSMordechay Goodstein 	struct ieee80211_sub_if_data *sdata = sta->sdata;
45631d8bb4eSMordechay Goodstein 	struct ieee80211_local *local = sta->local;
45731d8bb4eSMordechay Goodstein 	u8 tid = tid_tx->tid;
45831d8bb4eSMordechay Goodstein 	u16 buf_size;
45931d8bb4eSMordechay Goodstein 
46092bf4dd3SJohannes Berg 	if (WARN_ON_ONCE(test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state) ||
46192bf4dd3SJohannes Berg 			 test_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state)))
46292bf4dd3SJohannes Berg 		return;
46392bf4dd3SJohannes Berg 
4646f2db658SJohannes Berg 	lockdep_assert_held(&sta->ampdu_mlme.mtx);
4656f2db658SJohannes Berg 
46631d8bb4eSMordechay Goodstein 	/* activate the timer for the recipient's addBA response */
46731d8bb4eSMordechay Goodstein 	mod_timer(&tid_tx->addba_resp_timer, jiffies + ADDBA_RESP_INTERVAL);
46831d8bb4eSMordechay Goodstein 	ht_dbg(sdata, "activated addBA response timer on %pM tid %d\n",
46931d8bb4eSMordechay Goodstein 	       sta->sta.addr, tid);
47031d8bb4eSMordechay Goodstein 
47131d8bb4eSMordechay Goodstein 	spin_lock_bh(&sta->lock);
47231d8bb4eSMordechay Goodstein 	sta->ampdu_mlme.last_addba_req_time[tid] = jiffies;
47331d8bb4eSMordechay Goodstein 	sta->ampdu_mlme.addba_req_num[tid]++;
47431d8bb4eSMordechay Goodstein 	spin_unlock_bh(&sta->lock);
47531d8bb4eSMordechay Goodstein 
476046d2e7cSSriram R 	if (sta->sta.deflink.he_cap.has_he) {
47731d8bb4eSMordechay Goodstein 		buf_size = local->hw.max_tx_aggregation_subframes;
47831d8bb4eSMordechay Goodstein 	} else {
47931d8bb4eSMordechay Goodstein 		/*
48031d8bb4eSMordechay Goodstein 		 * We really should use what the driver told us it will
48131d8bb4eSMordechay Goodstein 		 * transmit as the maximum, but certain APs (e.g. the
48231d8bb4eSMordechay Goodstein 		 * LinkSys WRT120N with FW v1.0.07 build 002 Jun 18 2012)
48331d8bb4eSMordechay Goodstein 		 * will crash when we use a lower number.
48431d8bb4eSMordechay Goodstein 		 */
48531d8bb4eSMordechay Goodstein 		buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
48631d8bb4eSMordechay Goodstein 	}
48731d8bb4eSMordechay Goodstein 
48831d8bb4eSMordechay Goodstein 	/* send AddBA request */
48931d8bb4eSMordechay Goodstein 	ieee80211_send_addba_request(sdata, sta->sta.addr, tid,
49073111efaSFelix Fietkau 				     tid_tx->dialog_token, tid_tx->ssn,
49131d8bb4eSMordechay Goodstein 				     buf_size, tid_tx->timeout);
4920c197f16SMordechay Goodstein 
4930c197f16SMordechay Goodstein 	WARN_ON(test_and_set_bit(HT_AGG_STATE_SENT_ADDBA, &tid_tx->state));
49431d8bb4eSMordechay Goodstein }
49531d8bb4eSMordechay Goodstein 
ieee80211_tx_ba_session_handle_start(struct sta_info * sta,int tid)49667c282c0SJohannes Berg void ieee80211_tx_ba_session_handle_start(struct sta_info *sta, int tid)
4970ab33703SJohannes Berg {
49840b275b6SJohannes Berg 	struct tid_ampdu_tx *tid_tx;
4990ab33703SJohannes Berg 	struct ieee80211_local *local = sta->local;
50086f22e7cSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
50150ea05efSSara Sharon 	struct ieee80211_ampdu_params params = {
50250ea05efSSara Sharon 		.sta = &sta->sta,
50350ea05efSSara Sharon 		.action = IEEE80211_AMPDU_TX_START,
50450ea05efSSara Sharon 		.tid = tid,
50550ea05efSSara Sharon 		.buf_size = 0,
50650ea05efSSara Sharon 		.amsdu = false,
50750ea05efSSara Sharon 		.timeout = 0,
50850ea05efSSara Sharon 	};
5090ab33703SJohannes Berg 	int ret;
5100ab33703SJohannes Berg 
51140b275b6SJohannes Berg 	tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
512cfcdbde3SJohannes Berg 
5130ab33703SJohannes Berg 	/*
51415062e6aSJohannes Berg 	 * Start queuing up packets for this aggregation session.
51515062e6aSJohannes Berg 	 * We're going to release them once the driver is OK with
51615062e6aSJohannes Berg 	 * that.
5170ab33703SJohannes Berg 	 */
5180ab33703SJohannes Berg 	clear_bit(HT_AGG_STATE_WANT_START, &tid_tx->state);
5190ab33703SJohannes Berg 
5200ab33703SJohannes Berg 	/*
52115062e6aSJohannes Berg 	 * Make sure no packets are being processed. This ensures that
52215062e6aSJohannes Berg 	 * we have a valid starting sequence number and that in-flight
52315062e6aSJohannes Berg 	 * packets have been flushed out and no packets for this TID
52415062e6aSJohannes Berg 	 * will go into the driver during the ampdu_action call.
5250ab33703SJohannes Berg 	 */
526cfcdbde3SJohannes Berg 	synchronize_net();
527cfcdbde3SJohannes Berg 
52850ea05efSSara Sharon 	params.ssn = sta->tid_seq[tid] >> 4;
52950ea05efSSara Sharon 	ret = drv_ampdu_action(local, sdata, &params);
53073111efaSFelix Fietkau 	tid_tx->ssn = params.ssn;
5310c197f16SMordechay Goodstein 	if (ret == IEEE80211_AMPDU_TX_START_DELAY_ADDBA) {
5320c197f16SMordechay Goodstein 		return;
5330c197f16SMordechay Goodstein 	} else if (ret == IEEE80211_AMPDU_TX_START_IMMEDIATE) {
5342ce113deSJohannes Berg 		/*
5352ce113deSJohannes Berg 		 * We didn't send the request yet, so don't need to check
5362ce113deSJohannes Berg 		 * here if we already got a response, just mark as driver
5372ce113deSJohannes Berg 		 * ready immediately.
5382ce113deSJohannes Berg 		 */
5392ce113deSJohannes Berg 		set_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state);
5402ce113deSJohannes Berg 	} else if (ret) {
541bdcbd8e0SJohannes Berg 		ht_dbg(sdata,
5420a214d3fSJohannes Berg 		       "BA request denied - HW unavailable for %pM tid %d\n",
5430a214d3fSJohannes Berg 		       sta->sta.addr, tid);
544cfcdbde3SJohannes Berg 		spin_lock_bh(&sta->lock);
5453a25a8c8SJohannes Berg 		ieee80211_agg_splice_packets(sdata, tid_tx, tid);
546ec034b20SJohannes Berg 		ieee80211_assign_tid_tx(sta, tid, NULL);
5473a25a8c8SJohannes Berg 		ieee80211_agg_splice_finish(sdata, tid);
548cfcdbde3SJohannes Berg 		spin_unlock_bh(&sta->lock);
549cfcdbde3SJohannes Berg 
550ba8c3d6fSFelix Fietkau 		ieee80211_agg_start_txq(sta, tid, false);
551ba8c3d6fSFelix Fietkau 
5520744371aSLai Jiangshan 		kfree_rcu(tid_tx, rcu_head);
5530ab33703SJohannes Berg 		return;
5540ab33703SJohannes Berg 	}
5550ab33703SJohannes Berg 
55631d8bb4eSMordechay Goodstein 	ieee80211_send_addba_with_timeout(sta, tid_tx);
5570ab33703SJohannes Berg }
5580ab33703SJohannes Berg 
ieee80211_refresh_tx_agg_session_timer(struct ieee80211_sta * pubsta,u16 tid)5595383bfffSRyder Lee void ieee80211_refresh_tx_agg_session_timer(struct ieee80211_sta *pubsta,
5605383bfffSRyder Lee 					    u16 tid)
5615383bfffSRyder Lee {
5625383bfffSRyder Lee 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5635383bfffSRyder Lee 	struct tid_ampdu_tx *tid_tx;
5645383bfffSRyder Lee 
5655383bfffSRyder Lee 	if (WARN_ON_ONCE(tid >= IEEE80211_NUM_TIDS))
5665383bfffSRyder Lee 		return;
5675383bfffSRyder Lee 
5685383bfffSRyder Lee 	tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
5695383bfffSRyder Lee 	if (!tid_tx)
5705383bfffSRyder Lee 		return;
5715383bfffSRyder Lee 
5725383bfffSRyder Lee 	tid_tx->last_tx = jiffies;
5735383bfffSRyder Lee }
5745383bfffSRyder Lee EXPORT_SYMBOL(ieee80211_refresh_tx_agg_session_timer);
5755383bfffSRyder Lee 
576285fa695SNikolay Martynov /*
577285fa695SNikolay Martynov  * After accepting the AddBA Response we activated a timer,
578285fa695SNikolay Martynov  * resetting it after each frame that we send.
579285fa695SNikolay Martynov  */
sta_tx_agg_session_timer_expired(struct timer_list * t)5807cca2acdSKees Cook static void sta_tx_agg_session_timer_expired(struct timer_list *t)
581285fa695SNikolay Martynov {
582d559e303SJohannes Berg 	struct tid_ampdu_tx *tid_tx = from_timer(tid_tx, t, session_timer);
583d559e303SJohannes Berg 	struct sta_info *sta = tid_tx->sta;
584d559e303SJohannes Berg 	u8 tid = tid_tx->tid;
58512d3952fSFelix Fietkau 	unsigned long timeout;
58612d3952fSFelix Fietkau 
587d559e303SJohannes Berg 	if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
58812d3952fSFelix Fietkau 		return;
5899e73dee7SJohannes Berg 	}
59012d3952fSFelix Fietkau 
59112d3952fSFelix Fietkau 	timeout = tid_tx->last_tx + TU_TO_JIFFIES(tid_tx->timeout);
59212d3952fSFelix Fietkau 	if (time_is_after_jiffies(timeout)) {
59312d3952fSFelix Fietkau 		mod_timer(&tid_tx->session_timer, timeout);
59412d3952fSFelix Fietkau 		return;
59512d3952fSFelix Fietkau 	}
596285fa695SNikolay Martynov 
5970a214d3fSJohannes Berg 	ht_dbg(sta->sdata, "tx session timer expired on %pM tid %d\n",
5987cca2acdSKees Cook 	       sta->sta.addr, tid);
599285fa695SNikolay Martynov 
6007cca2acdSKees Cook 	ieee80211_stop_tx_ba_session(&sta->sta, tid);
601285fa695SNikolay Martynov }
602285fa695SNikolay Martynov 
ieee80211_start_tx_ba_session(struct ieee80211_sta * pubsta,u16 tid,u16 timeout)603bd2ce6e4SSujith Manoharan int ieee80211_start_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid,
604bd2ce6e4SSujith Manoharan 				  u16 timeout)
605b8695a8fSJohannes Berg {
606c951ad35SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
607c951ad35SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
608c951ad35SJohannes Berg 	struct ieee80211_local *local = sdata->local;
609a622ab72SJohannes Berg 	struct tid_ampdu_tx *tid_tx;
610e4e72fb4SJohannes Berg 	int ret = 0;
611b8695a8fSJohannes Berg 
6128f9c77fcSJohannes Berg 	trace_api_start_tx_ba_session(pubsta, tid);
6138f9c77fcSJohannes Berg 
614b6da911bSLiad Kaufman 	if (WARN(sta->reserved_tid == tid,
615b6da911bSLiad Kaufman 		 "Requested to start BA session on reserved tid=%d", tid))
616b6da911bSLiad Kaufman 		return -EINVAL;
617b6da911bSLiad Kaufman 
618046d2e7cSSriram R 	if (!pubsta->deflink.ht_cap.ht_supported &&
619*a53c2d84SZong-Zhe Yang 	    !pubsta->deflink.vht_cap.vht_supported &&
620*a53c2d84SZong-Zhe Yang 	    !pubsta->deflink.he_cap.has_he &&
621*a53c2d84SZong-Zhe Yang 	    !pubsta->deflink.eht_cap.has_eht)
6228f9c77fcSJohannes Berg 		return -EINVAL;
623b5878a2dSJohannes Berg 
62450c16e22SChaitanya 	if (WARN_ON_ONCE(!local->ops->ampdu_action))
62523e6a7eaSJohannes Berg 		return -EINVAL;
62623e6a7eaSJohannes Berg 
6275a306f58SJohannes Berg 	if ((tid >= IEEE80211_NUM_TIDS) ||
62830686bf7SJohannes Berg 	    !ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) ||
62930686bf7SJohannes Berg 	    ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW))
630b8695a8fSJohannes Berg 		return -EINVAL;
631b8695a8fSJohannes Berg 
63285d5313eSJohannes Berg 	if (WARN_ON(tid >= IEEE80211_FIRST_TSPEC_TSID))
63385d5313eSJohannes Berg 		return -EINVAL;
63485d5313eSJohannes Berg 
635bdcbd8e0SJohannes Berg 	ht_dbg(sdata, "Open BA session requested for %pM tid %u\n",
636c951ad35SJohannes Berg 	       pubsta->addr, tid);
637b8695a8fSJohannes Berg 
638c951ad35SJohannes Berg 	if (sdata->vif.type != NL80211_IFTYPE_STATION &&
639ae2772b3SThomas Pedersen 	    sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
640c951ad35SJohannes Berg 	    sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
64113c40c54SAlexander Simon 	    sdata->vif.type != NL80211_IFTYPE_AP &&
64213c40c54SAlexander Simon 	    sdata->vif.type != NL80211_IFTYPE_ADHOC)
643c951ad35SJohannes Berg 		return -EINVAL;
6448abd3f9bSJohannes Berg 
645c2c98fdeSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_BLOCK_BA)) {
646bdcbd8e0SJohannes Berg 		ht_dbg(sdata,
6470a214d3fSJohannes Berg 		       "BA sessions blocked - Denying BA session request %pM tid %d\n",
6480a214d3fSJohannes Berg 		       sta->sta.addr, tid);
649c951ad35SJohannes Berg 		return -EINVAL;
650722f069aSSujith 	}
651722f069aSSujith 
652a6bce782SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP) &&
653a6bce782SJohannes Berg 	    !test_sta_flag(sta, WLAN_STA_AUTHORIZED)) {
654a6bce782SJohannes Berg 		ht_dbg(sdata,
655a6bce782SJohannes Berg 		       "MFP STA not authorized - deny BA session request %pM tid %d\n",
656a6bce782SJohannes Berg 		       sta->sta.addr, tid);
657a6bce782SJohannes Berg 		return -EINVAL;
658a6bce782SJohannes Berg 	}
659a6bce782SJohannes Berg 
660ff3cc5f4SSimon Wunderlich 	/*
661ff3cc5f4SSimon Wunderlich 	 * 802.11n-2009 11.5.1.1: If the initiating STA is an HT STA, is a
662ff3cc5f4SSimon Wunderlich 	 * member of an IBSS, and has no other existing Block Ack agreement
663ff3cc5f4SSimon Wunderlich 	 * with the recipient STA, then the initiating STA shall transmit a
664ff3cc5f4SSimon Wunderlich 	 * Probe Request frame to the recipient STA and shall not transmit an
665ff3cc5f4SSimon Wunderlich 	 * ADDBA Request frame unless it receives a Probe Response frame
666ff3cc5f4SSimon Wunderlich 	 * from the recipient within dot11ADDBAFailureTimeout.
667ff3cc5f4SSimon Wunderlich 	 *
668ff3cc5f4SSimon Wunderlich 	 * The probe request mechanism for ADDBA is currently not implemented,
669ff3cc5f4SSimon Wunderlich 	 * but we only build up Block Ack session with HT STAs. This information
670ff3cc5f4SSimon Wunderlich 	 * is set when we receive a bss info from a probe response or a beacon.
671ff3cc5f4SSimon Wunderlich 	 */
672ff3cc5f4SSimon Wunderlich 	if (sta->sdata->vif.type == NL80211_IFTYPE_ADHOC &&
673046d2e7cSSriram R 	    !sta->sta.deflink.ht_cap.ht_supported) {
674bdcbd8e0SJohannes Berg 		ht_dbg(sdata,
675bdcbd8e0SJohannes Berg 		       "BA request denied - IBSS STA %pM does not advertise HT support\n",
676f0d23208SJoe Perches 		       pubsta->addr);
677ff3cc5f4SSimon Wunderlich 		return -EINVAL;
678ff3cc5f4SSimon Wunderlich 	}
679ff3cc5f4SSimon Wunderlich 
680b8695a8fSJohannes Berg 	spin_lock_bh(&sta->lock);
681b8695a8fSJohannes Berg 
682b8695a8fSJohannes Berg 	/* we have tried too many times, receiver does not want A-MPDU */
683b8695a8fSJohannes Berg 	if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_MAX_RETRIES) {
684b8695a8fSJohannes Berg 		ret = -EBUSY;
685b8695a8fSJohannes Berg 		goto err_unlock_sta;
686b8695a8fSJohannes Berg 	}
687b8695a8fSJohannes Berg 
68884381b4eSNikolay Martynov 	/*
68984381b4eSNikolay Martynov 	 * if we have tried more than HT_AGG_BURST_RETRIES times we
69084381b4eSNikolay Martynov 	 * will spread our requests in time to avoid stalling connection
69184381b4eSNikolay Martynov 	 * for too long
69284381b4eSNikolay Martynov 	 */
69384381b4eSNikolay Martynov 	if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_BURST_RETRIES &&
69484381b4eSNikolay Martynov 	    time_before(jiffies, sta->ampdu_mlme.last_addba_req_time[tid] +
69584381b4eSNikolay Martynov 			HT_AGG_RETRIES_PERIOD)) {
696bdcbd8e0SJohannes Berg 		ht_dbg(sdata,
697d81b0fd0SSharon Dvir 		       "BA request denied - %d failed requests on %pM tid %u\n",
6980a214d3fSJohannes Berg 		       sta->ampdu_mlme.addba_req_num[tid], sta->sta.addr, tid);
69984381b4eSNikolay Martynov 		ret = -EBUSY;
70084381b4eSNikolay Martynov 		goto err_unlock_sta;
70184381b4eSNikolay Martynov 	}
70284381b4eSNikolay Martynov 
70340b275b6SJohannes Berg 	tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
704b8695a8fSJohannes Berg 	/* check if the TID is not in aggregation flow already */
705ec034b20SJohannes Berg 	if (tid_tx || sta->ampdu_mlme.tid_start_tx[tid]) {
706bdcbd8e0SJohannes Berg 		ht_dbg(sdata,
7070a214d3fSJohannes Berg 		       "BA request denied - session is not idle on %pM tid %u\n",
7080a214d3fSJohannes Berg 		       sta->sta.addr, tid);
709b8695a8fSJohannes Berg 		ret = -EAGAIN;
710b8695a8fSJohannes Berg 		goto err_unlock_sta;
711b8695a8fSJohannes Berg 	}
712b8695a8fSJohannes Berg 
713b8695a8fSJohannes Berg 	/* prepare A-MPDU MLME for Tx aggregation */
714a622ab72SJohannes Berg 	tid_tx = kzalloc(sizeof(struct tid_ampdu_tx), GFP_ATOMIC);
715a622ab72SJohannes Berg 	if (!tid_tx) {
716b8695a8fSJohannes Berg 		ret = -ENOMEM;
7170ab33703SJohannes Berg 		goto err_unlock_sta;
718b8695a8fSJohannes Berg 	}
71996f5e66eSJohannes Berg 
720a622ab72SJohannes Berg 	skb_queue_head_init(&tid_tx->pending);
7210ab33703SJohannes Berg 	__set_bit(HT_AGG_STATE_WANT_START, &tid_tx->state);
722cd8ffc80SJohannes Berg 
723bd2ce6e4SSujith Manoharan 	tid_tx->timeout = timeout;
7247cca2acdSKees Cook 	tid_tx->sta = sta;
7257cca2acdSKees Cook 	tid_tx->tid = tid;
726bd2ce6e4SSujith Manoharan 
727285fa695SNikolay Martynov 	/* response timer */
7287cca2acdSKees Cook 	timer_setup(&tid_tx->addba_resp_timer, sta_addba_resp_timer_expired, 0);
729b8695a8fSJohannes Berg 
730285fa695SNikolay Martynov 	/* tx timer */
7317cca2acdSKees Cook 	timer_setup(&tid_tx->session_timer,
7327cca2acdSKees Cook 		    sta_tx_agg_session_timer_expired, TIMER_DEFERRABLE);
733285fa695SNikolay Martynov 
7340ab33703SJohannes Berg 	/* assign a dialog token */
735b8695a8fSJohannes Berg 	sta->ampdu_mlme.dialog_token_allocator++;
736a622ab72SJohannes Berg 	tid_tx->dialog_token = sta->ampdu_mlme.dialog_token_allocator;
737a622ab72SJohannes Berg 
738ec034b20SJohannes Berg 	/*
739ec034b20SJohannes Berg 	 * Finally, assign it to the start array; the work item will
740ec034b20SJohannes Berg 	 * collect it and move it to the normal array.
741ec034b20SJohannes Berg 	 */
742ec034b20SJohannes Berg 	sta->ampdu_mlme.tid_start_tx[tid] = tid_tx;
743b8695a8fSJohannes Berg 
7440ab33703SJohannes Berg 	ieee80211_queue_work(&local->hw, &sta->ampdu_mlme.work);
74551a0d38dSJohannes Berg 
7460ab33703SJohannes Berg 	/* this flow continues off the work */
747b8695a8fSJohannes Berg  err_unlock_sta:
748b8695a8fSJohannes Berg 	spin_unlock_bh(&sta->lock);
749b8695a8fSJohannes Berg 	return ret;
750b8695a8fSJohannes Berg }
751b8695a8fSJohannes Berg EXPORT_SYMBOL(ieee80211_start_tx_ba_session);
752b8695a8fSJohannes Berg 
ieee80211_agg_tx_operational(struct ieee80211_local * local,struct sta_info * sta,u16 tid)753b1720231SJohannes Berg static void ieee80211_agg_tx_operational(struct ieee80211_local *local,
754b1720231SJohannes Berg 					 struct sta_info *sta, u16 tid)
755b1720231SJohannes Berg {
75640b275b6SJohannes Berg 	struct tid_ampdu_tx *tid_tx;
75750ea05efSSara Sharon 	struct ieee80211_ampdu_params params = {
75850ea05efSSara Sharon 		.sta = &sta->sta,
75950ea05efSSara Sharon 		.action = IEEE80211_AMPDU_TX_OPERATIONAL,
76050ea05efSSara Sharon 		.tid = tid,
76150ea05efSSara Sharon 		.timeout = 0,
76250ea05efSSara Sharon 		.ssn = 0,
76350ea05efSSara Sharon 	};
76440b275b6SJohannes Berg 
765cfcdbde3SJohannes Berg 	lockdep_assert_held(&sta->ampdu_mlme.mtx);
766a622ab72SJohannes Berg 
76740b275b6SJohannes Berg 	tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
76850ea05efSSara Sharon 	params.buf_size = tid_tx->buf_size;
76950ea05efSSara Sharon 	params.amsdu = tid_tx->amsdu;
77040b275b6SJohannes Berg 
7710a214d3fSJohannes Berg 	ht_dbg(sta->sdata, "Aggregation is on for %pM tid %d\n",
7720a214d3fSJohannes Berg 	       sta->sta.addr, tid);
773b1720231SJohannes Berg 
77450ea05efSSara Sharon 	drv_ampdu_action(local, sta->sdata, &params);
775cfcdbde3SJohannes Berg 
776cfcdbde3SJohannes Berg 	/*
777cfcdbde3SJohannes Berg 	 * synchronize with TX path, while splicing the TX path
778cfcdbde3SJohannes Berg 	 * should block so it won't put more packets onto pending.
779cfcdbde3SJohannes Berg 	 */
780cfcdbde3SJohannes Berg 	spin_lock_bh(&sta->lock);
781cfcdbde3SJohannes Berg 
7823a25a8c8SJohannes Berg 	ieee80211_agg_splice_packets(sta->sdata, tid_tx, tid);
783b1720231SJohannes Berg 	/*
784a622ab72SJohannes Berg 	 * Now mark as operational. This will be visible
785a622ab72SJohannes Berg 	 * in the TX path, and lets it go lock-free in
786a622ab72SJohannes Berg 	 * the common case.
787b1720231SJohannes Berg 	 */
78840b275b6SJohannes Berg 	set_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state);
7893a25a8c8SJohannes Berg 	ieee80211_agg_splice_finish(sta->sdata, tid);
790b1720231SJohannes Berg 
791cfcdbde3SJohannes Berg 	spin_unlock_bh(&sta->lock);
792ba8c3d6fSFelix Fietkau 
793ba8c3d6fSFelix Fietkau 	ieee80211_agg_start_txq(sta, tid, true);
794b1720231SJohannes Berg }
795b1720231SJohannes Berg 
ieee80211_start_tx_ba_cb(struct sta_info * sta,int tid,struct tid_ampdu_tx * tid_tx)7967a7c0a64SJohannes Berg void ieee80211_start_tx_ba_cb(struct sta_info *sta, int tid,
7977a7c0a64SJohannes Berg 			      struct tid_ampdu_tx *tid_tx)
7987a7c0a64SJohannes Berg {
7997a7c0a64SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
8007a7c0a64SJohannes Berg 	struct ieee80211_local *local = sdata->local;
8017a7c0a64SJohannes Berg 
8026f2db658SJohannes Berg 	lockdep_assert_held(&sta->ampdu_mlme.mtx);
8036f2db658SJohannes Berg 
8047a7c0a64SJohannes Berg 	if (WARN_ON(test_and_set_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state)))
8057a7c0a64SJohannes Berg 		return;
8067a7c0a64SJohannes Berg 
80792bf4dd3SJohannes Berg 	if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state) ||
80892bf4dd3SJohannes Berg 	    test_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state))
80992bf4dd3SJohannes Berg 		return;
81092bf4dd3SJohannes Berg 
8110c197f16SMordechay Goodstein 	if (!test_bit(HT_AGG_STATE_SENT_ADDBA, &tid_tx->state)) {
8120c197f16SMordechay Goodstein 		ieee80211_send_addba_with_timeout(sta, tid_tx);
8130c197f16SMordechay Goodstein 		/* RESPONSE_RECEIVED state whould trigger the flow again */
8140c197f16SMordechay Goodstein 		return;
8150c197f16SMordechay Goodstein 	}
8160c197f16SMordechay Goodstein 
8177a7c0a64SJohannes Berg 	if (test_bit(HT_AGG_STATE_RESPONSE_RECEIVED, &tid_tx->state))
8187a7c0a64SJohannes Berg 		ieee80211_agg_tx_operational(local, sta, tid);
8197a7c0a64SJohannes Berg }
8207a7c0a64SJohannes Berg 
8217a7c0a64SJohannes Berg static struct tid_ampdu_tx *
ieee80211_lookup_tid_tx(struct ieee80211_sub_if_data * sdata,const u8 * ra,u16 tid,struct sta_info ** sta)8227a7c0a64SJohannes Berg ieee80211_lookup_tid_tx(struct ieee80211_sub_if_data *sdata,
8237a7c0a64SJohannes Berg 			const u8 *ra, u16 tid, struct sta_info **sta)
8247a7c0a64SJohannes Berg {
8257a7c0a64SJohannes Berg 	struct tid_ampdu_tx *tid_tx;
8267a7c0a64SJohannes Berg 
8277a7c0a64SJohannes Berg 	if (tid >= IEEE80211_NUM_TIDS) {
8287a7c0a64SJohannes Berg 		ht_dbg(sdata, "Bad TID value: tid = %d (>= %d)\n",
8297a7c0a64SJohannes Berg 		       tid, IEEE80211_NUM_TIDS);
8307a7c0a64SJohannes Berg 		return NULL;
8317a7c0a64SJohannes Berg 	}
8327a7c0a64SJohannes Berg 
8337a7c0a64SJohannes Berg 	*sta = sta_info_get_bss(sdata, ra);
8347a7c0a64SJohannes Berg 	if (!*sta) {
8357a7c0a64SJohannes Berg 		ht_dbg(sdata, "Could not find station: %pM\n", ra);
8367a7c0a64SJohannes Berg 		return NULL;
8377a7c0a64SJohannes Berg 	}
8387a7c0a64SJohannes Berg 
8397a7c0a64SJohannes Berg 	tid_tx = rcu_dereference((*sta)->ampdu_mlme.tid_tx[tid]);
8407a7c0a64SJohannes Berg 
8417a7c0a64SJohannes Berg 	if (WARN_ON(!tid_tx))
8427a7c0a64SJohannes Berg 		ht_dbg(sdata, "addBA was not requested!\n");
8437a7c0a64SJohannes Berg 
8447a7c0a64SJohannes Berg 	return tid_tx;
8457a7c0a64SJohannes Berg }
8467a7c0a64SJohannes Berg 
ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_vif * vif,const u8 * ra,u16 tid)8477a7c0a64SJohannes Berg void ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
8487a7c0a64SJohannes Berg 				      const u8 *ra, u16 tid)
849b8695a8fSJohannes Berg {
850c951ad35SJohannes Berg 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
851c951ad35SJohannes Berg 	struct ieee80211_local *local = sdata->local;
852b8695a8fSJohannes Berg 	struct sta_info *sta;
853a622ab72SJohannes Berg 	struct tid_ampdu_tx *tid_tx;
854b8695a8fSJohannes Berg 
855b5878a2dSJohannes Berg 	trace_api_start_tx_ba_cb(sdata, ra, tid);
856b5878a2dSJohannes Berg 
8577a7c0a64SJohannes Berg 	rcu_read_lock();
8587a7c0a64SJohannes Berg 	tid_tx = ieee80211_lookup_tid_tx(sdata, ra, tid, &sta);
8597a7c0a64SJohannes Berg 	if (!tid_tx)
8607a7c0a64SJohannes Berg 		goto out;
861b8695a8fSJohannes Berg 
8627a7c0a64SJohannes Berg 	set_bit(HT_AGG_STATE_START_CB, &tid_tx->state);
8637a7c0a64SJohannes Berg 	ieee80211_queue_work(&local->hw, &sta->ampdu_mlme.work);
8647a7c0a64SJohannes Berg  out:
8657a7c0a64SJohannes Berg 	rcu_read_unlock();
86686ab6c5aSJohannes Berg }
86786ab6c5aSJohannes Berg EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);
86886ab6c5aSJohannes Berg 
__ieee80211_stop_tx_ba_session(struct sta_info * sta,u16 tid,enum ieee80211_agg_stop_reason reason)869849b7967SJohannes Berg int __ieee80211_stop_tx_ba_session(struct sta_info *sta, u16 tid,
870c82c4a80SJohannes Berg 				   enum ieee80211_agg_stop_reason reason)
871849b7967SJohannes Berg {
872849b7967SJohannes Berg 	int ret;
873849b7967SJohannes Berg 
874cfcdbde3SJohannes Berg 	mutex_lock(&sta->ampdu_mlme.mtx);
875849b7967SJohannes Berg 
876c82c4a80SJohannes Berg 	ret = ___ieee80211_stop_tx_ba_session(sta, tid, reason);
877849b7967SJohannes Berg 
878cfcdbde3SJohannes Berg 	mutex_unlock(&sta->ampdu_mlme.mtx);
879cfcdbde3SJohannes Berg 
880849b7967SJohannes Berg 	return ret;
881849b7967SJohannes Berg }
882b8695a8fSJohannes Berg 
ieee80211_stop_tx_ba_session(struct ieee80211_sta * pubsta,u16 tid)8836a8579d0SJohannes Berg int ieee80211_stop_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid)
884b8695a8fSJohannes Berg {
885c951ad35SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
886c951ad35SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
887c951ad35SJohannes Berg 	struct ieee80211_local *local = sdata->local;
8880ab33703SJohannes Berg 	struct tid_ampdu_tx *tid_tx;
8890ab33703SJohannes Berg 	int ret = 0;
890b8695a8fSJohannes Berg 
8916a8579d0SJohannes Berg 	trace_api_stop_tx_ba_session(pubsta, tid);
892b5878a2dSJohannes Berg 
8934253119aSJohannes Berg 	if (!local->ops->ampdu_action)
89423e6a7eaSJohannes Berg 		return -EINVAL;
89523e6a7eaSJohannes Berg 
8965a306f58SJohannes Berg 	if (tid >= IEEE80211_NUM_TIDS)
897b8695a8fSJohannes Berg 		return -EINVAL;
898b8695a8fSJohannes Berg 
8990ab33703SJohannes Berg 	spin_lock_bh(&sta->lock);
90040b275b6SJohannes Berg 	tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
9010ab33703SJohannes Berg 
9020ab33703SJohannes Berg 	if (!tid_tx) {
9030ab33703SJohannes Berg 		ret = -ENOENT;
9040ab33703SJohannes Berg 		goto unlock;
9050ab33703SJohannes Berg 	}
9060ab33703SJohannes Berg 
907b6da911bSLiad Kaufman 	WARN(sta->reserved_tid == tid,
908b6da911bSLiad Kaufman 	     "Requested to stop BA session on reserved tid=%d", tid);
909b6da911bSLiad Kaufman 
9100ab33703SJohannes Berg 	if (test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
9110ab33703SJohannes Berg 		/* already in progress stopping it */
9120ab33703SJohannes Berg 		ret = 0;
9130ab33703SJohannes Berg 		goto unlock;
9140ab33703SJohannes Berg 	}
9150ab33703SJohannes Berg 
9160ab33703SJohannes Berg 	set_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state);
9170ab33703SJohannes Berg 	ieee80211_queue_work(&local->hw, &sta->ampdu_mlme.work);
9180ab33703SJohannes Berg 
9190ab33703SJohannes Berg  unlock:
9200ab33703SJohannes Berg 	spin_unlock_bh(&sta->lock);
9210ab33703SJohannes Berg 	return ret;
922b8695a8fSJohannes Berg }
923b8695a8fSJohannes Berg EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);
924b8695a8fSJohannes Berg 
ieee80211_stop_tx_ba_cb(struct sta_info * sta,int tid,struct tid_ampdu_tx * tid_tx)9257a7c0a64SJohannes Berg void ieee80211_stop_tx_ba_cb(struct sta_info *sta, int tid,
9267a7c0a64SJohannes Berg 			     struct tid_ampdu_tx *tid_tx)
927b8695a8fSJohannes Berg {
9287a7c0a64SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
9292c158887SJohannes Berg 	bool send_delba = false;
93006c41bdaSJohannes Berg 	bool start_txq = false;
931b8695a8fSJohannes Berg 
9327a7c0a64SJohannes Berg 	ht_dbg(sdata, "Stopping Tx BA session for %pM tid %d\n",
9337a7c0a64SJohannes Berg 	       sta->sta.addr, tid);
934b5878a2dSJohannes Berg 
935a622ab72SJohannes Berg 	spin_lock_bh(&sta->lock);
936a622ab72SJohannes Berg 
9377a7c0a64SJohannes Berg 	if (!test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
9380a214d3fSJohannes Berg 		ht_dbg(sdata,
9390a214d3fSJohannes Berg 		       "unexpected callback to A-MPDU stop for %pM tid %d\n",
9400a214d3fSJohannes Berg 		       sta->sta.addr, tid);
941cfcdbde3SJohannes Berg 		goto unlock_sta;
942b8695a8fSJohannes Berg 	}
943b8695a8fSJohannes Berg 
94453f73c09SJohannes Berg 	if (tid_tx->stop_initiator == WLAN_BACK_INITIATOR && tid_tx->tx_stop)
9452c158887SJohannes Berg 		send_delba = true;
946b8695a8fSJohannes Berg 
947faec12eeSJohannes Berg 	ieee80211_remove_tid_tx(sta, tid);
94806c41bdaSJohannes Berg 	start_txq = true;
949b8695a8fSJohannes Berg 
950cfcdbde3SJohannes Berg  unlock_sta:
951a622ab72SJohannes Berg 	spin_unlock_bh(&sta->lock);
9522c158887SJohannes Berg 
95306c41bdaSJohannes Berg 	if (start_txq)
95406c41bdaSJohannes Berg 		ieee80211_agg_start_txq(sta, tid, false);
95506c41bdaSJohannes Berg 
9562c158887SJohannes Berg 	if (send_delba)
9577a7c0a64SJohannes Berg 		ieee80211_send_delba(sdata, sta->sta.addr, tid,
9582c158887SJohannes Berg 			WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);
959b8695a8fSJohannes Berg }
960b8695a8fSJohannes Berg 
ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_vif * vif,const u8 * ra,u16 tid)961c951ad35SJohannes Berg void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_vif *vif,
962b8695a8fSJohannes Berg 				     const u8 *ra, u16 tid)
963b8695a8fSJohannes Berg {
964c951ad35SJohannes Berg 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
965c951ad35SJohannes Berg 	struct ieee80211_local *local = sdata->local;
9667a7c0a64SJohannes Berg 	struct sta_info *sta;
9677a7c0a64SJohannes Berg 	struct tid_ampdu_tx *tid_tx;
968b8695a8fSJohannes Berg 
9697a7c0a64SJohannes Berg 	trace_api_stop_tx_ba_cb(sdata, ra, tid);
970d15b8459SJoe Perches 
9717a7c0a64SJohannes Berg 	rcu_read_lock();
9727a7c0a64SJohannes Berg 	tid_tx = ieee80211_lookup_tid_tx(sdata, ra, tid, &sta);
9737a7c0a64SJohannes Berg 	if (!tid_tx)
9747a7c0a64SJohannes Berg 		goto out;
975b8695a8fSJohannes Berg 
9767a7c0a64SJohannes Berg 	set_bit(HT_AGG_STATE_STOP_CB, &tid_tx->state);
9777a7c0a64SJohannes Berg 	ieee80211_queue_work(&local->hw, &sta->ampdu_mlme.work);
9787a7c0a64SJohannes Berg  out:
9797a7c0a64SJohannes Berg 	rcu_read_unlock();
980b8695a8fSJohannes Berg }
981b8695a8fSJohannes Berg EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);
982b8695a8fSJohannes Berg 
98386ab6c5aSJohannes Berg 
ieee80211_process_addba_resp(struct ieee80211_local * local,struct sta_info * sta,struct ieee80211_mgmt * mgmt,size_t len)984b8695a8fSJohannes Berg void ieee80211_process_addba_resp(struct ieee80211_local *local,
985b8695a8fSJohannes Berg 				  struct sta_info *sta,
986b8695a8fSJohannes Berg 				  struct ieee80211_mgmt *mgmt,
987b8695a8fSJohannes Berg 				  size_t len)
988b8695a8fSJohannes Berg {
989a622ab72SJohannes Berg 	struct tid_ampdu_tx *tid_tx;
9906e0456b5SFelix Fietkau 	struct ieee80211_txq *txq;
99141cbb0f5SLuca Coelho 	u16 capab, tid, buf_size;
992e3abc8ffSEmmanuel Grumbach 	bool amsdu;
993b8695a8fSJohannes Berg 
994b8695a8fSJohannes Berg 	capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
995e3abc8ffSEmmanuel Grumbach 	amsdu = capab & IEEE80211_ADDBA_PARAM_AMSDU_MASK;
996db8ebd06SJohannes Berg 	tid = u16_get_bits(capab, IEEE80211_ADDBA_PARAM_TID_MASK);
997db8ebd06SJohannes Berg 	buf_size = u16_get_bits(capab, IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK);
998ac062197SGregory Greenman 	buf_size = min(buf_size, local->hw.max_tx_aggregation_subframes);
999b8695a8fSJohannes Berg 
10006e0456b5SFelix Fietkau 	txq = sta->sta.txq[tid];
10016e0456b5SFelix Fietkau 	if (!amsdu && txq)
10026e0456b5SFelix Fietkau 		set_bit(IEEE80211_TXQ_NO_AMSDU, &to_txq_info(txq)->flags);
10036e0456b5SFelix Fietkau 
1004cfcdbde3SJohannes Berg 	mutex_lock(&sta->ampdu_mlme.mtx);
1005b8695a8fSJohannes Berg 
100640b275b6SJohannes Berg 	tid_tx = rcu_dereference_protected_tid_tx(sta, tid);
1007a622ab72SJohannes Berg 	if (!tid_tx)
10088ade0082SJohannes Berg 		goto out;
1009b8695a8fSJohannes Berg 
1010a622ab72SJohannes Berg 	if (mgmt->u.action.u.addba_resp.dialog_token != tid_tx->dialog_token) {
10110a214d3fSJohannes Berg 		ht_dbg(sta->sdata, "wrong addBA response token, %pM tid %d\n",
10120a214d3fSJohannes Berg 		       sta->sta.addr, tid);
10138ade0082SJohannes Berg 		goto out;
1014b8695a8fSJohannes Berg 	}
1015b8695a8fSJohannes Berg 
1016d305a655SNikolay Martynov 	del_timer_sync(&tid_tx->addba_resp_timer);
10178ade0082SJohannes Berg 
10180a214d3fSJohannes Berg 	ht_dbg(sta->sdata, "switched off addBA timer for %pM tid %d\n",
10190a214d3fSJohannes Berg 	       sta->sta.addr, tid);
1020d305a655SNikolay Martynov 
1021d305a655SNikolay Martynov 	/*
1022d305a655SNikolay Martynov 	 * addba_resp_timer may have fired before we got here, and
1023d305a655SNikolay Martynov 	 * caused WANT_STOP to be set. If the stop then was already
1024d305a655SNikolay Martynov 	 * processed further, STOPPING might be set.
1025d305a655SNikolay Martynov 	 */
1026d305a655SNikolay Martynov 	if (test_bit(HT_AGG_STATE_WANT_STOP, &tid_tx->state) ||
1027d305a655SNikolay Martynov 	    test_bit(HT_AGG_STATE_STOPPING, &tid_tx->state)) {
1028bdcbd8e0SJohannes Berg 		ht_dbg(sta->sdata,
10290a214d3fSJohannes Berg 		       "got addBA resp for %pM tid %d but we already gave up\n",
10300a214d3fSJohannes Berg 		       sta->sta.addr, tid);
1031d305a655SNikolay Martynov 		goto out;
1032d305a655SNikolay Martynov 	}
1033d305a655SNikolay Martynov 
10340b01f030SJohannes Berg 	/*
10350b01f030SJohannes Berg 	 * IEEE 802.11-2007 7.3.1.14:
10360b01f030SJohannes Berg 	 * In an ADDBA Response frame, when the Status Code field
10370b01f030SJohannes Berg 	 * is set to 0, the Buffer Size subfield is set to a value
10380b01f030SJohannes Berg 	 * of at least 1.
10390b01f030SJohannes Berg 	 */
10403ca97880SHelmut Schaa 	if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
10413ca97880SHelmut Schaa 			== WLAN_STATUS_SUCCESS && buf_size) {
1042a622ab72SJohannes Berg 		if (test_and_set_bit(HT_AGG_STATE_RESPONSE_RECEIVED,
1043a622ab72SJohannes Berg 				     &tid_tx->state)) {
1044a622ab72SJohannes Berg 			/* ignore duplicate response */
1045a622ab72SJohannes Berg 			goto out;
1046a622ab72SJohannes Berg 		}
1047b8695a8fSJohannes Berg 
10480b01f030SJohannes Berg 		tid_tx->buf_size = buf_size;
1049e3abc8ffSEmmanuel Grumbach 		tid_tx->amsdu = amsdu;
10500b01f030SJohannes Berg 
1051a622ab72SJohannes Berg 		if (test_bit(HT_AGG_STATE_DRV_READY, &tid_tx->state))
1052b1720231SJohannes Berg 			ieee80211_agg_tx_operational(local, sta, tid);
1053b8695a8fSJohannes Berg 
1054b1720231SJohannes Berg 		sta->ampdu_mlme.addba_req_num[tid] = 0;
1055285fa695SNikolay Martynov 
1056914eac24SSara Sharon 		tid_tx->timeout =
1057914eac24SSara Sharon 			le16_to_cpu(mgmt->u.action.u.addba_resp.timeout);
1058914eac24SSara Sharon 
105912d3952fSFelix Fietkau 		if (tid_tx->timeout) {
1060285fa695SNikolay Martynov 			mod_timer(&tid_tx->session_timer,
1061285fa695SNikolay Martynov 				  TU_TO_EXP_TIME(tid_tx->timeout));
106212d3952fSFelix Fietkau 			tid_tx->last_tx = jiffies;
106312d3952fSFelix Fietkau 		}
1064285fa695SNikolay Martynov 
1065b8695a8fSJohannes Berg 	} else {
1066c82c4a80SJohannes Berg 		___ieee80211_stop_tx_ba_session(sta, tid, AGG_STOP_DECLINED);
1067b8695a8fSJohannes Berg 	}
10682171abc5SJohannes Berg 
10692171abc5SJohannes Berg  out:
1070cfcdbde3SJohannes Berg 	mutex_unlock(&sta->ampdu_mlme.mtx);
1071b8695a8fSJohannes Berg }
1072