1e705c121SKalle Valo /******************************************************************************
2e705c121SKalle Valo  *
3e705c121SKalle Valo  * This file is provided under a dual BSD/GPLv2 license.  When using or
4e705c121SKalle Valo  * redistributing this file, you may do so under either license.
5e705c121SKalle Valo  *
6e705c121SKalle Valo  * GPL LICENSE SUMMARY
7e705c121SKalle Valo  *
8e705c121SKalle Valo  * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
9e705c121SKalle Valo  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10854c5705SSara Sharon  * Copyright(c) 2016 Intel Deutschland GmbH
11e705c121SKalle Valo  *
12e705c121SKalle Valo  * This program is free software; you can redistribute it and/or modify
13e705c121SKalle Valo  * it under the terms of version 2 of the GNU General Public License as
14e705c121SKalle Valo  * published by the Free Software Foundation.
15e705c121SKalle Valo  *
16e705c121SKalle Valo  * This program is distributed in the hope that it will be useful, but
17e705c121SKalle Valo  * WITHOUT ANY WARRANTY; without even the implied warranty of
18e705c121SKalle Valo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19e705c121SKalle Valo  * General Public License for more details.
20e705c121SKalle Valo  *
21e705c121SKalle Valo  * You should have received a copy of the GNU General Public License
22e705c121SKalle Valo  * along with this program; if not, write to the Free Software
23e705c121SKalle Valo  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24e705c121SKalle Valo  * USA
25e705c121SKalle Valo  *
26e705c121SKalle Valo  * The full GNU General Public License is included in this distribution
27e705c121SKalle Valo  * in the file called COPYING.
28e705c121SKalle Valo  *
29e705c121SKalle Valo  * Contact Information:
30cb2f8277SEmmanuel Grumbach  *  Intel Linux Wireless <linuxwifi@intel.com>
31e705c121SKalle Valo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32e705c121SKalle Valo  *
33e705c121SKalle Valo  * BSD LICENSE
34e705c121SKalle Valo  *
35e705c121SKalle Valo  * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
36e705c121SKalle Valo  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
37854c5705SSara Sharon  * Copyright(c) 2016 Intel Deutschland GmbH
38e705c121SKalle Valo  * All rights reserved.
39e705c121SKalle Valo  *
40e705c121SKalle Valo  * Redistribution and use in source and binary forms, with or without
41e705c121SKalle Valo  * modification, are permitted provided that the following conditions
42e705c121SKalle Valo  * are met:
43e705c121SKalle Valo  *
44e705c121SKalle Valo  *  * Redistributions of source code must retain the above copyright
45e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer.
46e705c121SKalle Valo  *  * Redistributions in binary form must reproduce the above copyright
47e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer in
48e705c121SKalle Valo  *    the documentation and/or other materials provided with the
49e705c121SKalle Valo  *    distribution.
50e705c121SKalle Valo  *  * Neither the name Intel Corporation nor the names of its
51e705c121SKalle Valo  *    contributors may be used to endorse or promote products derived
52e705c121SKalle Valo  *    from this software without specific prior written permission.
53e705c121SKalle Valo  *
54e705c121SKalle Valo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55e705c121SKalle Valo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56e705c121SKalle Valo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57e705c121SKalle Valo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58e705c121SKalle Valo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59e705c121SKalle Valo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60e705c121SKalle Valo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61e705c121SKalle Valo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62e705c121SKalle Valo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63e705c121SKalle Valo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64e705c121SKalle Valo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65e705c121SKalle Valo  *
66e705c121SKalle Valo  *****************************************************************************/
67e705c121SKalle Valo #include <net/mac80211.h>
68e705c121SKalle Valo 
69e705c121SKalle Valo #include "mvm.h"
70e705c121SKalle Valo #include "sta.h"
71e705c121SKalle Valo #include "rs.h"
72e705c121SKalle Valo 
73854c5705SSara Sharon /*
74854c5705SSara Sharon  * New version of ADD_STA_sta command added new fields at the end of the
75854c5705SSara Sharon  * structure, so sending the size of the relevant API's structure is enough to
76854c5705SSara Sharon  * support both API versions.
77854c5705SSara Sharon  */
78854c5705SSara Sharon static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
79854c5705SSara Sharon {
80854c5705SSara Sharon 	return iwl_mvm_has_new_rx_api(mvm) ?
81854c5705SSara Sharon 		sizeof(struct iwl_mvm_add_sta_cmd) :
82854c5705SSara Sharon 		sizeof(struct iwl_mvm_add_sta_cmd_v7);
83854c5705SSara Sharon }
84854c5705SSara Sharon 
85e705c121SKalle Valo static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
86e705c121SKalle Valo 				    enum nl80211_iftype iftype)
87e705c121SKalle Valo {
88e705c121SKalle Valo 	int sta_id;
89e705c121SKalle Valo 	u32 reserved_ids = 0;
90e705c121SKalle Valo 
91e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
92e705c121SKalle Valo 	WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
93e705c121SKalle Valo 
94e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
95e705c121SKalle Valo 
96e705c121SKalle Valo 	/* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
97e705c121SKalle Valo 	if (iftype != NL80211_IFTYPE_STATION)
98e705c121SKalle Valo 		reserved_ids = BIT(0);
99e705c121SKalle Valo 
100e705c121SKalle Valo 	/* Don't take rcu_read_lock() since we are protected by mvm->mutex */
101e705c121SKalle Valo 	for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++) {
102e705c121SKalle Valo 		if (BIT(sta_id) & reserved_ids)
103e705c121SKalle Valo 			continue;
104e705c121SKalle Valo 
105e705c121SKalle Valo 		if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
106e705c121SKalle Valo 					       lockdep_is_held(&mvm->mutex)))
107e705c121SKalle Valo 			return sta_id;
108e705c121SKalle Valo 	}
109e705c121SKalle Valo 	return IWL_MVM_STATION_COUNT;
110e705c121SKalle Valo }
111e705c121SKalle Valo 
112e705c121SKalle Valo /* send station add/update command to firmware */
113e705c121SKalle Valo int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
11424afba76SLiad Kaufman 			   bool update, unsigned int flags)
115e705c121SKalle Valo {
116e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
117e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd add_sta_cmd = {
118e705c121SKalle Valo 		.sta_id = mvm_sta->sta_id,
119e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
120e705c121SKalle Valo 		.add_modify = update ? 1 : 0,
121e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
122e705c121SKalle Valo 						 STA_FLG_MIMO_EN_MSK),
123cf0cda19SLiad Kaufman 		.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
124e705c121SKalle Valo 	};
125e705c121SKalle Valo 	int ret;
126e705c121SKalle Valo 	u32 status;
127e705c121SKalle Valo 	u32 agg_size = 0, mpdu_dens = 0;
128e705c121SKalle Valo 
12924afba76SLiad Kaufman 	if (!update || (flags & STA_MODIFY_QUEUES)) {
130e705c121SKalle Valo 		add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
131e705c121SKalle Valo 		memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
13224afba76SLiad Kaufman 
13324afba76SLiad Kaufman 		if (flags & STA_MODIFY_QUEUES)
13424afba76SLiad Kaufman 			add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
135e705c121SKalle Valo 	}
136e705c121SKalle Valo 
137e705c121SKalle Valo 	switch (sta->bandwidth) {
138e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_160:
139e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
140e705c121SKalle Valo 		/* fall through */
141e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_80:
142e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
143e705c121SKalle Valo 		/* fall through */
144e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_40:
145e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
146e705c121SKalle Valo 		/* fall through */
147e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_20:
148e705c121SKalle Valo 		if (sta->ht_cap.ht_supported)
149e705c121SKalle Valo 			add_sta_cmd.station_flags |=
150e705c121SKalle Valo 				cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
151e705c121SKalle Valo 		break;
152e705c121SKalle Valo 	}
153e705c121SKalle Valo 
154e705c121SKalle Valo 	switch (sta->rx_nss) {
155e705c121SKalle Valo 	case 1:
156e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
157e705c121SKalle Valo 		break;
158e705c121SKalle Valo 	case 2:
159e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
160e705c121SKalle Valo 		break;
161e705c121SKalle Valo 	case 3 ... 8:
162e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
163e705c121SKalle Valo 		break;
164e705c121SKalle Valo 	}
165e705c121SKalle Valo 
166e705c121SKalle Valo 	switch (sta->smps_mode) {
167e705c121SKalle Valo 	case IEEE80211_SMPS_AUTOMATIC:
168e705c121SKalle Valo 	case IEEE80211_SMPS_NUM_MODES:
169e705c121SKalle Valo 		WARN_ON(1);
170e705c121SKalle Valo 		break;
171e705c121SKalle Valo 	case IEEE80211_SMPS_STATIC:
172e705c121SKalle Valo 		/* override NSS */
173e705c121SKalle Valo 		add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
174e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
175e705c121SKalle Valo 		break;
176e705c121SKalle Valo 	case IEEE80211_SMPS_DYNAMIC:
177e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
178e705c121SKalle Valo 		break;
179e705c121SKalle Valo 	case IEEE80211_SMPS_OFF:
180e705c121SKalle Valo 		/* nothing */
181e705c121SKalle Valo 		break;
182e705c121SKalle Valo 	}
183e705c121SKalle Valo 
184e705c121SKalle Valo 	if (sta->ht_cap.ht_supported) {
185e705c121SKalle Valo 		add_sta_cmd.station_flags_msk |=
186e705c121SKalle Valo 			cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
187e705c121SKalle Valo 				    STA_FLG_AGG_MPDU_DENS_MSK);
188e705c121SKalle Valo 
189e705c121SKalle Valo 		mpdu_dens = sta->ht_cap.ampdu_density;
190e705c121SKalle Valo 	}
191e705c121SKalle Valo 
192e705c121SKalle Valo 	if (sta->vht_cap.vht_supported) {
193e705c121SKalle Valo 		agg_size = sta->vht_cap.cap &
194e705c121SKalle Valo 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
195e705c121SKalle Valo 		agg_size >>=
196e705c121SKalle Valo 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
197e705c121SKalle Valo 	} else if (sta->ht_cap.ht_supported) {
198e705c121SKalle Valo 		agg_size = sta->ht_cap.ampdu_factor;
199e705c121SKalle Valo 	}
200e705c121SKalle Valo 
201e705c121SKalle Valo 	add_sta_cmd.station_flags |=
202e705c121SKalle Valo 		cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
203e705c121SKalle Valo 	add_sta_cmd.station_flags |=
204e705c121SKalle Valo 		cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
205e705c121SKalle Valo 
206e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
207854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
208854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
209e705c121SKalle Valo 					  &add_sta_cmd, &status);
210e705c121SKalle Valo 	if (ret)
211e705c121SKalle Valo 		return ret;
212e705c121SKalle Valo 
213837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
214e705c121SKalle Valo 	case ADD_STA_SUCCESS:
215e705c121SKalle Valo 		IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
216e705c121SKalle Valo 		break;
217e705c121SKalle Valo 	default:
218e705c121SKalle Valo 		ret = -EIO;
219e705c121SKalle Valo 		IWL_ERR(mvm, "ADD_STA failed\n");
220e705c121SKalle Valo 		break;
221e705c121SKalle Valo 	}
222e705c121SKalle Valo 
223e705c121SKalle Valo 	return ret;
224e705c121SKalle Valo }
225e705c121SKalle Valo 
22610b2b201SSara Sharon static void iwl_mvm_rx_agg_session_expired(unsigned long data)
22710b2b201SSara Sharon {
22810b2b201SSara Sharon 	struct iwl_mvm_baid_data __rcu **rcu_ptr = (void *)data;
22910b2b201SSara Sharon 	struct iwl_mvm_baid_data *ba_data;
23010b2b201SSara Sharon 	struct ieee80211_sta *sta;
23110b2b201SSara Sharon 	struct iwl_mvm_sta *mvm_sta;
23210b2b201SSara Sharon 	unsigned long timeout;
23310b2b201SSara Sharon 
23410b2b201SSara Sharon 	rcu_read_lock();
23510b2b201SSara Sharon 
23610b2b201SSara Sharon 	ba_data = rcu_dereference(*rcu_ptr);
23710b2b201SSara Sharon 
23810b2b201SSara Sharon 	if (WARN_ON(!ba_data))
23910b2b201SSara Sharon 		goto unlock;
24010b2b201SSara Sharon 
24110b2b201SSara Sharon 	if (!ba_data->timeout)
24210b2b201SSara Sharon 		goto unlock;
24310b2b201SSara Sharon 
24410b2b201SSara Sharon 	timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
24510b2b201SSara Sharon 	if (time_is_after_jiffies(timeout)) {
24610b2b201SSara Sharon 		mod_timer(&ba_data->session_timer, timeout);
24710b2b201SSara Sharon 		goto unlock;
24810b2b201SSara Sharon 	}
24910b2b201SSara Sharon 
25010b2b201SSara Sharon 	/* Timer expired */
25110b2b201SSara Sharon 	sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
25210b2b201SSara Sharon 	mvm_sta = iwl_mvm_sta_from_mac80211(sta);
25310b2b201SSara Sharon 	ieee80211_stop_rx_ba_session_offl(mvm_sta->vif,
25410b2b201SSara Sharon 					  sta->addr, ba_data->tid);
25510b2b201SSara Sharon unlock:
25610b2b201SSara Sharon 	rcu_read_unlock();
25710b2b201SSara Sharon }
25810b2b201SSara Sharon 
259e705c121SKalle Valo static int iwl_mvm_tdls_sta_init(struct iwl_mvm *mvm,
260e705c121SKalle Valo 				 struct ieee80211_sta *sta)
261e705c121SKalle Valo {
262e705c121SKalle Valo 	unsigned long used_hw_queues;
263e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
264e705c121SKalle Valo 	unsigned int wdg_timeout =
265e705c121SKalle Valo 		iwl_mvm_get_wd_timeout(mvm, NULL, true, false);
266e705c121SKalle Valo 	u32 ac;
267e705c121SKalle Valo 
268e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
269e705c121SKalle Valo 
270e705c121SKalle Valo 	used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, NULL);
271e705c121SKalle Valo 
272e705c121SKalle Valo 	/* Find available queues, and allocate them to the ACs */
273e705c121SKalle Valo 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
274e705c121SKalle Valo 		u8 queue = find_first_zero_bit(&used_hw_queues,
275e705c121SKalle Valo 					       mvm->first_agg_queue);
276e705c121SKalle Valo 
277e705c121SKalle Valo 		if (queue >= mvm->first_agg_queue) {
278e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to allocate STA queue\n");
279e705c121SKalle Valo 			return -EBUSY;
280e705c121SKalle Valo 		}
281e705c121SKalle Valo 
282e705c121SKalle Valo 		__set_bit(queue, &used_hw_queues);
283e705c121SKalle Valo 		mvmsta->hw_queue[ac] = queue;
284e705c121SKalle Valo 	}
285e705c121SKalle Valo 
286e705c121SKalle Valo 	/* Found a place for all queues - enable them */
287e705c121SKalle Valo 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
288e705c121SKalle Valo 		iwl_mvm_enable_ac_txq(mvm, mvmsta->hw_queue[ac],
289e705c121SKalle Valo 				      mvmsta->hw_queue[ac],
290e705c121SKalle Valo 				      iwl_mvm_ac_to_tx_fifo[ac], 0,
291e705c121SKalle Valo 				      wdg_timeout);
292e705c121SKalle Valo 		mvmsta->tfd_queue_msk |= BIT(mvmsta->hw_queue[ac]);
293e705c121SKalle Valo 	}
294e705c121SKalle Valo 
295e705c121SKalle Valo 	return 0;
296e705c121SKalle Valo }
297e705c121SKalle Valo 
298e705c121SKalle Valo static void iwl_mvm_tdls_sta_deinit(struct iwl_mvm *mvm,
299e705c121SKalle Valo 				    struct ieee80211_sta *sta)
300e705c121SKalle Valo {
301e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
302e705c121SKalle Valo 	unsigned long sta_msk;
303e705c121SKalle Valo 	int i;
304e705c121SKalle Valo 
305e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
306e705c121SKalle Valo 
307e705c121SKalle Valo 	/* disable the TDLS STA-specific queues */
308e705c121SKalle Valo 	sta_msk = mvmsta->tfd_queue_msk;
309e705c121SKalle Valo 	for_each_set_bit(i, &sta_msk, sizeof(sta_msk) * BITS_PER_BYTE)
310e705c121SKalle Valo 		iwl_mvm_disable_txq(mvm, i, i, IWL_MAX_TID_COUNT, 0);
311e705c121SKalle Valo }
312e705c121SKalle Valo 
3139794c64fSLiad Kaufman /* Disable aggregations for a bitmap of TIDs for a given station */
3149794c64fSLiad Kaufman static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
3159794c64fSLiad Kaufman 					unsigned long disable_agg_tids,
3169794c64fSLiad Kaufman 					bool remove_queue)
3179794c64fSLiad Kaufman {
3189794c64fSLiad Kaufman 	struct iwl_mvm_add_sta_cmd cmd = {};
3199794c64fSLiad Kaufman 	struct ieee80211_sta *sta;
3209794c64fSLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
3219794c64fSLiad Kaufman 	u32 status;
3229794c64fSLiad Kaufman 	u8 sta_id;
3239794c64fSLiad Kaufman 	int ret;
3249794c64fSLiad Kaufman 
3259794c64fSLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
3269794c64fSLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
3279794c64fSLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
3289794c64fSLiad Kaufman 
3299794c64fSLiad Kaufman 	rcu_read_lock();
3309794c64fSLiad Kaufman 
3319794c64fSLiad Kaufman 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3329794c64fSLiad Kaufman 
3339794c64fSLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
3349794c64fSLiad Kaufman 		rcu_read_unlock();
3359794c64fSLiad Kaufman 		return -EINVAL;
3369794c64fSLiad Kaufman 	}
3379794c64fSLiad Kaufman 
3389794c64fSLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
3399794c64fSLiad Kaufman 
3409794c64fSLiad Kaufman 	mvmsta->tid_disable_agg |= disable_agg_tids;
3419794c64fSLiad Kaufman 
3429794c64fSLiad Kaufman 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
3439794c64fSLiad Kaufman 	cmd.sta_id = mvmsta->sta_id;
3449794c64fSLiad Kaufman 	cmd.add_modify = STA_MODE_MODIFY;
3459794c64fSLiad Kaufman 	cmd.modify_mask = STA_MODIFY_QUEUES;
3469794c64fSLiad Kaufman 	if (disable_agg_tids)
3479794c64fSLiad Kaufman 		cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
3489794c64fSLiad Kaufman 	if (remove_queue)
3499794c64fSLiad Kaufman 		cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
3509794c64fSLiad Kaufman 	cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
3519794c64fSLiad Kaufman 	cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
3529794c64fSLiad Kaufman 
3539794c64fSLiad Kaufman 	rcu_read_unlock();
3549794c64fSLiad Kaufman 
3559794c64fSLiad Kaufman 	/* Notify FW of queue removal from the STA queues */
3569794c64fSLiad Kaufman 	status = ADD_STA_SUCCESS;
3579794c64fSLiad Kaufman 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
3589794c64fSLiad Kaufman 					  iwl_mvm_add_sta_cmd_size(mvm),
3599794c64fSLiad Kaufman 					  &cmd, &status);
3609794c64fSLiad Kaufman 
3619794c64fSLiad Kaufman 	return ret;
3629794c64fSLiad Kaufman }
3639794c64fSLiad Kaufman 
36442db09c1SLiad Kaufman static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
36542db09c1SLiad Kaufman {
36642db09c1SLiad Kaufman 	struct ieee80211_sta *sta;
36742db09c1SLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
36842db09c1SLiad Kaufman 	unsigned long tid_bitmap;
36942db09c1SLiad Kaufman 	unsigned long agg_tids = 0;
37042db09c1SLiad Kaufman 	s8 sta_id;
37142db09c1SLiad Kaufman 	int tid;
37242db09c1SLiad Kaufman 
37342db09c1SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
37442db09c1SLiad Kaufman 
37542db09c1SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
37642db09c1SLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
37742db09c1SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
37842db09c1SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
37942db09c1SLiad Kaufman 
38042db09c1SLiad Kaufman 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
38142db09c1SLiad Kaufman 					lockdep_is_held(&mvm->mutex));
38242db09c1SLiad Kaufman 
38342db09c1SLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
38442db09c1SLiad Kaufman 		return -EINVAL;
38542db09c1SLiad Kaufman 
38642db09c1SLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
38742db09c1SLiad Kaufman 
38842db09c1SLiad Kaufman 	spin_lock_bh(&mvmsta->lock);
38942db09c1SLiad Kaufman 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
39042db09c1SLiad Kaufman 		if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
39142db09c1SLiad Kaufman 			agg_tids |= BIT(tid);
39242db09c1SLiad Kaufman 	}
39342db09c1SLiad Kaufman 	spin_unlock_bh(&mvmsta->lock);
39442db09c1SLiad Kaufman 
39542db09c1SLiad Kaufman 	return agg_tids;
39642db09c1SLiad Kaufman }
39742db09c1SLiad Kaufman 
3989794c64fSLiad Kaufman /*
3999794c64fSLiad Kaufman  * Remove a queue from a station's resources.
4009794c64fSLiad Kaufman  * Note that this only marks as free. It DOESN'T delete a BA agreement, and
4019794c64fSLiad Kaufman  * doesn't disable the queue
4029794c64fSLiad Kaufman  */
4039794c64fSLiad Kaufman static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
4049794c64fSLiad Kaufman {
4059794c64fSLiad Kaufman 	struct ieee80211_sta *sta;
4069794c64fSLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
4079794c64fSLiad Kaufman 	unsigned long tid_bitmap;
4089794c64fSLiad Kaufman 	unsigned long disable_agg_tids = 0;
4099794c64fSLiad Kaufman 	u8 sta_id;
4109794c64fSLiad Kaufman 	int tid;
4119794c64fSLiad Kaufman 
4129794c64fSLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
4139794c64fSLiad Kaufman 
4149794c64fSLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
4159794c64fSLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
4169794c64fSLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
4179794c64fSLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
4189794c64fSLiad Kaufman 
4199794c64fSLiad Kaufman 	rcu_read_lock();
4209794c64fSLiad Kaufman 
4219794c64fSLiad Kaufman 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
4229794c64fSLiad Kaufman 
4239794c64fSLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
4249794c64fSLiad Kaufman 		rcu_read_unlock();
4259794c64fSLiad Kaufman 		return 0;
4269794c64fSLiad Kaufman 	}
4279794c64fSLiad Kaufman 
4289794c64fSLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
4299794c64fSLiad Kaufman 
4309794c64fSLiad Kaufman 	spin_lock_bh(&mvmsta->lock);
43142db09c1SLiad Kaufman 	/* Unmap MAC queues and TIDs from this queue */
4329794c64fSLiad Kaufman 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
4339794c64fSLiad Kaufman 		if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
4349794c64fSLiad Kaufman 			disable_agg_tids |= BIT(tid);
43542db09c1SLiad Kaufman 		mvmsta->tid_data[tid].txq_id = IEEE80211_INVAL_HW_QUEUE;
4369794c64fSLiad Kaufman 	}
4379794c64fSLiad Kaufman 
43842db09c1SLiad Kaufman 	mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
4399794c64fSLiad Kaufman 	spin_unlock_bh(&mvmsta->lock);
4409794c64fSLiad Kaufman 
4419794c64fSLiad Kaufman 	rcu_read_unlock();
4429794c64fSLiad Kaufman 
44342db09c1SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
4449794c64fSLiad Kaufman 	/* Unmap MAC queues and TIDs from this queue */
4459794c64fSLiad Kaufman 	mvm->queue_info[queue].hw_queue_to_mac80211 = 0;
4469794c64fSLiad Kaufman 	mvm->queue_info[queue].hw_queue_refcount = 0;
4479794c64fSLiad Kaufman 	mvm->queue_info[queue].tid_bitmap = 0;
44842db09c1SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
4499794c64fSLiad Kaufman 
4509794c64fSLiad Kaufman 	return disable_agg_tids;
4519794c64fSLiad Kaufman }
4529794c64fSLiad Kaufman 
45342db09c1SLiad Kaufman static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
45442db09c1SLiad Kaufman 				    unsigned long tfd_queue_mask, u8 ac)
45542db09c1SLiad Kaufman {
45642db09c1SLiad Kaufman 	int queue = 0;
45742db09c1SLiad Kaufman 	u8 ac_to_queue[IEEE80211_NUM_ACS];
45842db09c1SLiad Kaufman 	int i;
45942db09c1SLiad Kaufman 
46042db09c1SLiad Kaufman 	lockdep_assert_held(&mvm->queue_info_lock);
46142db09c1SLiad Kaufman 
46242db09c1SLiad Kaufman 	memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
46342db09c1SLiad Kaufman 
46442db09c1SLiad Kaufman 	/* See what ACs the existing queues for this STA have */
46542db09c1SLiad Kaufman 	for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
46642db09c1SLiad Kaufman 		/* Only DATA queues can be shared */
46742db09c1SLiad Kaufman 		if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
46842db09c1SLiad Kaufman 		    i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
46942db09c1SLiad Kaufman 			continue;
47042db09c1SLiad Kaufman 
4719f9af3d7SLiad Kaufman 		/* Don't try and take queues being reconfigured */
4729f9af3d7SLiad Kaufman 		if (mvm->queue_info[queue].status ==
4739f9af3d7SLiad Kaufman 		    IWL_MVM_QUEUE_RECONFIGURING)
4749f9af3d7SLiad Kaufman 			continue;
4759f9af3d7SLiad Kaufman 
47642db09c1SLiad Kaufman 		ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
47742db09c1SLiad Kaufman 	}
47842db09c1SLiad Kaufman 
47942db09c1SLiad Kaufman 	/*
48042db09c1SLiad Kaufman 	 * The queue to share is chosen only from DATA queues as follows (in
48142db09c1SLiad Kaufman 	 * descending priority):
48242db09c1SLiad Kaufman 	 * 1. An AC_BE queue
48342db09c1SLiad Kaufman 	 * 2. Same AC queue
48442db09c1SLiad Kaufman 	 * 3. Highest AC queue that is lower than new AC
48542db09c1SLiad Kaufman 	 * 4. Any existing AC (there always is at least 1 DATA queue)
48642db09c1SLiad Kaufman 	 */
48742db09c1SLiad Kaufman 
48842db09c1SLiad Kaufman 	/* Priority 1: An AC_BE queue */
48942db09c1SLiad Kaufman 	if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
49042db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_BE];
49142db09c1SLiad Kaufman 	/* Priority 2: Same AC queue */
49242db09c1SLiad Kaufman 	else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
49342db09c1SLiad Kaufman 		queue = ac_to_queue[ac];
49442db09c1SLiad Kaufman 	/* Priority 3a: If new AC is VO and VI exists - use VI */
49542db09c1SLiad Kaufman 	else if (ac == IEEE80211_AC_VO &&
49642db09c1SLiad Kaufman 		 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
49742db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VI];
49842db09c1SLiad Kaufman 	/* Priority 3b: No BE so only AC less than the new one is BK */
49942db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
50042db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_BK];
50142db09c1SLiad Kaufman 	/* Priority 4a: No BE nor BK - use VI if exists */
50242db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
50342db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VI];
50442db09c1SLiad Kaufman 	/* Priority 4b: No BE, BK nor VI - use VO if exists */
50542db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
50642db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VO];
50742db09c1SLiad Kaufman 
50842db09c1SLiad Kaufman 	/* Make sure queue found (or not) is legal */
5099f9af3d7SLiad Kaufman 	if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
5109f9af3d7SLiad Kaufman 	    !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
5119f9af3d7SLiad Kaufman 	    (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
51242db09c1SLiad Kaufman 		IWL_ERR(mvm, "No DATA queues available to share\n");
5139f9af3d7SLiad Kaufman 		return -ENOSPC;
5149f9af3d7SLiad Kaufman 	}
5159f9af3d7SLiad Kaufman 
5169f9af3d7SLiad Kaufman 	/* Make sure the queue isn't in the middle of being reconfigured */
5179f9af3d7SLiad Kaufman 	if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_RECONFIGURING) {
5189f9af3d7SLiad Kaufman 		IWL_ERR(mvm,
5199f9af3d7SLiad Kaufman 			"TXQ %d is in the middle of re-config - try again\n",
5209f9af3d7SLiad Kaufman 			queue);
5219f9af3d7SLiad Kaufman 		return -EBUSY;
52242db09c1SLiad Kaufman 	}
52342db09c1SLiad Kaufman 
52442db09c1SLiad Kaufman 	return queue;
52542db09c1SLiad Kaufman }
52642db09c1SLiad Kaufman 
52758f2cc57SLiad Kaufman /*
5289f9af3d7SLiad Kaufman  * If a given queue has a higher AC than the TID stream that is being compared
5299f9af3d7SLiad Kaufman  * to, the queue needs to be redirected to the lower AC. This function does that
53058f2cc57SLiad Kaufman  * in such a case, otherwise - if no redirection required - it does nothing,
53158f2cc57SLiad Kaufman  * unless the %force param is true.
53258f2cc57SLiad Kaufman  */
5339f9af3d7SLiad Kaufman int iwl_mvm_scd_queue_redirect(struct iwl_mvm *mvm, int queue, int tid,
53458f2cc57SLiad Kaufman 			       int ac, int ssn, unsigned int wdg_timeout,
53558f2cc57SLiad Kaufman 			       bool force)
53658f2cc57SLiad Kaufman {
53758f2cc57SLiad Kaufman 	struct iwl_scd_txq_cfg_cmd cmd = {
53858f2cc57SLiad Kaufman 		.scd_queue = queue,
539f7c692deSLiad Kaufman 		.action = SCD_CFG_DISABLE_QUEUE,
54058f2cc57SLiad Kaufman 	};
54158f2cc57SLiad Kaufman 	bool shared_queue;
54258f2cc57SLiad Kaufman 	unsigned long mq;
54358f2cc57SLiad Kaufman 	int ret;
54458f2cc57SLiad Kaufman 
54558f2cc57SLiad Kaufman 	/*
54658f2cc57SLiad Kaufman 	 * If the AC is lower than current one - FIFO needs to be redirected to
54758f2cc57SLiad Kaufman 	 * the lowest one of the streams in the queue. Check if this is needed
54858f2cc57SLiad Kaufman 	 * here.
54958f2cc57SLiad Kaufman 	 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
55058f2cc57SLiad Kaufman 	 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
55158f2cc57SLiad Kaufman 	 * we need to check if the numerical value of X is LARGER than of Y.
55258f2cc57SLiad Kaufman 	 */
55358f2cc57SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
55458f2cc57SLiad Kaufman 	if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
55558f2cc57SLiad Kaufman 		spin_unlock_bh(&mvm->queue_info_lock);
55658f2cc57SLiad Kaufman 
55758f2cc57SLiad Kaufman 		IWL_DEBUG_TX_QUEUES(mvm,
55858f2cc57SLiad Kaufman 				    "No redirection needed on TXQ #%d\n",
55958f2cc57SLiad Kaufman 				    queue);
56058f2cc57SLiad Kaufman 		return 0;
56158f2cc57SLiad Kaufman 	}
56258f2cc57SLiad Kaufman 
56358f2cc57SLiad Kaufman 	cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
56458f2cc57SLiad Kaufman 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
565edbe961cSLiad Kaufman 	cmd.tid = mvm->queue_info[queue].txq_tid;
56658f2cc57SLiad Kaufman 	mq = mvm->queue_info[queue].hw_queue_to_mac80211;
56758f2cc57SLiad Kaufman 	shared_queue = (mvm->queue_info[queue].hw_queue_refcount > 1);
56858f2cc57SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
56958f2cc57SLiad Kaufman 
5709f9af3d7SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
57158f2cc57SLiad Kaufman 			    queue, iwl_mvm_ac_to_tx_fifo[ac]);
57258f2cc57SLiad Kaufman 
57358f2cc57SLiad Kaufman 	/* Stop MAC queues and wait for this queue to empty */
57458f2cc57SLiad Kaufman 	iwl_mvm_stop_mac_queues(mvm, mq);
57558f2cc57SLiad Kaufman 	ret = iwl_trans_wait_tx_queue_empty(mvm->trans, BIT(queue));
57658f2cc57SLiad Kaufman 	if (ret) {
57758f2cc57SLiad Kaufman 		IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
57858f2cc57SLiad Kaufman 			queue);
57958f2cc57SLiad Kaufman 		ret = -EIO;
58058f2cc57SLiad Kaufman 		goto out;
58158f2cc57SLiad Kaufman 	}
58258f2cc57SLiad Kaufman 
58358f2cc57SLiad Kaufman 	/* Before redirecting the queue we need to de-activate it */
58458f2cc57SLiad Kaufman 	iwl_trans_txq_disable(mvm->trans, queue, false);
58558f2cc57SLiad Kaufman 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
58658f2cc57SLiad Kaufman 	if (ret)
58758f2cc57SLiad Kaufman 		IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
58858f2cc57SLiad Kaufman 			ret);
58958f2cc57SLiad Kaufman 
59058f2cc57SLiad Kaufman 	/* Make sure the SCD wrptr is correctly set before reconfiguring */
591ca3b9c6bSSara Sharon 	iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
59258f2cc57SLiad Kaufman 
593edbe961cSLiad Kaufman 	/* Update the TID "owner" of the queue */
594edbe961cSLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
595edbe961cSLiad Kaufman 	mvm->queue_info[queue].txq_tid = tid;
596edbe961cSLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
597edbe961cSLiad Kaufman 
59858f2cc57SLiad Kaufman 	/* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
59958f2cc57SLiad Kaufman 
60058f2cc57SLiad Kaufman 	/* Redirect to lower AC */
60158f2cc57SLiad Kaufman 	iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
60258f2cc57SLiad Kaufman 			     cmd.sta_id, tid, LINK_QUAL_AGG_FRAME_LIMIT_DEF,
60358f2cc57SLiad Kaufman 			     ssn);
60458f2cc57SLiad Kaufman 
60558f2cc57SLiad Kaufman 	/* Update AC marking of the queue */
60658f2cc57SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
60758f2cc57SLiad Kaufman 	mvm->queue_info[queue].mac80211_ac = ac;
60858f2cc57SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
60958f2cc57SLiad Kaufman 
61058f2cc57SLiad Kaufman 	/*
61158f2cc57SLiad Kaufman 	 * Mark queue as shared in transport if shared
61258f2cc57SLiad Kaufman 	 * Note this has to be done after queue enablement because enablement
61358f2cc57SLiad Kaufman 	 * can also set this value, and there is no indication there to shared
61458f2cc57SLiad Kaufman 	 * queues
61558f2cc57SLiad Kaufman 	 */
61658f2cc57SLiad Kaufman 	if (shared_queue)
61758f2cc57SLiad Kaufman 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
61858f2cc57SLiad Kaufman 
61958f2cc57SLiad Kaufman out:
62058f2cc57SLiad Kaufman 	/* Continue using the MAC queues */
62158f2cc57SLiad Kaufman 	iwl_mvm_start_mac_queues(mvm, mq);
62258f2cc57SLiad Kaufman 
62358f2cc57SLiad Kaufman 	return ret;
62458f2cc57SLiad Kaufman }
62558f2cc57SLiad Kaufman 
62624afba76SLiad Kaufman static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
62724afba76SLiad Kaufman 				   struct ieee80211_sta *sta, u8 ac, int tid,
62824afba76SLiad Kaufman 				   struct ieee80211_hdr *hdr)
62924afba76SLiad Kaufman {
63024afba76SLiad Kaufman 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
63124afba76SLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
63224afba76SLiad Kaufman 		.fifo = iwl_mvm_ac_to_tx_fifo[ac],
63324afba76SLiad Kaufman 		.sta_id = mvmsta->sta_id,
63424afba76SLiad Kaufman 		.tid = tid,
63524afba76SLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
63624afba76SLiad Kaufman 	};
63724afba76SLiad Kaufman 	unsigned int wdg_timeout =
63824afba76SLiad Kaufman 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
63924afba76SLiad Kaufman 	u8 mac_queue = mvmsta->vif->hw_queue[ac];
64024afba76SLiad Kaufman 	int queue = -1;
6419794c64fSLiad Kaufman 	bool using_inactive_queue = false;
6429794c64fSLiad Kaufman 	unsigned long disable_agg_tids = 0;
6439794c64fSLiad Kaufman 	enum iwl_mvm_agg_state queue_state;
64442db09c1SLiad Kaufman 	bool shared_queue = false;
64524afba76SLiad Kaufman 	int ssn;
64642db09c1SLiad Kaufman 	unsigned long tfd_queue_mask;
647cf961e16SLiad Kaufman 	int ret;
64824afba76SLiad Kaufman 
64924afba76SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
65024afba76SLiad Kaufman 
65142db09c1SLiad Kaufman 	spin_lock_bh(&mvmsta->lock);
65242db09c1SLiad Kaufman 	tfd_queue_mask = mvmsta->tfd_queue_msk;
65342db09c1SLiad Kaufman 	spin_unlock_bh(&mvmsta->lock);
65442db09c1SLiad Kaufman 
655d2515a99SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
65624afba76SLiad Kaufman 
65724afba76SLiad Kaufman 	/*
65824afba76SLiad Kaufman 	 * Non-QoS, QoS NDP and MGMT frames should go to a MGMT queue, if one
65924afba76SLiad Kaufman 	 * exists
66024afba76SLiad Kaufman 	 */
66124afba76SLiad Kaufman 	if (!ieee80211_is_data_qos(hdr->frame_control) ||
66224afba76SLiad Kaufman 	    ieee80211_is_qos_nullfunc(hdr->frame_control)) {
6639794c64fSLiad Kaufman 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
6649794c64fSLiad Kaufman 						IWL_MVM_DQA_MIN_MGMT_QUEUE,
66524afba76SLiad Kaufman 						IWL_MVM_DQA_MAX_MGMT_QUEUE);
66624afba76SLiad Kaufman 		if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
66724afba76SLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
66824afba76SLiad Kaufman 					    queue);
66924afba76SLiad Kaufman 
67024afba76SLiad Kaufman 		/* If no such queue is found, we'll use a DATA queue instead */
67124afba76SLiad Kaufman 	}
67224afba76SLiad Kaufman 
6739794c64fSLiad Kaufman 	if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
6749794c64fSLiad Kaufman 	    (mvm->queue_info[mvmsta->reserved_queue].status ==
6759794c64fSLiad Kaufman 	     IWL_MVM_QUEUE_RESERVED ||
6769794c64fSLiad Kaufman 	     mvm->queue_info[mvmsta->reserved_queue].status ==
6779794c64fSLiad Kaufman 	     IWL_MVM_QUEUE_INACTIVE)) {
67824afba76SLiad Kaufman 		queue = mvmsta->reserved_queue;
6799794c64fSLiad Kaufman 		mvm->queue_info[queue].reserved = true;
68024afba76SLiad Kaufman 		IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
68124afba76SLiad Kaufman 	}
68224afba76SLiad Kaufman 
68324afba76SLiad Kaufman 	if (queue < 0)
6849794c64fSLiad Kaufman 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
6859794c64fSLiad Kaufman 						IWL_MVM_DQA_MIN_DATA_QUEUE,
68624afba76SLiad Kaufman 						IWL_MVM_DQA_MAX_DATA_QUEUE);
687cf961e16SLiad Kaufman 
688cf961e16SLiad Kaufman 	/*
6899794c64fSLiad Kaufman 	 * Check if this queue is already allocated but inactive.
6909794c64fSLiad Kaufman 	 * In such a case, we'll need to first free this queue before enabling
6919794c64fSLiad Kaufman 	 * it again, so we'll mark it as reserved to make sure no new traffic
6929794c64fSLiad Kaufman 	 * arrives on it
6939794c64fSLiad Kaufman 	 */
6949794c64fSLiad Kaufman 	if (queue > 0 &&
6959794c64fSLiad Kaufman 	    mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
6969794c64fSLiad Kaufman 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
6979794c64fSLiad Kaufman 		using_inactive_queue = true;
6989794c64fSLiad Kaufman 		IWL_DEBUG_TX_QUEUES(mvm,
6999794c64fSLiad Kaufman 				    "Re-assigning TXQ %d: sta_id=%d, tid=%d\n",
7009794c64fSLiad Kaufman 				    queue, mvmsta->sta_id, tid);
7019794c64fSLiad Kaufman 	}
7029794c64fSLiad Kaufman 
70342db09c1SLiad Kaufman 	/* No free queue - we'll have to share */
70442db09c1SLiad Kaufman 	if (queue <= 0) {
70542db09c1SLiad Kaufman 		queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
70642db09c1SLiad Kaufman 		if (queue > 0) {
70742db09c1SLiad Kaufman 			shared_queue = true;
70842db09c1SLiad Kaufman 			mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
70942db09c1SLiad Kaufman 		}
71042db09c1SLiad Kaufman 	}
71142db09c1SLiad Kaufman 
7129794c64fSLiad Kaufman 	/*
713cf961e16SLiad Kaufman 	 * Mark TXQ as ready, even though it hasn't been fully configured yet,
714cf961e16SLiad Kaufman 	 * to make sure no one else takes it.
715cf961e16SLiad Kaufman 	 * This will allow avoiding re-acquiring the lock at the end of the
716cf961e16SLiad Kaufman 	 * configuration. On error we'll mark it back as free.
717cf961e16SLiad Kaufman 	 */
71842db09c1SLiad Kaufman 	if ((queue > 0) && !shared_queue)
719cf961e16SLiad Kaufman 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
72024afba76SLiad Kaufman 
721d2515a99SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
72224afba76SLiad Kaufman 
72342db09c1SLiad Kaufman 	/* This shouldn't happen - out of queues */
72442db09c1SLiad Kaufman 	if (WARN_ON(queue <= 0)) {
72542db09c1SLiad Kaufman 		IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
72642db09c1SLiad Kaufman 			tid, cfg.sta_id);
7279f9af3d7SLiad Kaufman 		return queue;
72842db09c1SLiad Kaufman 	}
72924afba76SLiad Kaufman 
73024afba76SLiad Kaufman 	/*
73124afba76SLiad Kaufman 	 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
73224afba76SLiad Kaufman 	 * but for configuring the SCD to send A-MPDUs we need to mark the queue
73324afba76SLiad Kaufman 	 * as aggregatable.
73424afba76SLiad Kaufman 	 * Mark all DATA queues as allowing to be aggregated at some point
73524afba76SLiad Kaufman 	 */
736d5216a28SLiad Kaufman 	cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
737d5216a28SLiad Kaufman 			 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
73824afba76SLiad Kaufman 
7399794c64fSLiad Kaufman 	/*
7409794c64fSLiad Kaufman 	 * If this queue was previously inactive (idle) - we need to free it
7419794c64fSLiad Kaufman 	 * first
7429794c64fSLiad Kaufman 	 */
7439794c64fSLiad Kaufman 	if (using_inactive_queue) {
7449794c64fSLiad Kaufman 		struct iwl_scd_txq_cfg_cmd cmd = {
7459794c64fSLiad Kaufman 			.scd_queue = queue,
746f7c692deSLiad Kaufman 			.action = SCD_CFG_DISABLE_QUEUE,
7479794c64fSLiad Kaufman 		};
748d55092b4SLiad Kaufman 		u8 txq_curr_ac;
7499794c64fSLiad Kaufman 
7509794c64fSLiad Kaufman 		disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
7519794c64fSLiad Kaufman 
75293f436e2SLiad Kaufman 		spin_lock_bh(&mvm->queue_info_lock);
753d55092b4SLiad Kaufman 		txq_curr_ac = mvm->queue_info[queue].mac80211_ac;
75493f436e2SLiad Kaufman 		cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
755d55092b4SLiad Kaufman 		cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[txq_curr_ac];
756edbe961cSLiad Kaufman 		cmd.tid = mvm->queue_info[queue].txq_tid;
75793f436e2SLiad Kaufman 		spin_unlock_bh(&mvm->queue_info_lock);
75893f436e2SLiad Kaufman 
7599794c64fSLiad Kaufman 		/* Disable the queue */
7608d98ae6eSLiad Kaufman 		if (disable_agg_tids)
7618d98ae6eSLiad Kaufman 			iwl_mvm_invalidate_sta_queue(mvm, queue,
7628d98ae6eSLiad Kaufman 						     disable_agg_tids, false);
7639794c64fSLiad Kaufman 		iwl_trans_txq_disable(mvm->trans, queue, false);
7649794c64fSLiad Kaufman 		ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd),
7659794c64fSLiad Kaufman 					   &cmd);
7669794c64fSLiad Kaufman 		if (ret) {
7679794c64fSLiad Kaufman 			IWL_ERR(mvm,
7689794c64fSLiad Kaufman 				"Failed to free inactive queue %d (ret=%d)\n",
7699794c64fSLiad Kaufman 				queue, ret);
7709794c64fSLiad Kaufman 
7719794c64fSLiad Kaufman 			/* Re-mark the inactive queue as inactive */
7729794c64fSLiad Kaufman 			spin_lock_bh(&mvm->queue_info_lock);
7739794c64fSLiad Kaufman 			mvm->queue_info[queue].status = IWL_MVM_QUEUE_INACTIVE;
7749794c64fSLiad Kaufman 			spin_unlock_bh(&mvm->queue_info_lock);
7759794c64fSLiad Kaufman 
7769794c64fSLiad Kaufman 			return ret;
7779794c64fSLiad Kaufman 		}
7788d98ae6eSLiad Kaufman 
7798d98ae6eSLiad Kaufman 		/* If TXQ is allocated to another STA, update removal in FW */
7808d98ae6eSLiad Kaufman 		if (cmd.sta_id != mvmsta->sta_id)
7818d98ae6eSLiad Kaufman 			iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
7829794c64fSLiad Kaufman 	}
7839794c64fSLiad Kaufman 
78442db09c1SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm,
78542db09c1SLiad Kaufman 			    "Allocating %squeue #%d to sta %d on tid %d\n",
78642db09c1SLiad Kaufman 			    shared_queue ? "shared " : "", queue,
78742db09c1SLiad Kaufman 			    mvmsta->sta_id, tid);
78842db09c1SLiad Kaufman 
78942db09c1SLiad Kaufman 	if (shared_queue) {
79042db09c1SLiad Kaufman 		/* Disable any open aggs on this queue */
79142db09c1SLiad Kaufman 		disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
79242db09c1SLiad Kaufman 
79342db09c1SLiad Kaufman 		if (disable_agg_tids) {
79442db09c1SLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
79542db09c1SLiad Kaufman 					    queue);
79642db09c1SLiad Kaufman 			iwl_mvm_invalidate_sta_queue(mvm, queue,
79742db09c1SLiad Kaufman 						     disable_agg_tids, false);
79842db09c1SLiad Kaufman 		}
79942db09c1SLiad Kaufman 	}
80024afba76SLiad Kaufman 
80124afba76SLiad Kaufman 	ssn = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
80224afba76SLiad Kaufman 	iwl_mvm_enable_txq(mvm, queue, mac_queue, ssn, &cfg,
80324afba76SLiad Kaufman 			   wdg_timeout);
80424afba76SLiad Kaufman 
80558f2cc57SLiad Kaufman 	/*
80658f2cc57SLiad Kaufman 	 * Mark queue as shared in transport if shared
80758f2cc57SLiad Kaufman 	 * Note this has to be done after queue enablement because enablement
80858f2cc57SLiad Kaufman 	 * can also set this value, and there is no indication there to shared
80958f2cc57SLiad Kaufman 	 * queues
81058f2cc57SLiad Kaufman 	 */
81158f2cc57SLiad Kaufman 	if (shared_queue)
81258f2cc57SLiad Kaufman 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
81358f2cc57SLiad Kaufman 
81424afba76SLiad Kaufman 	spin_lock_bh(&mvmsta->lock);
81524afba76SLiad Kaufman 	mvmsta->tid_data[tid].txq_id = queue;
8169794c64fSLiad Kaufman 	mvmsta->tid_data[tid].is_tid_active = true;
81724afba76SLiad Kaufman 	mvmsta->tfd_queue_msk |= BIT(queue);
8189794c64fSLiad Kaufman 	queue_state = mvmsta->tid_data[tid].state;
81924afba76SLiad Kaufman 
82024afba76SLiad Kaufman 	if (mvmsta->reserved_queue == queue)
82124afba76SLiad Kaufman 		mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
82224afba76SLiad Kaufman 	spin_unlock_bh(&mvmsta->lock);
82324afba76SLiad Kaufman 
82442db09c1SLiad Kaufman 	if (!shared_queue) {
825cf961e16SLiad Kaufman 		ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
826cf961e16SLiad Kaufman 		if (ret)
827cf961e16SLiad Kaufman 			goto out_err;
828cf961e16SLiad Kaufman 
8299794c64fSLiad Kaufman 		/* If we need to re-enable aggregations... */
83042db09c1SLiad Kaufman 		if (queue_state == IWL_AGG_ON) {
8319794c64fSLiad Kaufman 			ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
83242db09c1SLiad Kaufman 			if (ret)
83342db09c1SLiad Kaufman 				goto out_err;
83442db09c1SLiad Kaufman 		}
83558f2cc57SLiad Kaufman 	} else {
83658f2cc57SLiad Kaufman 		/* Redirect queue, if needed */
83758f2cc57SLiad Kaufman 		ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid, ac, ssn,
83858f2cc57SLiad Kaufman 						 wdg_timeout, false);
83958f2cc57SLiad Kaufman 		if (ret)
84058f2cc57SLiad Kaufman 			goto out_err;
84142db09c1SLiad Kaufman 	}
8429794c64fSLiad Kaufman 
84342db09c1SLiad Kaufman 	return 0;
844cf961e16SLiad Kaufman 
845cf961e16SLiad Kaufman out_err:
846cf961e16SLiad Kaufman 	iwl_mvm_disable_txq(mvm, queue, mac_queue, tid, 0);
847cf961e16SLiad Kaufman 
848cf961e16SLiad Kaufman 	return ret;
84924afba76SLiad Kaufman }
85024afba76SLiad Kaufman 
85119aefa45SLiad Kaufman static void iwl_mvm_change_queue_owner(struct iwl_mvm *mvm, int queue)
85219aefa45SLiad Kaufman {
85319aefa45SLiad Kaufman 	struct iwl_scd_txq_cfg_cmd cmd = {
85419aefa45SLiad Kaufman 		.scd_queue = queue,
85519aefa45SLiad Kaufman 		.action = SCD_CFG_UPDATE_QUEUE_TID,
85619aefa45SLiad Kaufman 	};
85719aefa45SLiad Kaufman 	s8 sta_id;
85819aefa45SLiad Kaufman 	int tid;
85919aefa45SLiad Kaufman 	unsigned long tid_bitmap;
86019aefa45SLiad Kaufman 	int ret;
86119aefa45SLiad Kaufman 
86219aefa45SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
86319aefa45SLiad Kaufman 
86419aefa45SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
86519aefa45SLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
86619aefa45SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
86719aefa45SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
86819aefa45SLiad Kaufman 
86919aefa45SLiad Kaufman 	if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
87019aefa45SLiad Kaufman 		return;
87119aefa45SLiad Kaufman 
87219aefa45SLiad Kaufman 	/* Find any TID for queue */
87319aefa45SLiad Kaufman 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
87419aefa45SLiad Kaufman 	cmd.tid = tid;
87519aefa45SLiad Kaufman 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
87619aefa45SLiad Kaufman 
87719aefa45SLiad Kaufman 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
878341ca402SLiad Kaufman 	if (ret) {
87919aefa45SLiad Kaufman 		IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
88019aefa45SLiad Kaufman 			queue, ret);
881341ca402SLiad Kaufman 		return;
882341ca402SLiad Kaufman 	}
883341ca402SLiad Kaufman 
884341ca402SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
885341ca402SLiad Kaufman 	mvm->queue_info[queue].txq_tid = tid;
886341ca402SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
88719aefa45SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
88819aefa45SLiad Kaufman 			    queue, tid);
88919aefa45SLiad Kaufman }
89019aefa45SLiad Kaufman 
8919f9af3d7SLiad Kaufman static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
8929f9af3d7SLiad Kaufman {
8939f9af3d7SLiad Kaufman 	struct ieee80211_sta *sta;
8949f9af3d7SLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
8959f9af3d7SLiad Kaufman 	s8 sta_id;
8969f9af3d7SLiad Kaufman 	int tid = -1;
8979f9af3d7SLiad Kaufman 	unsigned long tid_bitmap;
8989f9af3d7SLiad Kaufman 	unsigned int wdg_timeout;
8999f9af3d7SLiad Kaufman 	int ssn;
9009f9af3d7SLiad Kaufman 	int ret = true;
9019f9af3d7SLiad Kaufman 
9029f9af3d7SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
9039f9af3d7SLiad Kaufman 
9049f9af3d7SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
9059f9af3d7SLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
9069f9af3d7SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
9079f9af3d7SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
9089f9af3d7SLiad Kaufman 
9099f9af3d7SLiad Kaufman 	/* Find TID for queue, and make sure it is the only one on the queue */
9109f9af3d7SLiad Kaufman 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
9119f9af3d7SLiad Kaufman 	if (tid_bitmap != BIT(tid)) {
9129f9af3d7SLiad Kaufman 		IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
9139f9af3d7SLiad Kaufman 			queue, tid_bitmap);
9149f9af3d7SLiad Kaufman 		return;
9159f9af3d7SLiad Kaufman 	}
9169f9af3d7SLiad Kaufman 
9179f9af3d7SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
9189f9af3d7SLiad Kaufman 			    tid);
9199f9af3d7SLiad Kaufman 
9209f9af3d7SLiad Kaufman 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
9219f9af3d7SLiad Kaufman 					lockdep_is_held(&mvm->mutex));
9229f9af3d7SLiad Kaufman 
9239f9af3d7SLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
9249f9af3d7SLiad Kaufman 		return;
9259f9af3d7SLiad Kaufman 
9269f9af3d7SLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
9279f9af3d7SLiad Kaufman 	wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
9289f9af3d7SLiad Kaufman 
9299f9af3d7SLiad Kaufman 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
9309f9af3d7SLiad Kaufman 
9319f9af3d7SLiad Kaufman 	ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid,
9329f9af3d7SLiad Kaufman 					 tid_to_mac80211_ac[tid], ssn,
9339f9af3d7SLiad Kaufman 					 wdg_timeout, true);
9349f9af3d7SLiad Kaufman 	if (ret) {
9359f9af3d7SLiad Kaufman 		IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
9369f9af3d7SLiad Kaufman 		return;
9379f9af3d7SLiad Kaufman 	}
9389f9af3d7SLiad Kaufman 
9399f9af3d7SLiad Kaufman 	/* If aggs should be turned back on - do it */
9409f9af3d7SLiad Kaufman 	if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
9419cd70e80SEmmanuel Grumbach 		struct iwl_mvm_add_sta_cmd cmd = {0};
9429f9af3d7SLiad Kaufman 
9439f9af3d7SLiad Kaufman 		mvmsta->tid_disable_agg &= ~BIT(tid);
9449f9af3d7SLiad Kaufman 
9459f9af3d7SLiad Kaufman 		cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
9469f9af3d7SLiad Kaufman 		cmd.sta_id = mvmsta->sta_id;
9479f9af3d7SLiad Kaufman 		cmd.add_modify = STA_MODE_MODIFY;
9489f9af3d7SLiad Kaufman 		cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
9499f9af3d7SLiad Kaufman 		cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
9509f9af3d7SLiad Kaufman 		cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
9519f9af3d7SLiad Kaufman 
9529f9af3d7SLiad Kaufman 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
9539f9af3d7SLiad Kaufman 					   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
9549f9af3d7SLiad Kaufman 		if (!ret) {
9559f9af3d7SLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm,
9569f9af3d7SLiad Kaufman 					    "TXQ #%d is now aggregated again\n",
9579f9af3d7SLiad Kaufman 					    queue);
9589f9af3d7SLiad Kaufman 
9599f9af3d7SLiad Kaufman 			/* Mark queue intenally as aggregating again */
9609f9af3d7SLiad Kaufman 			iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
9619f9af3d7SLiad Kaufman 		}
9629f9af3d7SLiad Kaufman 	}
9639f9af3d7SLiad Kaufman 
9649f9af3d7SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
9659f9af3d7SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
9669f9af3d7SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
9679f9af3d7SLiad Kaufman }
9689f9af3d7SLiad Kaufman 
96924afba76SLiad Kaufman static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
97024afba76SLiad Kaufman {
97124afba76SLiad Kaufman 	if (tid == IWL_MAX_TID_COUNT)
97224afba76SLiad Kaufman 		return IEEE80211_AC_VO; /* MGMT */
97324afba76SLiad Kaufman 
97424afba76SLiad Kaufman 	return tid_to_mac80211_ac[tid];
97524afba76SLiad Kaufman }
97624afba76SLiad Kaufman 
97724afba76SLiad Kaufman static void iwl_mvm_tx_deferred_stream(struct iwl_mvm *mvm,
97824afba76SLiad Kaufman 				       struct ieee80211_sta *sta, int tid)
97924afba76SLiad Kaufman {
98024afba76SLiad Kaufman 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
98124afba76SLiad Kaufman 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
98224afba76SLiad Kaufman 	struct sk_buff *skb;
98324afba76SLiad Kaufman 	struct ieee80211_hdr *hdr;
98424afba76SLiad Kaufman 	struct sk_buff_head deferred_tx;
98524afba76SLiad Kaufman 	u8 mac_queue;
98624afba76SLiad Kaufman 	bool no_queue = false; /* Marks if there is a problem with the queue */
98724afba76SLiad Kaufman 	u8 ac;
98824afba76SLiad Kaufman 
98924afba76SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
99024afba76SLiad Kaufman 
99124afba76SLiad Kaufman 	skb = skb_peek(&tid_data->deferred_tx_frames);
99224afba76SLiad Kaufman 	if (!skb)
99324afba76SLiad Kaufman 		return;
99424afba76SLiad Kaufman 	hdr = (void *)skb->data;
99524afba76SLiad Kaufman 
99624afba76SLiad Kaufman 	ac = iwl_mvm_tid_to_ac_queue(tid);
99724afba76SLiad Kaufman 	mac_queue = IEEE80211_SKB_CB(skb)->hw_queue;
99824afba76SLiad Kaufman 
99924afba76SLiad Kaufman 	if (tid_data->txq_id == IEEE80211_INVAL_HW_QUEUE &&
100024afba76SLiad Kaufman 	    iwl_mvm_sta_alloc_queue(mvm, sta, ac, tid, hdr)) {
100124afba76SLiad Kaufman 		IWL_ERR(mvm,
100224afba76SLiad Kaufman 			"Can't alloc TXQ for sta %d tid %d - dropping frame\n",
100324afba76SLiad Kaufman 			mvmsta->sta_id, tid);
100424afba76SLiad Kaufman 
100524afba76SLiad Kaufman 		/*
100624afba76SLiad Kaufman 		 * Mark queue as problematic so later the deferred traffic is
100724afba76SLiad Kaufman 		 * freed, as we can do nothing with it
100824afba76SLiad Kaufman 		 */
100924afba76SLiad Kaufman 		no_queue = true;
101024afba76SLiad Kaufman 	}
101124afba76SLiad Kaufman 
101224afba76SLiad Kaufman 	__skb_queue_head_init(&deferred_tx);
101324afba76SLiad Kaufman 
1014d2515a99SLiad Kaufman 	/* Disable bottom-halves when entering TX path */
1015d2515a99SLiad Kaufman 	local_bh_disable();
101624afba76SLiad Kaufman 	spin_lock(&mvmsta->lock);
101724afba76SLiad Kaufman 	skb_queue_splice_init(&tid_data->deferred_tx_frames, &deferred_tx);
1018ad5de737SLiad Kaufman 	mvmsta->deferred_traffic_tid_map &= ~BIT(tid);
101924afba76SLiad Kaufman 	spin_unlock(&mvmsta->lock);
102024afba76SLiad Kaufman 
102124afba76SLiad Kaufman 	while ((skb = __skb_dequeue(&deferred_tx)))
102224afba76SLiad Kaufman 		if (no_queue || iwl_mvm_tx_skb(mvm, skb, sta))
102324afba76SLiad Kaufman 			ieee80211_free_txskb(mvm->hw, skb);
102424afba76SLiad Kaufman 	local_bh_enable();
102524afba76SLiad Kaufman 
102624afba76SLiad Kaufman 	/* Wake queue */
102724afba76SLiad Kaufman 	iwl_mvm_start_mac_queues(mvm, BIT(mac_queue));
102824afba76SLiad Kaufman }
102924afba76SLiad Kaufman 
103024afba76SLiad Kaufman void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
103124afba76SLiad Kaufman {
103224afba76SLiad Kaufman 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
103324afba76SLiad Kaufman 					   add_stream_wk);
103424afba76SLiad Kaufman 	struct ieee80211_sta *sta;
103524afba76SLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
103624afba76SLiad Kaufman 	unsigned long deferred_tid_traffic;
10379f9af3d7SLiad Kaufman 	int queue, sta_id, tid;
103824afba76SLiad Kaufman 
10399794c64fSLiad Kaufman 	/* Check inactivity of queues */
10409794c64fSLiad Kaufman 	iwl_mvm_inactivity_check(mvm);
10419794c64fSLiad Kaufman 
104224afba76SLiad Kaufman 	mutex_lock(&mvm->mutex);
104324afba76SLiad Kaufman 
10449f9af3d7SLiad Kaufman 	/* Reconfigure queues requiring reconfiguation */
10459f9af3d7SLiad Kaufman 	for (queue = 0; queue < IWL_MAX_HW_QUEUES; queue++) {
10469f9af3d7SLiad Kaufman 		bool reconfig;
104719aefa45SLiad Kaufman 		bool change_owner;
10489f9af3d7SLiad Kaufman 
10499f9af3d7SLiad Kaufman 		spin_lock_bh(&mvm->queue_info_lock);
10509f9af3d7SLiad Kaufman 		reconfig = (mvm->queue_info[queue].status ==
10519f9af3d7SLiad Kaufman 			    IWL_MVM_QUEUE_RECONFIGURING);
105219aefa45SLiad Kaufman 
105319aefa45SLiad Kaufman 		/*
105419aefa45SLiad Kaufman 		 * We need to take into account a situation in which a TXQ was
105519aefa45SLiad Kaufman 		 * allocated to TID x, and then turned shared by adding TIDs y
105619aefa45SLiad Kaufman 		 * and z. If TID x becomes inactive and is removed from the TXQ,
105719aefa45SLiad Kaufman 		 * ownership must be given to one of the remaining TIDs.
105819aefa45SLiad Kaufman 		 * This is mainly because if TID x continues - a new queue can't
105919aefa45SLiad Kaufman 		 * be allocated for it as long as it is an owner of another TXQ.
106019aefa45SLiad Kaufman 		 */
106119aefa45SLiad Kaufman 		change_owner = !(mvm->queue_info[queue].tid_bitmap &
106219aefa45SLiad Kaufman 				 BIT(mvm->queue_info[queue].txq_tid)) &&
106319aefa45SLiad Kaufman 			       (mvm->queue_info[queue].status ==
106419aefa45SLiad Kaufman 				IWL_MVM_QUEUE_SHARED);
10659f9af3d7SLiad Kaufman 		spin_unlock_bh(&mvm->queue_info_lock);
10669f9af3d7SLiad Kaufman 
10679f9af3d7SLiad Kaufman 		if (reconfig)
10689f9af3d7SLiad Kaufman 			iwl_mvm_unshare_queue(mvm, queue);
106919aefa45SLiad Kaufman 		else if (change_owner)
107019aefa45SLiad Kaufman 			iwl_mvm_change_queue_owner(mvm, queue);
10719f9af3d7SLiad Kaufman 	}
10729f9af3d7SLiad Kaufman 
107324afba76SLiad Kaufman 	/* Go over all stations with deferred traffic */
107424afba76SLiad Kaufman 	for_each_set_bit(sta_id, mvm->sta_deferred_frames,
107524afba76SLiad Kaufman 			 IWL_MVM_STATION_COUNT) {
107624afba76SLiad Kaufman 		clear_bit(sta_id, mvm->sta_deferred_frames);
107724afba76SLiad Kaufman 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
107824afba76SLiad Kaufman 						lockdep_is_held(&mvm->mutex));
107924afba76SLiad Kaufman 		if (IS_ERR_OR_NULL(sta))
108024afba76SLiad Kaufman 			continue;
108124afba76SLiad Kaufman 
108224afba76SLiad Kaufman 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
108324afba76SLiad Kaufman 		deferred_tid_traffic = mvmsta->deferred_traffic_tid_map;
108424afba76SLiad Kaufman 
108524afba76SLiad Kaufman 		for_each_set_bit(tid, &deferred_tid_traffic,
108624afba76SLiad Kaufman 				 IWL_MAX_TID_COUNT + 1)
108724afba76SLiad Kaufman 			iwl_mvm_tx_deferred_stream(mvm, sta, tid);
108824afba76SLiad Kaufman 	}
108924afba76SLiad Kaufman 
109024afba76SLiad Kaufman 	mutex_unlock(&mvm->mutex);
109124afba76SLiad Kaufman }
109224afba76SLiad Kaufman 
109324afba76SLiad Kaufman static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1094d5216a28SLiad Kaufman 				      struct ieee80211_sta *sta,
1095d5216a28SLiad Kaufman 				      enum nl80211_iftype vif_type)
109624afba76SLiad Kaufman {
109724afba76SLiad Kaufman 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
109824afba76SLiad Kaufman 	int queue;
109924afba76SLiad Kaufman 
11009794c64fSLiad Kaufman 	/*
11019794c64fSLiad Kaufman 	 * Check for inactive queues, so we don't reach a situation where we
11029794c64fSLiad Kaufman 	 * can't add a STA due to a shortage in queues that doesn't really exist
11039794c64fSLiad Kaufman 	 */
11049794c64fSLiad Kaufman 	iwl_mvm_inactivity_check(mvm);
11059794c64fSLiad Kaufman 
110624afba76SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
110724afba76SLiad Kaufman 
110824afba76SLiad Kaufman 	/* Make sure we have free resources for this STA */
1109d5216a28SLiad Kaufman 	if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1110d5216a28SLiad Kaufman 	    !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].hw_queue_refcount &&
1111cf961e16SLiad Kaufman 	    (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1112cf961e16SLiad Kaufman 	     IWL_MVM_QUEUE_FREE))
1113d5216a28SLiad Kaufman 		queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1114d5216a28SLiad Kaufman 	else
11159794c64fSLiad Kaufman 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
11169794c64fSLiad Kaufman 						IWL_MVM_DQA_MIN_DATA_QUEUE,
111724afba76SLiad Kaufman 						IWL_MVM_DQA_MAX_DATA_QUEUE);
111824afba76SLiad Kaufman 	if (queue < 0) {
111924afba76SLiad Kaufman 		spin_unlock_bh(&mvm->queue_info_lock);
112024afba76SLiad Kaufman 		IWL_ERR(mvm, "No available queues for new station\n");
112124afba76SLiad Kaufman 		return -ENOSPC;
112224afba76SLiad Kaufman 	}
1123cf961e16SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
112424afba76SLiad Kaufman 
112524afba76SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
112624afba76SLiad Kaufman 
112724afba76SLiad Kaufman 	mvmsta->reserved_queue = queue;
112824afba76SLiad Kaufman 
112924afba76SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
113024afba76SLiad Kaufman 			    queue, mvmsta->sta_id);
113124afba76SLiad Kaufman 
113224afba76SLiad Kaufman 	return 0;
113324afba76SLiad Kaufman }
113424afba76SLiad Kaufman 
11358d98ae6eSLiad Kaufman /*
11368d98ae6eSLiad Kaufman  * In DQA mode, after a HW restart the queues should be allocated as before, in
11378d98ae6eSLiad Kaufman  * order to avoid race conditions when there are shared queues. This function
11388d98ae6eSLiad Kaufman  * does the re-mapping and queue allocation.
11398d98ae6eSLiad Kaufman  *
11408d98ae6eSLiad Kaufman  * Note that re-enabling aggregations isn't done in this function.
11418d98ae6eSLiad Kaufman  */
11428d98ae6eSLiad Kaufman static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
11438d98ae6eSLiad Kaufman 						 struct iwl_mvm_sta *mvm_sta)
11448d98ae6eSLiad Kaufman {
11458d98ae6eSLiad Kaufman 	unsigned int wdg_timeout =
11468d98ae6eSLiad Kaufman 			iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
11478d98ae6eSLiad Kaufman 	int i;
11488d98ae6eSLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
11498d98ae6eSLiad Kaufman 		.sta_id = mvm_sta->sta_id,
11508d98ae6eSLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
11518d98ae6eSLiad Kaufman 	};
11528d98ae6eSLiad Kaufman 
11538d98ae6eSLiad Kaufman 	/* Make sure reserved queue is still marked as such (or allocated) */
11548d98ae6eSLiad Kaufman 	mvm->queue_info[mvm_sta->reserved_queue].status =
11558d98ae6eSLiad Kaufman 		IWL_MVM_QUEUE_RESERVED;
11568d98ae6eSLiad Kaufman 
11578d98ae6eSLiad Kaufman 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
11588d98ae6eSLiad Kaufman 		struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
11598d98ae6eSLiad Kaufman 		int txq_id = tid_data->txq_id;
11608d98ae6eSLiad Kaufman 		int ac;
11618d98ae6eSLiad Kaufman 		u8 mac_queue;
11628d98ae6eSLiad Kaufman 
11638d98ae6eSLiad Kaufman 		if (txq_id == IEEE80211_INVAL_HW_QUEUE)
11648d98ae6eSLiad Kaufman 			continue;
11658d98ae6eSLiad Kaufman 
11668d98ae6eSLiad Kaufman 		skb_queue_head_init(&tid_data->deferred_tx_frames);
11678d98ae6eSLiad Kaufman 
11688d98ae6eSLiad Kaufman 		ac = tid_to_mac80211_ac[i];
11698d98ae6eSLiad Kaufman 		mac_queue = mvm_sta->vif->hw_queue[ac];
11708d98ae6eSLiad Kaufman 
11718d98ae6eSLiad Kaufman 		cfg.tid = i;
11728d98ae6eSLiad Kaufman 		cfg.fifo = iwl_mvm_ac_to_tx_fifo[ac];
11738d98ae6eSLiad Kaufman 		cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
11748d98ae6eSLiad Kaufman 				 txq_id == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
11758d98ae6eSLiad Kaufman 
11768d98ae6eSLiad Kaufman 		IWL_DEBUG_TX_QUEUES(mvm,
11778d98ae6eSLiad Kaufman 				    "Re-mapping sta %d tid %d to queue %d\n",
11788d98ae6eSLiad Kaufman 				    mvm_sta->sta_id, i, txq_id);
11798d98ae6eSLiad Kaufman 
11808d98ae6eSLiad Kaufman 		iwl_mvm_enable_txq(mvm, txq_id, mac_queue,
11818d98ae6eSLiad Kaufman 				   IEEE80211_SEQ_TO_SN(tid_data->seq_number),
11828d98ae6eSLiad Kaufman 				   &cfg, wdg_timeout);
11838d98ae6eSLiad Kaufman 
11848d98ae6eSLiad Kaufman 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
11858d98ae6eSLiad Kaufman 	}
11868d98ae6eSLiad Kaufman 
11878d98ae6eSLiad Kaufman 	atomic_set(&mvm->pending_frames[mvm_sta->sta_id], 0);
11888d98ae6eSLiad Kaufman }
11898d98ae6eSLiad Kaufman 
1190e705c121SKalle Valo int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1191e705c121SKalle Valo 		    struct ieee80211_vif *vif,
1192e705c121SKalle Valo 		    struct ieee80211_sta *sta)
1193e705c121SKalle Valo {
1194e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1195e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1196a571f5f6SSara Sharon 	struct iwl_mvm_rxq_dup_data *dup_data;
1197e705c121SKalle Valo 	int i, ret, sta_id;
1198e705c121SKalle Valo 
1199e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1200e705c121SKalle Valo 
1201e705c121SKalle Valo 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1202e705c121SKalle Valo 		sta_id = iwl_mvm_find_free_sta_id(mvm,
1203e705c121SKalle Valo 						  ieee80211_vif_type_p2p(vif));
1204e705c121SKalle Valo 	else
1205e705c121SKalle Valo 		sta_id = mvm_sta->sta_id;
1206e705c121SKalle Valo 
1207e705c121SKalle Valo 	if (sta_id == IWL_MVM_STATION_COUNT)
1208e705c121SKalle Valo 		return -ENOSPC;
1209e705c121SKalle Valo 
1210e705c121SKalle Valo 	spin_lock_init(&mvm_sta->lock);
1211e705c121SKalle Valo 
12128d98ae6eSLiad Kaufman 	/* In DQA mode, if this is a HW restart, re-alloc existing queues */
12138d98ae6eSLiad Kaufman 	if (iwl_mvm_is_dqa_supported(mvm) &&
12148d98ae6eSLiad Kaufman 	    test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
12158d98ae6eSLiad Kaufman 		iwl_mvm_realloc_queues_after_restart(mvm, mvm_sta);
12168d98ae6eSLiad Kaufman 		goto update_fw;
12178d98ae6eSLiad Kaufman 	}
12188d98ae6eSLiad Kaufman 
1219e705c121SKalle Valo 	mvm_sta->sta_id = sta_id;
1220e705c121SKalle Valo 	mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1221e705c121SKalle Valo 						      mvmvif->color);
1222e705c121SKalle Valo 	mvm_sta->vif = vif;
1223e705c121SKalle Valo 	mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1224e705c121SKalle Valo 	mvm_sta->tx_protection = 0;
1225e705c121SKalle Valo 	mvm_sta->tt_tx_protection = false;
1226e705c121SKalle Valo 
1227e705c121SKalle Valo 	/* HW restart, don't assume the memory has been zeroed */
1228e705c121SKalle Valo 	atomic_set(&mvm->pending_frames[sta_id], 0);
1229e705c121SKalle Valo 	mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1230e705c121SKalle Valo 	mvm_sta->tfd_queue_msk = 0;
1231e705c121SKalle Valo 
1232e3118ad7SLiad Kaufman 	/*
1233e3118ad7SLiad Kaufman 	 * Allocate new queues for a TDLS station, unless we're in DQA mode,
1234e3118ad7SLiad Kaufman 	 * and then they'll be allocated dynamically
1235e3118ad7SLiad Kaufman 	 */
1236e3118ad7SLiad Kaufman 	if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls) {
1237e705c121SKalle Valo 		ret = iwl_mvm_tdls_sta_init(mvm, sta);
1238e705c121SKalle Valo 		if (ret)
1239e705c121SKalle Valo 			return ret;
124024afba76SLiad Kaufman 	} else if (!iwl_mvm_is_dqa_supported(mvm)) {
1241e705c121SKalle Valo 		for (i = 0; i < IEEE80211_NUM_ACS; i++)
1242e705c121SKalle Valo 			if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
1243e705c121SKalle Valo 				mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
1244e705c121SKalle Valo 	}
1245e705c121SKalle Valo 
1246e705c121SKalle Valo 	/* for HW restart - reset everything but the sequence number */
124724afba76SLiad Kaufman 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1248e705c121SKalle Valo 		u16 seq = mvm_sta->tid_data[i].seq_number;
1249e705c121SKalle Valo 		memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1250e705c121SKalle Valo 		mvm_sta->tid_data[i].seq_number = seq;
125124afba76SLiad Kaufman 
125224afba76SLiad Kaufman 		if (!iwl_mvm_is_dqa_supported(mvm))
125324afba76SLiad Kaufman 			continue;
125424afba76SLiad Kaufman 
125524afba76SLiad Kaufman 		/*
125624afba76SLiad Kaufman 		 * Mark all queues for this STA as unallocated and defer TX
125724afba76SLiad Kaufman 		 * frames until the queue is allocated
125824afba76SLiad Kaufman 		 */
125924afba76SLiad Kaufman 		mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
126024afba76SLiad Kaufman 		skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
1261e705c121SKalle Valo 	}
126224afba76SLiad Kaufman 	mvm_sta->deferred_traffic_tid_map = 0;
1263e705c121SKalle Valo 	mvm_sta->agg_tids = 0;
1264e705c121SKalle Valo 
1265a571f5f6SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) &&
1266a571f5f6SSara Sharon 	    !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1267a571f5f6SSara Sharon 		dup_data = kcalloc(mvm->trans->num_rx_queues,
1268a571f5f6SSara Sharon 				   sizeof(*dup_data),
1269a571f5f6SSara Sharon 				   GFP_KERNEL);
1270a571f5f6SSara Sharon 		if (!dup_data)
1271a571f5f6SSara Sharon 			return -ENOMEM;
1272a571f5f6SSara Sharon 		mvm_sta->dup_data = dup_data;
1273a571f5f6SSara Sharon 	}
1274a571f5f6SSara Sharon 
127524afba76SLiad Kaufman 	if (iwl_mvm_is_dqa_supported(mvm)) {
1276d5216a28SLiad Kaufman 		ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1277d5216a28SLiad Kaufman 						 ieee80211_vif_type_p2p(vif));
127824afba76SLiad Kaufman 		if (ret)
127924afba76SLiad Kaufman 			goto err;
128024afba76SLiad Kaufman 	}
128124afba76SLiad Kaufman 
12828d98ae6eSLiad Kaufman update_fw:
128324afba76SLiad Kaufman 	ret = iwl_mvm_sta_send_to_fw(mvm, sta, false, 0);
1284e705c121SKalle Valo 	if (ret)
1285e705c121SKalle Valo 		goto err;
1286e705c121SKalle Valo 
1287e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION) {
1288e705c121SKalle Valo 		if (!sta->tdls) {
1289e705c121SKalle Valo 			WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
1290e705c121SKalle Valo 			mvmvif->ap_sta_id = sta_id;
1291e705c121SKalle Valo 		} else {
1292e705c121SKalle Valo 			WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
1293e705c121SKalle Valo 		}
1294e705c121SKalle Valo 	}
1295e705c121SKalle Valo 
1296e705c121SKalle Valo 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1297e705c121SKalle Valo 
1298e705c121SKalle Valo 	return 0;
1299e705c121SKalle Valo 
1300e705c121SKalle Valo err:
1301e3118ad7SLiad Kaufman 	if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
1302e705c121SKalle Valo 		iwl_mvm_tdls_sta_deinit(mvm, sta);
1303e705c121SKalle Valo 	return ret;
1304e705c121SKalle Valo }
1305e705c121SKalle Valo 
1306e705c121SKalle Valo int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1307e705c121SKalle Valo 		      bool drain)
1308e705c121SKalle Valo {
1309e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
1310e705c121SKalle Valo 	int ret;
1311e705c121SKalle Valo 	u32 status;
1312e705c121SKalle Valo 
1313e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1314e705c121SKalle Valo 
1315e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1316e705c121SKalle Valo 	cmd.sta_id = mvmsta->sta_id;
1317e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
1318e705c121SKalle Valo 	cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1319e705c121SKalle Valo 	cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1320e705c121SKalle Valo 
1321e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
1322854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1323854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
1324e705c121SKalle Valo 					  &cmd, &status);
1325e705c121SKalle Valo 	if (ret)
1326e705c121SKalle Valo 		return ret;
1327e705c121SKalle Valo 
1328837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1329e705c121SKalle Valo 	case ADD_STA_SUCCESS:
1330e705c121SKalle Valo 		IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1331e705c121SKalle Valo 			       mvmsta->sta_id);
1332e705c121SKalle Valo 		break;
1333e705c121SKalle Valo 	default:
1334e705c121SKalle Valo 		ret = -EIO;
1335e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1336e705c121SKalle Valo 			mvmsta->sta_id);
1337e705c121SKalle Valo 		break;
1338e705c121SKalle Valo 	}
1339e705c121SKalle Valo 
1340e705c121SKalle Valo 	return ret;
1341e705c121SKalle Valo }
1342e705c121SKalle Valo 
1343e705c121SKalle Valo /*
1344e705c121SKalle Valo  * Remove a station from the FW table. Before sending the command to remove
1345e705c121SKalle Valo  * the station validate that the station is indeed known to the driver (sanity
1346e705c121SKalle Valo  * only).
1347e705c121SKalle Valo  */
1348e705c121SKalle Valo static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1349e705c121SKalle Valo {
1350e705c121SKalle Valo 	struct ieee80211_sta *sta;
1351e705c121SKalle Valo 	struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1352e705c121SKalle Valo 		.sta_id = sta_id,
1353e705c121SKalle Valo 	};
1354e705c121SKalle Valo 	int ret;
1355e705c121SKalle Valo 
1356e705c121SKalle Valo 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1357e705c121SKalle Valo 					lockdep_is_held(&mvm->mutex));
1358e705c121SKalle Valo 
1359e705c121SKalle Valo 	/* Note: internal stations are marked as error values */
1360e705c121SKalle Valo 	if (!sta) {
1361e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid station id\n");
1362e705c121SKalle Valo 		return -EINVAL;
1363e705c121SKalle Valo 	}
1364e705c121SKalle Valo 
1365e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1366e705c121SKalle Valo 				   sizeof(rm_sta_cmd), &rm_sta_cmd);
1367e705c121SKalle Valo 	if (ret) {
1368e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1369e705c121SKalle Valo 		return ret;
1370e705c121SKalle Valo 	}
1371e705c121SKalle Valo 
1372e705c121SKalle Valo 	return 0;
1373e705c121SKalle Valo }
1374e705c121SKalle Valo 
1375e705c121SKalle Valo void iwl_mvm_sta_drained_wk(struct work_struct *wk)
1376e705c121SKalle Valo {
1377e705c121SKalle Valo 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
1378e705c121SKalle Valo 	u8 sta_id;
1379e705c121SKalle Valo 
1380e705c121SKalle Valo 	/*
1381e705c121SKalle Valo 	 * The mutex is needed because of the SYNC cmd, but not only: if the
1382e705c121SKalle Valo 	 * work would run concurrently with iwl_mvm_rm_sta, it would run before
1383e705c121SKalle Valo 	 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
1384e705c121SKalle Valo 	 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
1385e705c121SKalle Valo 	 * that later.
1386e705c121SKalle Valo 	 */
1387e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1388e705c121SKalle Valo 
1389e705c121SKalle Valo 	for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
1390e705c121SKalle Valo 		int ret;
1391e705c121SKalle Valo 		struct ieee80211_sta *sta =
1392e705c121SKalle Valo 			rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1393e705c121SKalle Valo 						  lockdep_is_held(&mvm->mutex));
1394e705c121SKalle Valo 
1395e705c121SKalle Valo 		/*
1396e705c121SKalle Valo 		 * This station is in use or RCU-removed; the latter happens in
1397e705c121SKalle Valo 		 * managed mode, where mac80211 removes the station before we
1398e705c121SKalle Valo 		 * can remove it from firmware (we can only do that after the
1399e705c121SKalle Valo 		 * MAC is marked unassociated), and possibly while the deauth
1400e705c121SKalle Valo 		 * frame to disconnect from the AP is still queued. Then, the
1401e705c121SKalle Valo 		 * station pointer is -ENOENT when the last skb is reclaimed.
1402e705c121SKalle Valo 		 */
1403e705c121SKalle Valo 		if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
1404e705c121SKalle Valo 			continue;
1405e705c121SKalle Valo 
1406e705c121SKalle Valo 		if (PTR_ERR(sta) == -EINVAL) {
1407e705c121SKalle Valo 			IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
1408e705c121SKalle Valo 				sta_id);
1409e705c121SKalle Valo 			continue;
1410e705c121SKalle Valo 		}
1411e705c121SKalle Valo 
1412e705c121SKalle Valo 		if (!sta) {
1413e705c121SKalle Valo 			IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
1414e705c121SKalle Valo 				sta_id);
1415e705c121SKalle Valo 			continue;
1416e705c121SKalle Valo 		}
1417e705c121SKalle Valo 
1418e705c121SKalle Valo 		WARN_ON(PTR_ERR(sta) != -EBUSY);
1419e705c121SKalle Valo 		/* This station was removed and we waited until it got drained,
1420e705c121SKalle Valo 		 * we can now proceed and remove it.
1421e705c121SKalle Valo 		 */
1422e705c121SKalle Valo 		ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1423e705c121SKalle Valo 		if (ret) {
1424e705c121SKalle Valo 			IWL_ERR(mvm,
1425e705c121SKalle Valo 				"Couldn't remove sta %d after it was drained\n",
1426e705c121SKalle Valo 				sta_id);
1427e705c121SKalle Valo 			continue;
1428e705c121SKalle Valo 		}
1429e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1430e705c121SKalle Valo 		clear_bit(sta_id, mvm->sta_drained);
1431e705c121SKalle Valo 
1432e705c121SKalle Valo 		if (mvm->tfd_drained[sta_id]) {
1433e705c121SKalle Valo 			unsigned long i, msk = mvm->tfd_drained[sta_id];
1434e705c121SKalle Valo 
1435e705c121SKalle Valo 			for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
1436e705c121SKalle Valo 				iwl_mvm_disable_txq(mvm, i, i,
1437e705c121SKalle Valo 						    IWL_MAX_TID_COUNT, 0);
1438e705c121SKalle Valo 
1439e705c121SKalle Valo 			mvm->tfd_drained[sta_id] = 0;
1440e705c121SKalle Valo 			IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
1441e705c121SKalle Valo 				       sta_id, msk);
1442e705c121SKalle Valo 		}
1443e705c121SKalle Valo 	}
1444e705c121SKalle Valo 
1445e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1446e705c121SKalle Valo }
1447e705c121SKalle Valo 
144824afba76SLiad Kaufman static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
144924afba76SLiad Kaufman 				       struct ieee80211_vif *vif,
145024afba76SLiad Kaufman 				       struct iwl_mvm_sta *mvm_sta)
145124afba76SLiad Kaufman {
145224afba76SLiad Kaufman 	int ac;
145324afba76SLiad Kaufman 	int i;
145424afba76SLiad Kaufman 
145524afba76SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
145624afba76SLiad Kaufman 
145724afba76SLiad Kaufman 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
145824afba76SLiad Kaufman 		if (mvm_sta->tid_data[i].txq_id == IEEE80211_INVAL_HW_QUEUE)
145924afba76SLiad Kaufman 			continue;
146024afba76SLiad Kaufman 
146124afba76SLiad Kaufman 		ac = iwl_mvm_tid_to_ac_queue(i);
146224afba76SLiad Kaufman 		iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
146324afba76SLiad Kaufman 				    vif->hw_queue[ac], i, 0);
146424afba76SLiad Kaufman 		mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
146524afba76SLiad Kaufman 	}
146624afba76SLiad Kaufman }
146724afba76SLiad Kaufman 
1468e705c121SKalle Valo int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1469e705c121SKalle Valo 		   struct ieee80211_vif *vif,
1470e705c121SKalle Valo 		   struct ieee80211_sta *sta)
1471e705c121SKalle Valo {
1472e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1473e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1474e705c121SKalle Valo 	int ret;
1475e705c121SKalle Valo 
1476e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1477e705c121SKalle Valo 
1478a571f5f6SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
1479a571f5f6SSara Sharon 		kfree(mvm_sta->dup_data);
1480a571f5f6SSara Sharon 
1481a6f035a0SLiad Kaufman 	if ((vif->type == NL80211_IFTYPE_STATION &&
1482a6f035a0SLiad Kaufman 	     mvmvif->ap_sta_id == mvm_sta->sta_id) ||
1483a6f035a0SLiad Kaufman 	    iwl_mvm_is_dqa_supported(mvm)){
1484e705c121SKalle Valo 		ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1485e705c121SKalle Valo 		if (ret)
1486e705c121SKalle Valo 			return ret;
1487e705c121SKalle Valo 		/* flush its queues here since we are freeing mvm_sta */
1488e705c121SKalle Valo 		ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, 0);
1489e705c121SKalle Valo 		if (ret)
1490e705c121SKalle Valo 			return ret;
1491e705c121SKalle Valo 		ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
1492e705c121SKalle Valo 						    mvm_sta->tfd_queue_msk);
1493e705c121SKalle Valo 		if (ret)
1494e705c121SKalle Valo 			return ret;
1495e705c121SKalle Valo 		ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1496e705c121SKalle Valo 
149724afba76SLiad Kaufman 		/* If DQA is supported - the queues can be disabled now */
149856214749SLiad Kaufman 		if (iwl_mvm_is_dqa_supported(mvm))
149956214749SLiad Kaufman 			iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
150056214749SLiad Kaufman 
150156214749SLiad Kaufman 		/* If there is a TXQ still marked as reserved - free it */
150256214749SLiad Kaufman 		if (iwl_mvm_is_dqa_supported(mvm) &&
150356214749SLiad Kaufman 		    mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1504a0315deaSLiad Kaufman 			u8 reserved_txq = mvm_sta->reserved_queue;
1505a0315deaSLiad Kaufman 			enum iwl_mvm_queue_status *status;
1506a0315deaSLiad Kaufman 
1507a0315deaSLiad Kaufman 			/*
1508a0315deaSLiad Kaufman 			 * If no traffic has gone through the reserved TXQ - it
1509a0315deaSLiad Kaufman 			 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1510a0315deaSLiad Kaufman 			 * should be manually marked as free again
1511a0315deaSLiad Kaufman 			 */
1512a0315deaSLiad Kaufman 			spin_lock_bh(&mvm->queue_info_lock);
1513a0315deaSLiad Kaufman 			status = &mvm->queue_info[reserved_txq].status;
1514a0315deaSLiad Kaufman 			if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1515a0315deaSLiad Kaufman 				 (*status != IWL_MVM_QUEUE_FREE),
1516a0315deaSLiad Kaufman 				 "sta_id %d reserved txq %d status %d",
1517a0315deaSLiad Kaufman 				 mvm_sta->sta_id, reserved_txq, *status)) {
1518a0315deaSLiad Kaufman 				spin_unlock_bh(&mvm->queue_info_lock);
1519a0315deaSLiad Kaufman 				return -EINVAL;
1520a0315deaSLiad Kaufman 			}
1521a0315deaSLiad Kaufman 
1522a0315deaSLiad Kaufman 			*status = IWL_MVM_QUEUE_FREE;
1523a0315deaSLiad Kaufman 			spin_unlock_bh(&mvm->queue_info_lock);
1524a0315deaSLiad Kaufman 		}
1525a0315deaSLiad Kaufman 
1526e3118ad7SLiad Kaufman 		if (vif->type == NL80211_IFTYPE_STATION &&
1527e3118ad7SLiad Kaufman 		    mvmvif->ap_sta_id == mvm_sta->sta_id) {
1528e3118ad7SLiad Kaufman 			/* if associated - we can't remove the AP STA now */
1529e705c121SKalle Valo 			if (vif->bss_conf.assoc)
1530e705c121SKalle Valo 				return ret;
1531e705c121SKalle Valo 
1532e705c121SKalle Valo 			/* unassoc - go ahead - remove the AP STA now */
1533e705c121SKalle Valo 			mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
1534e705c121SKalle Valo 
1535e705c121SKalle Valo 			/* clear d0i3_ap_sta_id if no longer relevant */
1536e705c121SKalle Valo 			if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
1537e705c121SKalle Valo 				mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
1538e705c121SKalle Valo 		}
1539e3118ad7SLiad Kaufman 	}
1540e705c121SKalle Valo 
1541e705c121SKalle Valo 	/*
1542e705c121SKalle Valo 	 * This shouldn't happen - the TDLS channel switch should be canceled
1543e705c121SKalle Valo 	 * before the STA is removed.
1544e705c121SKalle Valo 	 */
1545e705c121SKalle Valo 	if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
1546e705c121SKalle Valo 		mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
1547e705c121SKalle Valo 		cancel_delayed_work(&mvm->tdls_cs.dwork);
1548e705c121SKalle Valo 	}
1549e705c121SKalle Valo 
1550e705c121SKalle Valo 	/*
1551e705c121SKalle Valo 	 * Make sure that the tx response code sees the station as -EBUSY and
1552e705c121SKalle Valo 	 * calls the drain worker.
1553e705c121SKalle Valo 	 */
1554e705c121SKalle Valo 	spin_lock_bh(&mvm_sta->lock);
1555e705c121SKalle Valo 	/*
1556e705c121SKalle Valo 	 * There are frames pending on the AC queues for this station.
1557e705c121SKalle Valo 	 * We need to wait until all the frames are drained...
1558e705c121SKalle Valo 	 */
1559e705c121SKalle Valo 	if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
1560e705c121SKalle Valo 		rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
1561e705c121SKalle Valo 				   ERR_PTR(-EBUSY));
1562e705c121SKalle Valo 		spin_unlock_bh(&mvm_sta->lock);
1563e705c121SKalle Valo 
1564e705c121SKalle Valo 		/* disable TDLS sta queues on drain complete */
1565e705c121SKalle Valo 		if (sta->tdls) {
1566e705c121SKalle Valo 			mvm->tfd_drained[mvm_sta->sta_id] =
1567e705c121SKalle Valo 							mvm_sta->tfd_queue_msk;
1568e705c121SKalle Valo 			IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
1569e705c121SKalle Valo 				       mvm_sta->sta_id);
1570e705c121SKalle Valo 		}
1571e705c121SKalle Valo 
1572e705c121SKalle Valo 		ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1573e705c121SKalle Valo 	} else {
1574e705c121SKalle Valo 		spin_unlock_bh(&mvm_sta->lock);
1575e705c121SKalle Valo 
1576e3118ad7SLiad Kaufman 		if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
1577e705c121SKalle Valo 			iwl_mvm_tdls_sta_deinit(mvm, sta);
1578e705c121SKalle Valo 
1579e705c121SKalle Valo 		ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1580e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1581e705c121SKalle Valo 	}
1582e705c121SKalle Valo 
1583e705c121SKalle Valo 	return ret;
1584e705c121SKalle Valo }
1585e705c121SKalle Valo 
1586e705c121SKalle Valo int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1587e705c121SKalle Valo 		      struct ieee80211_vif *vif,
1588e705c121SKalle Valo 		      u8 sta_id)
1589e705c121SKalle Valo {
1590e705c121SKalle Valo 	int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1591e705c121SKalle Valo 
1592e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1593e705c121SKalle Valo 
1594e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1595e705c121SKalle Valo 	return ret;
1596e705c121SKalle Valo }
1597e705c121SKalle Valo 
15980e39eb03SChaya Rachel Ivgi int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1599e705c121SKalle Valo 			     struct iwl_mvm_int_sta *sta,
1600e705c121SKalle Valo 			     u32 qmask, enum nl80211_iftype iftype)
1601e705c121SKalle Valo {
1602e705c121SKalle Valo 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1603e705c121SKalle Valo 		sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
1604e705c121SKalle Valo 		if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
1605e705c121SKalle Valo 			return -ENOSPC;
1606e705c121SKalle Valo 	}
1607e705c121SKalle Valo 
1608e705c121SKalle Valo 	sta->tfd_queue_msk = qmask;
1609e705c121SKalle Valo 
1610e705c121SKalle Valo 	/* put a non-NULL value so iterating over the stations won't stop */
1611e705c121SKalle Valo 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1612e705c121SKalle Valo 	return 0;
1613e705c121SKalle Valo }
1614e705c121SKalle Valo 
1615e705c121SKalle Valo static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
1616e705c121SKalle Valo 				    struct iwl_mvm_int_sta *sta)
1617e705c121SKalle Valo {
1618e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
1619e705c121SKalle Valo 	memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
1620e705c121SKalle Valo 	sta->sta_id = IWL_MVM_STATION_COUNT;
1621e705c121SKalle Valo }
1622e705c121SKalle Valo 
1623e705c121SKalle Valo static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1624e705c121SKalle Valo 				      struct iwl_mvm_int_sta *sta,
1625e705c121SKalle Valo 				      const u8 *addr,
1626e705c121SKalle Valo 				      u16 mac_id, u16 color)
1627e705c121SKalle Valo {
1628e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd;
1629e705c121SKalle Valo 	int ret;
1630e705c121SKalle Valo 	u32 status;
1631e705c121SKalle Valo 
1632e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1633e705c121SKalle Valo 
1634e705c121SKalle Valo 	memset(&cmd, 0, sizeof(cmd));
1635e705c121SKalle Valo 	cmd.sta_id = sta->sta_id;
1636e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1637e705c121SKalle Valo 							     color));
1638e705c121SKalle Valo 
1639e705c121SKalle Valo 	cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1640cf0cda19SLiad Kaufman 	cmd.tid_disable_tx = cpu_to_le16(0xffff);
1641e705c121SKalle Valo 
1642e705c121SKalle Valo 	if (addr)
1643e705c121SKalle Valo 		memcpy(cmd.addr, addr, ETH_ALEN);
1644e705c121SKalle Valo 
1645854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1646854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
1647e705c121SKalle Valo 					  &cmd, &status);
1648e705c121SKalle Valo 	if (ret)
1649e705c121SKalle Valo 		return ret;
1650e705c121SKalle Valo 
1651837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1652e705c121SKalle Valo 	case ADD_STA_SUCCESS:
1653e705c121SKalle Valo 		IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1654e705c121SKalle Valo 		return 0;
1655e705c121SKalle Valo 	default:
1656e705c121SKalle Valo 		ret = -EIO;
1657e705c121SKalle Valo 		IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1658e705c121SKalle Valo 			status);
1659e705c121SKalle Valo 		break;
1660e705c121SKalle Valo 	}
1661e705c121SKalle Valo 	return ret;
1662e705c121SKalle Valo }
1663e705c121SKalle Valo 
1664e705c121SKalle Valo int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1665e705c121SKalle Valo {
1666e705c121SKalle Valo 	unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1667e705c121SKalle Valo 					mvm->cfg->base_params->wd_timeout :
1668e705c121SKalle Valo 					IWL_WATCHDOG_DISABLED;
1669e705c121SKalle Valo 	int ret;
1670e705c121SKalle Valo 
1671e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1672e705c121SKalle Valo 
1673e705c121SKalle Valo 	/* Map Aux queue to fifo - needs to happen before adding Aux station */
167428d0793eSLiad Kaufman 	if (!iwl_mvm_is_dqa_supported(mvm))
1675e705c121SKalle Valo 		iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
1676e705c121SKalle Valo 				      IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
1677e705c121SKalle Valo 
1678e705c121SKalle Valo 	/* Allocate aux station and assign to it the aux queue */
1679e705c121SKalle Valo 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
1680e705c121SKalle Valo 				       NL80211_IFTYPE_UNSPECIFIED);
1681e705c121SKalle Valo 	if (ret)
1682e705c121SKalle Valo 		return ret;
1683e705c121SKalle Valo 
168428d0793eSLiad Kaufman 	if (iwl_mvm_is_dqa_supported(mvm)) {
168528d0793eSLiad Kaufman 		struct iwl_trans_txq_scd_cfg cfg = {
168628d0793eSLiad Kaufman 			.fifo = IWL_MVM_TX_FIFO_MCAST,
168728d0793eSLiad Kaufman 			.sta_id = mvm->aux_sta.sta_id,
168828d0793eSLiad Kaufman 			.tid = IWL_MAX_TID_COUNT,
168928d0793eSLiad Kaufman 			.aggregate = false,
169028d0793eSLiad Kaufman 			.frame_limit = IWL_FRAME_LIMIT,
169128d0793eSLiad Kaufman 		};
169228d0793eSLiad Kaufman 
169328d0793eSLiad Kaufman 		iwl_mvm_enable_txq(mvm, mvm->aux_queue, mvm->aux_queue, 0, &cfg,
169428d0793eSLiad Kaufman 				   wdg_timeout);
169528d0793eSLiad Kaufman 	}
169628d0793eSLiad Kaufman 
1697e705c121SKalle Valo 	ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
1698e705c121SKalle Valo 					 MAC_INDEX_AUX, 0);
1699e705c121SKalle Valo 
1700e705c121SKalle Valo 	if (ret)
1701e705c121SKalle Valo 		iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1702e705c121SKalle Valo 	return ret;
1703e705c121SKalle Valo }
1704e705c121SKalle Valo 
17050e39eb03SChaya Rachel Ivgi int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
17060e39eb03SChaya Rachel Ivgi {
17070e39eb03SChaya Rachel Ivgi 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
17080e39eb03SChaya Rachel Ivgi 
17090e39eb03SChaya Rachel Ivgi 	lockdep_assert_held(&mvm->mutex);
17100e39eb03SChaya Rachel Ivgi 	return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
17110e39eb03SChaya Rachel Ivgi 					 mvmvif->id, 0);
17120e39eb03SChaya Rachel Ivgi }
17130e39eb03SChaya Rachel Ivgi 
17140e39eb03SChaya Rachel Ivgi int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
17150e39eb03SChaya Rachel Ivgi {
17160e39eb03SChaya Rachel Ivgi 	int ret;
17170e39eb03SChaya Rachel Ivgi 
17180e39eb03SChaya Rachel Ivgi 	lockdep_assert_held(&mvm->mutex);
17190e39eb03SChaya Rachel Ivgi 
17200e39eb03SChaya Rachel Ivgi 	ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
17210e39eb03SChaya Rachel Ivgi 	if (ret)
17220e39eb03SChaya Rachel Ivgi 		IWL_WARN(mvm, "Failed sending remove station\n");
17230e39eb03SChaya Rachel Ivgi 
17240e39eb03SChaya Rachel Ivgi 	return ret;
17250e39eb03SChaya Rachel Ivgi }
17260e39eb03SChaya Rachel Ivgi 
17270e39eb03SChaya Rachel Ivgi void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
17280e39eb03SChaya Rachel Ivgi {
17290e39eb03SChaya Rachel Ivgi 	iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
17300e39eb03SChaya Rachel Ivgi }
17310e39eb03SChaya Rachel Ivgi 
1732e705c121SKalle Valo void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
1733e705c121SKalle Valo {
1734e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1735e705c121SKalle Valo 
1736e705c121SKalle Valo 	iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1737e705c121SKalle Valo }
1738e705c121SKalle Valo 
1739e705c121SKalle Valo /*
1740e705c121SKalle Valo  * Send the add station command for the vif's broadcast station.
1741e705c121SKalle Valo  * Assumes that the station was already allocated.
1742e705c121SKalle Valo  *
1743e705c121SKalle Valo  * @mvm: the mvm component
1744e705c121SKalle Valo  * @vif: the interface to which the broadcast station is added
1745e705c121SKalle Valo  * @bsta: the broadcast station to add.
1746e705c121SKalle Valo  */
1747e705c121SKalle Valo int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1748e705c121SKalle Valo {
1749e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1750e705c121SKalle Valo 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
1751e705c121SKalle Valo 	static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1752e705c121SKalle Valo 	const u8 *baddr = _baddr;
1753e705c121SKalle Valo 
1754e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1755e705c121SKalle Valo 
1756de24f638SLiad Kaufman 	if (iwl_mvm_is_dqa_supported(mvm)) {
1757de24f638SLiad Kaufman 		struct iwl_trans_txq_scd_cfg cfg = {
1758de24f638SLiad Kaufman 			.fifo = IWL_MVM_TX_FIFO_VO,
1759de24f638SLiad Kaufman 			.sta_id = mvmvif->bcast_sta.sta_id,
1760de24f638SLiad Kaufman 			.tid = IWL_MAX_TID_COUNT,
1761de24f638SLiad Kaufman 			.aggregate = false,
1762de24f638SLiad Kaufman 			.frame_limit = IWL_FRAME_LIMIT,
1763de24f638SLiad Kaufman 		};
1764de24f638SLiad Kaufman 		unsigned int wdg_timeout =
1765de24f638SLiad Kaufman 			iwl_mvm_get_wd_timeout(mvm, vif, false, false);
1766de24f638SLiad Kaufman 		int queue;
1767de24f638SLiad Kaufman 
1768de24f638SLiad Kaufman 		if ((vif->type == NL80211_IFTYPE_AP) &&
1769de24f638SLiad Kaufman 		    (mvmvif->bcast_sta.tfd_queue_msk &
1770de24f638SLiad Kaufman 		     BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE)))
1771de24f638SLiad Kaufman 			queue = IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
17724c965139SLiad Kaufman 		else if ((vif->type == NL80211_IFTYPE_P2P_DEVICE) &&
17734c965139SLiad Kaufman 			 (mvmvif->bcast_sta.tfd_queue_msk &
17744c965139SLiad Kaufman 			  BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE)))
17754c965139SLiad Kaufman 			queue = IWL_MVM_DQA_P2P_DEVICE_QUEUE;
1776de24f638SLiad Kaufman 		else if (WARN(1, "Missed required TXQ for adding bcast STA\n"))
1777de24f638SLiad Kaufman 			return -EINVAL;
1778de24f638SLiad Kaufman 
1779de24f638SLiad Kaufman 		iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0, &cfg,
1780de24f638SLiad Kaufman 				   wdg_timeout);
1781de24f638SLiad Kaufman 	}
1782de24f638SLiad Kaufman 
1783e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_ADHOC)
1784e705c121SKalle Valo 		baddr = vif->bss_conf.bssid;
1785e705c121SKalle Valo 
1786e705c121SKalle Valo 	if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
1787e705c121SKalle Valo 		return -ENOSPC;
1788e705c121SKalle Valo 
1789e705c121SKalle Valo 	return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
1790e705c121SKalle Valo 					  mvmvif->id, mvmvif->color);
1791e705c121SKalle Valo }
1792e705c121SKalle Valo 
1793e705c121SKalle Valo /* Send the FW a request to remove the station from it's internal data
1794e705c121SKalle Valo  * structures, but DO NOT remove the entry from the local data structures. */
1795e705c121SKalle Valo int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1796e705c121SKalle Valo {
1797e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1798e705c121SKalle Valo 	int ret;
1799e705c121SKalle Valo 
1800e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1801e705c121SKalle Valo 
1802e705c121SKalle Valo 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
1803e705c121SKalle Valo 	if (ret)
1804e705c121SKalle Valo 		IWL_WARN(mvm, "Failed sending remove station\n");
1805e705c121SKalle Valo 	return ret;
1806e705c121SKalle Valo }
1807e705c121SKalle Valo 
1808e705c121SKalle Valo int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1809e705c121SKalle Valo {
1810e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1811de24f638SLiad Kaufman 	u32 qmask = 0;
1812e705c121SKalle Valo 
1813e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1814e705c121SKalle Valo 
1815de24f638SLiad Kaufman 	if (!iwl_mvm_is_dqa_supported(mvm))
1816e705c121SKalle Valo 		qmask = iwl_mvm_mac_get_queues_mask(vif);
1817e705c121SKalle Valo 
1818de24f638SLiad Kaufman 	if (vif->type == NL80211_IFTYPE_AP) {
1819e705c121SKalle Valo 		/*
1820e705c121SKalle Valo 		 * The firmware defines the TFD queue mask to only be relevant
1821e705c121SKalle Valo 		 * for *unicast* queues, so the multicast (CAB) queue shouldn't
1822e705c121SKalle Valo 		 * be included.
1823e705c121SKalle Valo 		 */
1824e705c121SKalle Valo 		qmask &= ~BIT(vif->cab_queue);
1825e705c121SKalle Valo 
1826de24f638SLiad Kaufman 		if (iwl_mvm_is_dqa_supported(mvm))
1827de24f638SLiad Kaufman 			qmask |= BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE);
18284c965139SLiad Kaufman 	} else if (iwl_mvm_is_dqa_supported(mvm) &&
18294c965139SLiad Kaufman 		   vif->type == NL80211_IFTYPE_P2P_DEVICE) {
18304c965139SLiad Kaufman 		qmask |= BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE);
1831de24f638SLiad Kaufman 	}
1832de24f638SLiad Kaufman 
1833e705c121SKalle Valo 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
1834e705c121SKalle Valo 					ieee80211_vif_type_p2p(vif));
1835e705c121SKalle Valo }
1836e705c121SKalle Valo 
1837e705c121SKalle Valo /* Allocate a new station entry for the broadcast station to the given vif,
1838e705c121SKalle Valo  * and send it to the FW.
1839e705c121SKalle Valo  * Note that each P2P mac should have its own broadcast station.
1840e705c121SKalle Valo  *
1841e705c121SKalle Valo  * @mvm: the mvm component
1842e705c121SKalle Valo  * @vif: the interface to which the broadcast station is added
1843e705c121SKalle Valo  * @bsta: the broadcast station to add. */
1844e705c121SKalle Valo int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1845e705c121SKalle Valo {
1846e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1847e705c121SKalle Valo 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
1848e705c121SKalle Valo 	int ret;
1849e705c121SKalle Valo 
1850e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1851e705c121SKalle Valo 
1852e705c121SKalle Valo 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1853e705c121SKalle Valo 	if (ret)
1854e705c121SKalle Valo 		return ret;
1855e705c121SKalle Valo 
1856e705c121SKalle Valo 	ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
1857e705c121SKalle Valo 
1858e705c121SKalle Valo 	if (ret)
1859e705c121SKalle Valo 		iwl_mvm_dealloc_int_sta(mvm, bsta);
1860e705c121SKalle Valo 
1861e705c121SKalle Valo 	return ret;
1862e705c121SKalle Valo }
1863e705c121SKalle Valo 
1864e705c121SKalle Valo void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1865e705c121SKalle Valo {
1866e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1867e705c121SKalle Valo 
1868e705c121SKalle Valo 	iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
1869e705c121SKalle Valo }
1870e705c121SKalle Valo 
1871e705c121SKalle Valo /*
1872e705c121SKalle Valo  * Send the FW a request to remove the station from it's internal data
1873e705c121SKalle Valo  * structures, and in addition remove it from the local data structure.
1874e705c121SKalle Valo  */
1875e705c121SKalle Valo int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1876e705c121SKalle Valo {
1877e705c121SKalle Valo 	int ret;
1878e705c121SKalle Valo 
1879e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1880e705c121SKalle Valo 
1881e705c121SKalle Valo 	ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
1882e705c121SKalle Valo 
1883e705c121SKalle Valo 	iwl_mvm_dealloc_bcast_sta(mvm, vif);
1884e705c121SKalle Valo 
1885e705c121SKalle Valo 	return ret;
1886e705c121SKalle Valo }
1887e705c121SKalle Valo 
1888e705c121SKalle Valo #define IWL_MAX_RX_BA_SESSIONS 16
1889e705c121SKalle Valo 
1890b915c101SSara Sharon static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
189110b2b201SSara Sharon {
1892b915c101SSara Sharon 	struct iwl_mvm_delba_notif notif = {
1893b915c101SSara Sharon 		.metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
1894b915c101SSara Sharon 		.metadata.sync = 1,
1895b915c101SSara Sharon 		.delba.baid = baid,
1896b915c101SSara Sharon 	};
1897b915c101SSara Sharon 	iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
189810b2b201SSara Sharon };
189910b2b201SSara Sharon 
1900b915c101SSara Sharon static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
1901b915c101SSara Sharon 				 struct iwl_mvm_baid_data *data)
1902b915c101SSara Sharon {
1903b915c101SSara Sharon 	int i;
1904b915c101SSara Sharon 
1905b915c101SSara Sharon 	iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
1906b915c101SSara Sharon 
1907b915c101SSara Sharon 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
1908b915c101SSara Sharon 		int j;
1909b915c101SSara Sharon 		struct iwl_mvm_reorder_buffer *reorder_buf =
1910b915c101SSara Sharon 			&data->reorder_buf[i];
1911b915c101SSara Sharon 
19120690405fSSara Sharon 		spin_lock_bh(&reorder_buf->lock);
19130690405fSSara Sharon 		if (likely(!reorder_buf->num_stored)) {
19140690405fSSara Sharon 			spin_unlock_bh(&reorder_buf->lock);
1915b915c101SSara Sharon 			continue;
19160690405fSSara Sharon 		}
1917b915c101SSara Sharon 
1918b915c101SSara Sharon 		/*
1919b915c101SSara Sharon 		 * This shouldn't happen in regular DELBA since the internal
1920b915c101SSara Sharon 		 * delBA notification should trigger a release of all frames in
1921b915c101SSara Sharon 		 * the reorder buffer.
1922b915c101SSara Sharon 		 */
1923b915c101SSara Sharon 		WARN_ON(1);
1924b915c101SSara Sharon 
1925b915c101SSara Sharon 		for (j = 0; j < reorder_buf->buf_size; j++)
1926b915c101SSara Sharon 			__skb_queue_purge(&reorder_buf->entries[j]);
19270690405fSSara Sharon 		/*
19280690405fSSara Sharon 		 * Prevent timer re-arm. This prevents a very far fetched case
19290690405fSSara Sharon 		 * where we timed out on the notification. There may be prior
19300690405fSSara Sharon 		 * RX frames pending in the RX queue before the notification
19310690405fSSara Sharon 		 * that might get processed between now and the actual deletion
19320690405fSSara Sharon 		 * and we would re-arm the timer although we are deleting the
19330690405fSSara Sharon 		 * reorder buffer.
19340690405fSSara Sharon 		 */
19350690405fSSara Sharon 		reorder_buf->removed = true;
19360690405fSSara Sharon 		spin_unlock_bh(&reorder_buf->lock);
19370690405fSSara Sharon 		del_timer_sync(&reorder_buf->reorder_timer);
1938b915c101SSara Sharon 	}
1939b915c101SSara Sharon }
1940b915c101SSara Sharon 
1941b915c101SSara Sharon static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
1942b915c101SSara Sharon 					u32 sta_id,
1943b915c101SSara Sharon 					struct iwl_mvm_baid_data *data,
1944b915c101SSara Sharon 					u16 ssn, u8 buf_size)
1945b915c101SSara Sharon {
1946b915c101SSara Sharon 	int i;
1947b915c101SSara Sharon 
1948b915c101SSara Sharon 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
1949b915c101SSara Sharon 		struct iwl_mvm_reorder_buffer *reorder_buf =
1950b915c101SSara Sharon 			&data->reorder_buf[i];
1951b915c101SSara Sharon 		int j;
1952b915c101SSara Sharon 
1953b915c101SSara Sharon 		reorder_buf->num_stored = 0;
1954b915c101SSara Sharon 		reorder_buf->head_sn = ssn;
1955b915c101SSara Sharon 		reorder_buf->buf_size = buf_size;
19560690405fSSara Sharon 		/* rx reorder timer */
19570690405fSSara Sharon 		reorder_buf->reorder_timer.function =
19580690405fSSara Sharon 			iwl_mvm_reorder_timer_expired;
19590690405fSSara Sharon 		reorder_buf->reorder_timer.data = (unsigned long)reorder_buf;
19600690405fSSara Sharon 		init_timer(&reorder_buf->reorder_timer);
19610690405fSSara Sharon 		spin_lock_init(&reorder_buf->lock);
19620690405fSSara Sharon 		reorder_buf->mvm = mvm;
1963b915c101SSara Sharon 		reorder_buf->queue = i;
1964b915c101SSara Sharon 		reorder_buf->sta_id = sta_id;
1965b915c101SSara Sharon 		for (j = 0; j < reorder_buf->buf_size; j++)
1966b915c101SSara Sharon 			__skb_queue_head_init(&reorder_buf->entries[j]);
1967b915c101SSara Sharon 	}
196810b2b201SSara Sharon }
196910b2b201SSara Sharon 
1970e705c121SKalle Valo int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
197110b2b201SSara Sharon 		       int tid, u16 ssn, bool start, u8 buf_size, u16 timeout)
1972e705c121SKalle Valo {
1973e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1974e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
197510b2b201SSara Sharon 	struct iwl_mvm_baid_data *baid_data = NULL;
1976e705c121SKalle Valo 	int ret;
1977e705c121SKalle Valo 	u32 status;
1978e705c121SKalle Valo 
1979e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1980e705c121SKalle Valo 
1981e705c121SKalle Valo 	if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
1982e705c121SKalle Valo 		IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
1983e705c121SKalle Valo 		return -ENOSPC;
1984e705c121SKalle Valo 	}
1985e705c121SKalle Valo 
198610b2b201SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) && start) {
198710b2b201SSara Sharon 		/*
198810b2b201SSara Sharon 		 * Allocate here so if allocation fails we can bail out early
198910b2b201SSara Sharon 		 * before starting the BA session in the firmware
199010b2b201SSara Sharon 		 */
1991b915c101SSara Sharon 		baid_data = kzalloc(sizeof(*baid_data) +
1992b915c101SSara Sharon 				    mvm->trans->num_rx_queues *
1993b915c101SSara Sharon 				    sizeof(baid_data->reorder_buf[0]),
1994b915c101SSara Sharon 				    GFP_KERNEL);
199510b2b201SSara Sharon 		if (!baid_data)
199610b2b201SSara Sharon 			return -ENOMEM;
199710b2b201SSara Sharon 	}
199810b2b201SSara Sharon 
1999e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2000e705c121SKalle Valo 	cmd.sta_id = mvm_sta->sta_id;
2001e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
2002e705c121SKalle Valo 	if (start) {
2003e705c121SKalle Valo 		cmd.add_immediate_ba_tid = (u8) tid;
2004e705c121SKalle Valo 		cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
2005854c5705SSara Sharon 		cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
2006e705c121SKalle Valo 	} else {
2007e705c121SKalle Valo 		cmd.remove_immediate_ba_tid = (u8) tid;
2008e705c121SKalle Valo 	}
2009e705c121SKalle Valo 	cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2010e705c121SKalle Valo 				  STA_MODIFY_REMOVE_BA_TID;
2011e705c121SKalle Valo 
2012e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
2013854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2014854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
2015e705c121SKalle Valo 					  &cmd, &status);
2016e705c121SKalle Valo 	if (ret)
201710b2b201SSara Sharon 		goto out_free;
2018e705c121SKalle Valo 
2019837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2020e705c121SKalle Valo 	case ADD_STA_SUCCESS:
202135263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2022e705c121SKalle Valo 			     start ? "start" : "stopp");
2023e705c121SKalle Valo 		break;
2024e705c121SKalle Valo 	case ADD_STA_IMMEDIATE_BA_FAILURE:
2025e705c121SKalle Valo 		IWL_WARN(mvm, "RX BA Session refused by fw\n");
2026e705c121SKalle Valo 		ret = -ENOSPC;
2027e705c121SKalle Valo 		break;
2028e705c121SKalle Valo 	default:
2029e705c121SKalle Valo 		ret = -EIO;
2030e705c121SKalle Valo 		IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2031e705c121SKalle Valo 			start ? "start" : "stopp", status);
2032e705c121SKalle Valo 		break;
2033e705c121SKalle Valo 	}
2034e705c121SKalle Valo 
203510b2b201SSara Sharon 	if (ret)
203610b2b201SSara Sharon 		goto out_free;
203710b2b201SSara Sharon 
203810b2b201SSara Sharon 	if (start) {
203910b2b201SSara Sharon 		u8 baid;
204010b2b201SSara Sharon 
2041e705c121SKalle Valo 		mvm->rx_ba_sessions++;
204210b2b201SSara Sharon 
204310b2b201SSara Sharon 		if (!iwl_mvm_has_new_rx_api(mvm))
204410b2b201SSara Sharon 			return 0;
204510b2b201SSara Sharon 
204610b2b201SSara Sharon 		if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
204710b2b201SSara Sharon 			ret = -EINVAL;
204810b2b201SSara Sharon 			goto out_free;
204910b2b201SSara Sharon 		}
205010b2b201SSara Sharon 		baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
205110b2b201SSara Sharon 			    IWL_ADD_STA_BAID_SHIFT);
205210b2b201SSara Sharon 		baid_data->baid = baid;
205310b2b201SSara Sharon 		baid_data->timeout = timeout;
205410b2b201SSara Sharon 		baid_data->last_rx = jiffies;
205572c240feSWei Yongjun 		setup_timer(&baid_data->session_timer,
205672c240feSWei Yongjun 			    iwl_mvm_rx_agg_session_expired,
205772c240feSWei Yongjun 			    (unsigned long)&mvm->baid_map[baid]);
205810b2b201SSara Sharon 		baid_data->mvm = mvm;
205910b2b201SSara Sharon 		baid_data->tid = tid;
206010b2b201SSara Sharon 		baid_data->sta_id = mvm_sta->sta_id;
206110b2b201SSara Sharon 
206210b2b201SSara Sharon 		mvm_sta->tid_to_baid[tid] = baid;
206310b2b201SSara Sharon 		if (timeout)
206410b2b201SSara Sharon 			mod_timer(&baid_data->session_timer,
206510b2b201SSara Sharon 				  TU_TO_EXP_TIME(timeout * 2));
206610b2b201SSara Sharon 
2067b915c101SSara Sharon 		iwl_mvm_init_reorder_buffer(mvm, mvm_sta->sta_id,
2068b915c101SSara Sharon 					    baid_data, ssn, buf_size);
206910b2b201SSara Sharon 		/*
207010b2b201SSara Sharon 		 * protect the BA data with RCU to cover a case where our
207110b2b201SSara Sharon 		 * internal RX sync mechanism will timeout (not that it's
207210b2b201SSara Sharon 		 * supposed to happen) and we will free the session data while
207310b2b201SSara Sharon 		 * RX is being processed in parallel
207410b2b201SSara Sharon 		 */
207535263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
207635263a03SSara Sharon 			     mvm_sta->sta_id, tid, baid);
207710b2b201SSara Sharon 		WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
207810b2b201SSara Sharon 		rcu_assign_pointer(mvm->baid_map[baid], baid_data);
207960dec523SSara Sharon 	} else  {
208010b2b201SSara Sharon 		u8 baid = mvm_sta->tid_to_baid[tid];
208110b2b201SSara Sharon 
208260dec523SSara Sharon 		if (mvm->rx_ba_sessions > 0)
2083e705c121SKalle Valo 			/* check that restart flow didn't zero the counter */
2084e705c121SKalle Valo 			mvm->rx_ba_sessions--;
208510b2b201SSara Sharon 		if (!iwl_mvm_has_new_rx_api(mvm))
208610b2b201SSara Sharon 			return 0;
2087e705c121SKalle Valo 
208810b2b201SSara Sharon 		if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
208910b2b201SSara Sharon 			return -EINVAL;
209010b2b201SSara Sharon 
209110b2b201SSara Sharon 		baid_data = rcu_access_pointer(mvm->baid_map[baid]);
209210b2b201SSara Sharon 		if (WARN_ON(!baid_data))
209310b2b201SSara Sharon 			return -EINVAL;
209410b2b201SSara Sharon 
209510b2b201SSara Sharon 		/* synchronize all rx queues so we can safely delete */
2096b915c101SSara Sharon 		iwl_mvm_free_reorder(mvm, baid_data);
209710b2b201SSara Sharon 		del_timer_sync(&baid_data->session_timer);
209810b2b201SSara Sharon 		RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
209910b2b201SSara Sharon 		kfree_rcu(baid_data, rcu_head);
210035263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
210110b2b201SSara Sharon 	}
210210b2b201SSara Sharon 	return 0;
210310b2b201SSara Sharon 
210410b2b201SSara Sharon out_free:
210510b2b201SSara Sharon 	kfree(baid_data);
2106e705c121SKalle Valo 	return ret;
2107e705c121SKalle Valo }
2108e705c121SKalle Valo 
21099794c64fSLiad Kaufman int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2110e705c121SKalle Valo 		       int tid, u8 queue, bool start)
2111e705c121SKalle Valo {
2112e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2113e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
2114e705c121SKalle Valo 	int ret;
2115e705c121SKalle Valo 	u32 status;
2116e705c121SKalle Valo 
2117e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2118e705c121SKalle Valo 
2119e705c121SKalle Valo 	if (start) {
2120e705c121SKalle Valo 		mvm_sta->tfd_queue_msk |= BIT(queue);
2121e705c121SKalle Valo 		mvm_sta->tid_disable_agg &= ~BIT(tid);
2122e705c121SKalle Valo 	} else {
2123cf961e16SLiad Kaufman 		/* In DQA-mode the queue isn't removed on agg termination */
2124cf961e16SLiad Kaufman 		if (!iwl_mvm_is_dqa_supported(mvm))
2125e705c121SKalle Valo 			mvm_sta->tfd_queue_msk &= ~BIT(queue);
2126e705c121SKalle Valo 		mvm_sta->tid_disable_agg |= BIT(tid);
2127e705c121SKalle Valo 	}
2128e705c121SKalle Valo 
2129e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2130e705c121SKalle Valo 	cmd.sta_id = mvm_sta->sta_id;
2131e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
2132e705c121SKalle Valo 	cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
2133e705c121SKalle Valo 	cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2134e705c121SKalle Valo 	cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2135e705c121SKalle Valo 
2136e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
2137854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2138854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
2139e705c121SKalle Valo 					  &cmd, &status);
2140e705c121SKalle Valo 	if (ret)
2141e705c121SKalle Valo 		return ret;
2142e705c121SKalle Valo 
2143837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2144e705c121SKalle Valo 	case ADD_STA_SUCCESS:
2145e705c121SKalle Valo 		break;
2146e705c121SKalle Valo 	default:
2147e705c121SKalle Valo 		ret = -EIO;
2148e705c121SKalle Valo 		IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2149e705c121SKalle Valo 			start ? "start" : "stopp", status);
2150e705c121SKalle Valo 		break;
2151e705c121SKalle Valo 	}
2152e705c121SKalle Valo 
2153e705c121SKalle Valo 	return ret;
2154e705c121SKalle Valo }
2155e705c121SKalle Valo 
2156e705c121SKalle Valo const u8 tid_to_mac80211_ac[] = {
2157e705c121SKalle Valo 	IEEE80211_AC_BE,
2158e705c121SKalle Valo 	IEEE80211_AC_BK,
2159e705c121SKalle Valo 	IEEE80211_AC_BK,
2160e705c121SKalle Valo 	IEEE80211_AC_BE,
2161e705c121SKalle Valo 	IEEE80211_AC_VI,
2162e705c121SKalle Valo 	IEEE80211_AC_VI,
2163e705c121SKalle Valo 	IEEE80211_AC_VO,
2164e705c121SKalle Valo 	IEEE80211_AC_VO,
21659794c64fSLiad Kaufman 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2166e705c121SKalle Valo };
2167e705c121SKalle Valo 
2168e705c121SKalle Valo static const u8 tid_to_ucode_ac[] = {
2169e705c121SKalle Valo 	AC_BE,
2170e705c121SKalle Valo 	AC_BK,
2171e705c121SKalle Valo 	AC_BK,
2172e705c121SKalle Valo 	AC_BE,
2173e705c121SKalle Valo 	AC_VI,
2174e705c121SKalle Valo 	AC_VI,
2175e705c121SKalle Valo 	AC_VO,
2176e705c121SKalle Valo 	AC_VO,
2177e705c121SKalle Valo };
2178e705c121SKalle Valo 
2179e705c121SKalle Valo int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2180e705c121SKalle Valo 			     struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2181e705c121SKalle Valo {
2182e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2183e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data;
2184e705c121SKalle Valo 	int txq_id;
2185e705c121SKalle Valo 	int ret;
2186e705c121SKalle Valo 
2187e705c121SKalle Valo 	if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2188e705c121SKalle Valo 		return -EINVAL;
2189e705c121SKalle Valo 
2190e705c121SKalle Valo 	if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2191e705c121SKalle Valo 		IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
2192e705c121SKalle Valo 			mvmsta->tid_data[tid].state);
2193e705c121SKalle Valo 		return -ENXIO;
2194e705c121SKalle Valo 	}
2195e705c121SKalle Valo 
2196e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2197e705c121SKalle Valo 
2198e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2199e705c121SKalle Valo 
2200e705c121SKalle Valo 	/* possible race condition - we entered D0i3 while starting agg */
2201e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
2202e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
2203e705c121SKalle Valo 		IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
2204e705c121SKalle Valo 		return -EIO;
2205e705c121SKalle Valo 	}
2206e705c121SKalle Valo 
22079f9af3d7SLiad Kaufman 	spin_lock(&mvm->queue_info_lock);
2208e705c121SKalle Valo 
2209cf961e16SLiad Kaufman 	/*
2210cf961e16SLiad Kaufman 	 * Note the possible cases:
2211cf961e16SLiad Kaufman 	 *  1. In DQA mode with an enabled TXQ - TXQ needs to become agg'ed
2212cf961e16SLiad Kaufman 	 *  2. Non-DQA mode: the TXQ hasn't yet been enabled, so find a free
2213cf961e16SLiad Kaufman 	 *	one and mark it as reserved
2214cf961e16SLiad Kaufman 	 *  3. In DQA mode, but no traffic yet on this TID: same treatment as in
2215cf961e16SLiad Kaufman 	 *	non-DQA mode, since the TXQ hasn't yet been allocated
2216cf961e16SLiad Kaufman 	 */
2217cf961e16SLiad Kaufman 	txq_id = mvmsta->tid_data[tid].txq_id;
22189f9af3d7SLiad Kaufman 	if (iwl_mvm_is_dqa_supported(mvm) &&
22199f9af3d7SLiad Kaufman 	    unlikely(mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_SHARED)) {
22209f9af3d7SLiad Kaufman 		ret = -ENXIO;
22219f9af3d7SLiad Kaufman 		IWL_DEBUG_TX_QUEUES(mvm,
22229f9af3d7SLiad Kaufman 				    "Can't start tid %d agg on shared queue!\n",
22239f9af3d7SLiad Kaufman 				    tid);
22249f9af3d7SLiad Kaufman 		goto release_locks;
22259f9af3d7SLiad Kaufman 	} else if (!iwl_mvm_is_dqa_supported(mvm) ||
2226cf961e16SLiad Kaufman 	    mvm->queue_info[txq_id].status != IWL_MVM_QUEUE_READY) {
22279794c64fSLiad Kaufman 		txq_id = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
22289794c64fSLiad Kaufman 						 mvm->first_agg_queue,
2229e705c121SKalle Valo 						 mvm->last_agg_queue);
2230e705c121SKalle Valo 		if (txq_id < 0) {
2231e705c121SKalle Valo 			ret = txq_id;
2232e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to allocate agg queue\n");
2233e705c121SKalle Valo 			goto release_locks;
2234e705c121SKalle Valo 		}
2235cf961e16SLiad Kaufman 
2236cf961e16SLiad Kaufman 		/* TXQ hasn't yet been enabled, so mark it only as reserved */
2237cf961e16SLiad Kaufman 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2238cf961e16SLiad Kaufman 	}
22399f9af3d7SLiad Kaufman 
22409f9af3d7SLiad Kaufman 	spin_unlock(&mvm->queue_info_lock);
2241e705c121SKalle Valo 
2242cf961e16SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm,
2243cf961e16SLiad Kaufman 			    "AGG for tid %d will be on queue #%d\n",
2244cf961e16SLiad Kaufman 			    tid, txq_id);
2245cf961e16SLiad Kaufman 
2246e705c121SKalle Valo 	tid_data = &mvmsta->tid_data[tid];
2247e705c121SKalle Valo 	tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2248e705c121SKalle Valo 	tid_data->txq_id = txq_id;
2249e705c121SKalle Valo 	*ssn = tid_data->ssn;
2250e705c121SKalle Valo 
2251e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm,
2252e705c121SKalle Valo 			    "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2253e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2254e705c121SKalle Valo 			    tid_data->next_reclaimed);
2255e705c121SKalle Valo 
2256e705c121SKalle Valo 	if (tid_data->ssn == tid_data->next_reclaimed) {
2257e705c121SKalle Valo 		tid_data->state = IWL_AGG_STARTING;
2258e705c121SKalle Valo 		ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2259e705c121SKalle Valo 	} else {
2260e705c121SKalle Valo 		tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2261e705c121SKalle Valo 	}
2262e705c121SKalle Valo 
2263e705c121SKalle Valo 	ret = 0;
22649f9af3d7SLiad Kaufman 	goto out;
2265e705c121SKalle Valo 
2266e705c121SKalle Valo release_locks:
22679f9af3d7SLiad Kaufman 	spin_unlock(&mvm->queue_info_lock);
22689f9af3d7SLiad Kaufman out:
2269e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2270e705c121SKalle Valo 
2271e705c121SKalle Valo 	return ret;
2272e705c121SKalle Valo }
2273e705c121SKalle Valo 
2274e705c121SKalle Valo int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2275bb81bb68SEmmanuel Grumbach 			    struct ieee80211_sta *sta, u16 tid, u8 buf_size,
2276bb81bb68SEmmanuel Grumbach 			    bool amsdu)
2277e705c121SKalle Valo {
2278e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2279e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2280e705c121SKalle Valo 	unsigned int wdg_timeout =
2281e705c121SKalle Valo 		iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
2282eea76c36SEmmanuel Grumbach 	int queue, ret;
2283cf961e16SLiad Kaufman 	bool alloc_queue = true;
22849f9af3d7SLiad Kaufman 	enum iwl_mvm_queue_status queue_status;
2285e705c121SKalle Valo 	u16 ssn;
2286e705c121SKalle Valo 
2287eea76c36SEmmanuel Grumbach 	struct iwl_trans_txq_scd_cfg cfg = {
2288eea76c36SEmmanuel Grumbach 		.sta_id = mvmsta->sta_id,
2289eea76c36SEmmanuel Grumbach 		.tid = tid,
2290eea76c36SEmmanuel Grumbach 		.frame_limit = buf_size,
2291eea76c36SEmmanuel Grumbach 		.aggregate = true,
2292eea76c36SEmmanuel Grumbach 	};
2293eea76c36SEmmanuel Grumbach 
2294e705c121SKalle Valo 	BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2295e705c121SKalle Valo 		     != IWL_MAX_TID_COUNT);
2296e705c121SKalle Valo 
2297e705c121SKalle Valo 	buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
2298e705c121SKalle Valo 
2299e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2300e705c121SKalle Valo 	ssn = tid_data->ssn;
2301e705c121SKalle Valo 	queue = tid_data->txq_id;
2302e705c121SKalle Valo 	tid_data->state = IWL_AGG_ON;
2303e705c121SKalle Valo 	mvmsta->agg_tids |= BIT(tid);
2304e705c121SKalle Valo 	tid_data->ssn = 0xffff;
2305bb81bb68SEmmanuel Grumbach 	tid_data->amsdu_in_ampdu_allowed = amsdu;
2306e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2307e705c121SKalle Valo 
2308eea76c36SEmmanuel Grumbach 	cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
2309e705c121SKalle Valo 
23109f9af3d7SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
23119f9af3d7SLiad Kaufman 	queue_status = mvm->queue_info[queue].status;
23129f9af3d7SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
23139f9af3d7SLiad Kaufman 
2314cf961e16SLiad Kaufman 	/* In DQA mode, the existing queue might need to be reconfigured */
2315cf961e16SLiad Kaufman 	if (iwl_mvm_is_dqa_supported(mvm)) {
2316cf961e16SLiad Kaufman 		/* Maybe there is no need to even alloc a queue... */
2317cf961e16SLiad Kaufman 		if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2318cf961e16SLiad Kaufman 			alloc_queue = false;
2319cf961e16SLiad Kaufman 
2320cf961e16SLiad Kaufman 		/*
2321cf961e16SLiad Kaufman 		 * Only reconfig the SCD for the queue if the window size has
2322cf961e16SLiad Kaufman 		 * changed from current (become smaller)
2323cf961e16SLiad Kaufman 		 */
2324cf961e16SLiad Kaufman 		if (!alloc_queue && buf_size < mvmsta->max_agg_bufsize) {
2325cf961e16SLiad Kaufman 			/*
2326cf961e16SLiad Kaufman 			 * If reconfiguring an existing queue, it first must be
2327cf961e16SLiad Kaufman 			 * drained
2328cf961e16SLiad Kaufman 			 */
2329cf961e16SLiad Kaufman 			ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
2330cf961e16SLiad Kaufman 							    BIT(queue));
2331cf961e16SLiad Kaufman 			if (ret) {
2332cf961e16SLiad Kaufman 				IWL_ERR(mvm,
2333cf961e16SLiad Kaufman 					"Error draining queue before reconfig\n");
2334cf961e16SLiad Kaufman 				return ret;
2335cf961e16SLiad Kaufman 			}
2336cf961e16SLiad Kaufman 
2337cf961e16SLiad Kaufman 			ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2338cf961e16SLiad Kaufman 						   mvmsta->sta_id, tid,
2339cf961e16SLiad Kaufman 						   buf_size, ssn);
2340cf961e16SLiad Kaufman 			if (ret) {
2341cf961e16SLiad Kaufman 				IWL_ERR(mvm,
2342cf961e16SLiad Kaufman 					"Error reconfiguring TXQ #%d\n", queue);
2343cf961e16SLiad Kaufman 				return ret;
2344cf961e16SLiad Kaufman 			}
2345cf961e16SLiad Kaufman 		}
2346cf961e16SLiad Kaufman 	}
2347cf961e16SLiad Kaufman 
2348cf961e16SLiad Kaufman 	if (alloc_queue)
2349cf961e16SLiad Kaufman 		iwl_mvm_enable_txq(mvm, queue,
2350cf961e16SLiad Kaufman 				   vif->hw_queue[tid_to_mac80211_ac[tid]], ssn,
2351cf961e16SLiad Kaufman 				   &cfg, wdg_timeout);
2352e705c121SKalle Valo 
23539f9af3d7SLiad Kaufman 	/* Send ADD_STA command to enable aggs only if the queue isn't shared */
23549f9af3d7SLiad Kaufman 	if (queue_status != IWL_MVM_QUEUE_SHARED) {
2355e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2356e705c121SKalle Valo 		if (ret)
2357e705c121SKalle Valo 			return -EIO;
23589f9af3d7SLiad Kaufman 	}
2359e705c121SKalle Valo 
2360e705c121SKalle Valo 	/* No need to mark as reserved */
2361e705c121SKalle Valo 	spin_lock_bh(&mvm->queue_info_lock);
2362cf961e16SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
2363e705c121SKalle Valo 	spin_unlock_bh(&mvm->queue_info_lock);
2364e705c121SKalle Valo 
2365e705c121SKalle Valo 	/*
2366e705c121SKalle Valo 	 * Even though in theory the peer could have different
2367e705c121SKalle Valo 	 * aggregation reorder buffer sizes for different sessions,
2368e705c121SKalle Valo 	 * our ucode doesn't allow for that and has a global limit
2369e705c121SKalle Valo 	 * for each station. Therefore, use the minimum of all the
2370e705c121SKalle Valo 	 * aggregation sessions and our default value.
2371e705c121SKalle Valo 	 */
2372e705c121SKalle Valo 	mvmsta->max_agg_bufsize =
2373e705c121SKalle Valo 		min(mvmsta->max_agg_bufsize, buf_size);
2374e705c121SKalle Valo 	mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2375e705c121SKalle Valo 
2376e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2377e705c121SKalle Valo 		     sta->addr, tid);
2378e705c121SKalle Valo 
2379e705c121SKalle Valo 	return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
2380e705c121SKalle Valo }
2381e705c121SKalle Valo 
2382e705c121SKalle Valo int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2383e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid)
2384e705c121SKalle Valo {
2385e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2386e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2387e705c121SKalle Valo 	u16 txq_id;
2388e705c121SKalle Valo 	int err;
2389e705c121SKalle Valo 
2390e705c121SKalle Valo 	/*
2391e705c121SKalle Valo 	 * If mac80211 is cleaning its state, then say that we finished since
2392e705c121SKalle Valo 	 * our state has been cleared anyway.
2393e705c121SKalle Valo 	 */
2394e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2395e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2396e705c121SKalle Valo 		return 0;
2397e705c121SKalle Valo 	}
2398e705c121SKalle Valo 
2399e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2400e705c121SKalle Valo 
2401e705c121SKalle Valo 	txq_id = tid_data->txq_id;
2402e705c121SKalle Valo 
2403e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
2404e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
2405e705c121SKalle Valo 
2406e705c121SKalle Valo 	mvmsta->agg_tids &= ~BIT(tid);
2407e705c121SKalle Valo 
2408e705c121SKalle Valo 	spin_lock_bh(&mvm->queue_info_lock);
2409cf961e16SLiad Kaufman 	/*
2410cf961e16SLiad Kaufman 	 * The TXQ is marked as reserved only if no traffic came through yet
2411cf961e16SLiad Kaufman 	 * This means no traffic has been sent on this TID (agg'd or not), so
2412cf961e16SLiad Kaufman 	 * we no longer have use for the queue. Since it hasn't even been
2413cf961e16SLiad Kaufman 	 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2414cf961e16SLiad Kaufman 	 * free.
2415cf961e16SLiad Kaufman 	 */
2416cf961e16SLiad Kaufman 	if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2417cf961e16SLiad Kaufman 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
24189f9af3d7SLiad Kaufman 
2419e705c121SKalle Valo 	spin_unlock_bh(&mvm->queue_info_lock);
2420e705c121SKalle Valo 
2421e705c121SKalle Valo 	switch (tid_data->state) {
2422e705c121SKalle Valo 	case IWL_AGG_ON:
2423e705c121SKalle Valo 		tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2424e705c121SKalle Valo 
2425e705c121SKalle Valo 		IWL_DEBUG_TX_QUEUES(mvm,
2426e705c121SKalle Valo 				    "ssn = %d, next_recl = %d\n",
2427e705c121SKalle Valo 				    tid_data->ssn, tid_data->next_reclaimed);
2428e705c121SKalle Valo 
2429e705c121SKalle Valo 		/* There are still packets for this RA / TID in the HW */
2430e705c121SKalle Valo 		if (tid_data->ssn != tid_data->next_reclaimed) {
2431e705c121SKalle Valo 			tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
2432e705c121SKalle Valo 			err = 0;
2433e705c121SKalle Valo 			break;
2434e705c121SKalle Valo 		}
2435e705c121SKalle Valo 
2436e705c121SKalle Valo 		tid_data->ssn = 0xffff;
2437e705c121SKalle Valo 		tid_data->state = IWL_AGG_OFF;
2438e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
2439e705c121SKalle Valo 
2440e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2441e705c121SKalle Valo 
2442e705c121SKalle Valo 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2443e705c121SKalle Valo 
2444cf961e16SLiad Kaufman 		if (!iwl_mvm_is_dqa_supported(mvm)) {
2445cf961e16SLiad Kaufman 			int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2446cf961e16SLiad Kaufman 
2447cf961e16SLiad Kaufman 			iwl_mvm_disable_txq(mvm, txq_id, mac_queue, tid, 0);
2448cf961e16SLiad Kaufman 		}
2449e705c121SKalle Valo 		return 0;
2450e705c121SKalle Valo 	case IWL_AGG_STARTING:
2451e705c121SKalle Valo 	case IWL_EMPTYING_HW_QUEUE_ADDBA:
2452e705c121SKalle Valo 		/*
2453e705c121SKalle Valo 		 * The agg session has been stopped before it was set up. This
2454e705c121SKalle Valo 		 * can happen when the AddBA timer times out for example.
2455e705c121SKalle Valo 		 */
2456e705c121SKalle Valo 
2457e705c121SKalle Valo 		/* No barriers since we are under mutex */
2458e705c121SKalle Valo 		lockdep_assert_held(&mvm->mutex);
2459e705c121SKalle Valo 
2460e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2461e705c121SKalle Valo 		tid_data->state = IWL_AGG_OFF;
2462e705c121SKalle Valo 		err = 0;
2463e705c121SKalle Valo 		break;
2464e705c121SKalle Valo 	default:
2465e705c121SKalle Valo 		IWL_ERR(mvm,
2466e705c121SKalle Valo 			"Stopping AGG while state not ON or starting for %d on %d (%d)\n",
2467e705c121SKalle Valo 			mvmsta->sta_id, tid, tid_data->state);
2468e705c121SKalle Valo 		IWL_ERR(mvm,
2469e705c121SKalle Valo 			"\ttid_data->txq_id = %d\n", tid_data->txq_id);
2470e705c121SKalle Valo 		err = -EINVAL;
2471e705c121SKalle Valo 	}
2472e705c121SKalle Valo 
2473e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2474e705c121SKalle Valo 
2475e705c121SKalle Valo 	return err;
2476e705c121SKalle Valo }
2477e705c121SKalle Valo 
2478e705c121SKalle Valo int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2479e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid)
2480e705c121SKalle Valo {
2481e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2482e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2483e705c121SKalle Valo 	u16 txq_id;
2484e705c121SKalle Valo 	enum iwl_mvm_agg_state old_state;
2485e705c121SKalle Valo 
2486e705c121SKalle Valo 	/*
2487e705c121SKalle Valo 	 * First set the agg state to OFF to avoid calling
2488e705c121SKalle Valo 	 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
2489e705c121SKalle Valo 	 */
2490e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2491e705c121SKalle Valo 	txq_id = tid_data->txq_id;
2492e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
2493e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
2494e705c121SKalle Valo 	old_state = tid_data->state;
2495e705c121SKalle Valo 	tid_data->state = IWL_AGG_OFF;
2496e705c121SKalle Valo 	mvmsta->agg_tids &= ~BIT(tid);
2497e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2498e705c121SKalle Valo 
2499e705c121SKalle Valo 	spin_lock_bh(&mvm->queue_info_lock);
2500cf961e16SLiad Kaufman 	/*
2501cf961e16SLiad Kaufman 	 * The TXQ is marked as reserved only if no traffic came through yet
2502cf961e16SLiad Kaufman 	 * This means no traffic has been sent on this TID (agg'd or not), so
2503cf961e16SLiad Kaufman 	 * we no longer have use for the queue. Since it hasn't even been
2504cf961e16SLiad Kaufman 	 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2505cf961e16SLiad Kaufman 	 * free.
2506cf961e16SLiad Kaufman 	 */
2507cf961e16SLiad Kaufman 	if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2508cf961e16SLiad Kaufman 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
2509e705c121SKalle Valo 	spin_unlock_bh(&mvm->queue_info_lock);
2510e705c121SKalle Valo 
2511e705c121SKalle Valo 	if (old_state >= IWL_AGG_ON) {
2512e705c121SKalle Valo 		iwl_mvm_drain_sta(mvm, mvmsta, true);
2513e705c121SKalle Valo 		if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
2514e705c121SKalle Valo 			IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
2515e705c121SKalle Valo 		iwl_trans_wait_tx_queue_empty(mvm->trans,
2516e705c121SKalle Valo 					      mvmsta->tfd_queue_msk);
2517e705c121SKalle Valo 		iwl_mvm_drain_sta(mvm, mvmsta, false);
2518e705c121SKalle Valo 
2519e705c121SKalle Valo 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2520e705c121SKalle Valo 
2521cf961e16SLiad Kaufman 		if (!iwl_mvm_is_dqa_supported(mvm)) {
2522cf961e16SLiad Kaufman 			int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2523cf961e16SLiad Kaufman 
2524cf961e16SLiad Kaufman 			iwl_mvm_disable_txq(mvm, tid_data->txq_id, mac_queue,
2525cf961e16SLiad Kaufman 					    tid, 0);
2526cf961e16SLiad Kaufman 		}
2527e705c121SKalle Valo 	}
2528e705c121SKalle Valo 
2529e705c121SKalle Valo 	return 0;
2530e705c121SKalle Valo }
2531e705c121SKalle Valo 
2532e705c121SKalle Valo static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
2533e705c121SKalle Valo {
2534e705c121SKalle Valo 	int i, max = -1, max_offs = -1;
2535e705c121SKalle Valo 
2536e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2537e705c121SKalle Valo 
2538e705c121SKalle Valo 	/* Pick the unused key offset with the highest 'deleted'
2539e705c121SKalle Valo 	 * counter. Every time a key is deleted, all the counters
2540e705c121SKalle Valo 	 * are incremented and the one that was just deleted is
2541e705c121SKalle Valo 	 * reset to zero. Thus, the highest counter is the one
2542e705c121SKalle Valo 	 * that was deleted longest ago. Pick that one.
2543e705c121SKalle Valo 	 */
2544e705c121SKalle Valo 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2545e705c121SKalle Valo 		if (test_bit(i, mvm->fw_key_table))
2546e705c121SKalle Valo 			continue;
2547e705c121SKalle Valo 		if (mvm->fw_key_deleted[i] > max) {
2548e705c121SKalle Valo 			max = mvm->fw_key_deleted[i];
2549e705c121SKalle Valo 			max_offs = i;
2550e705c121SKalle Valo 		}
2551e705c121SKalle Valo 	}
2552e705c121SKalle Valo 
2553e705c121SKalle Valo 	if (max_offs < 0)
2554e705c121SKalle Valo 		return STA_KEY_IDX_INVALID;
2555e705c121SKalle Valo 
2556e705c121SKalle Valo 	return max_offs;
2557e705c121SKalle Valo }
2558e705c121SKalle Valo 
25595f7a1847SJohannes Berg static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
25604615fd15SEmmanuel Grumbach 					       struct ieee80211_vif *vif,
2561e705c121SKalle Valo 					       struct ieee80211_sta *sta)
2562e705c121SKalle Valo {
2563e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2564e705c121SKalle Valo 
25655f7a1847SJohannes Berg 	if (sta)
25665f7a1847SJohannes Berg 		return iwl_mvm_sta_from_mac80211(sta);
2567e705c121SKalle Valo 
2568e705c121SKalle Valo 	/*
2569e705c121SKalle Valo 	 * The device expects GTKs for station interfaces to be
2570e705c121SKalle Valo 	 * installed as GTKs for the AP station. If we have no
2571e705c121SKalle Valo 	 * station ID, then use AP's station ID.
2572e705c121SKalle Valo 	 */
2573e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
25744615fd15SEmmanuel Grumbach 	    mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
25754615fd15SEmmanuel Grumbach 		u8 sta_id = mvmvif->ap_sta_id;
25764615fd15SEmmanuel Grumbach 
25777d6a1ab6SEmmanuel Grumbach 		sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
25787d6a1ab6SEmmanuel Grumbach 					    lockdep_is_held(&mvm->mutex));
25797d6a1ab6SEmmanuel Grumbach 
25804615fd15SEmmanuel Grumbach 		/*
25814615fd15SEmmanuel Grumbach 		 * It is possible that the 'sta' parameter is NULL,
25824615fd15SEmmanuel Grumbach 		 * for example when a GTK is removed - the sta_id will then
25834615fd15SEmmanuel Grumbach 		 * be the AP ID, and no station was passed by mac80211.
25844615fd15SEmmanuel Grumbach 		 */
25857d6a1ab6SEmmanuel Grumbach 		if (IS_ERR_OR_NULL(sta))
25867d6a1ab6SEmmanuel Grumbach 			return NULL;
25877d6a1ab6SEmmanuel Grumbach 
25887d6a1ab6SEmmanuel Grumbach 		return iwl_mvm_sta_from_mac80211(sta);
25894615fd15SEmmanuel Grumbach 	}
2590e705c121SKalle Valo 
25915f7a1847SJohannes Berg 	return NULL;
2592e705c121SKalle Valo }
2593e705c121SKalle Valo 
2594e705c121SKalle Valo static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
2595e705c121SKalle Valo 				struct iwl_mvm_sta *mvm_sta,
2596e705c121SKalle Valo 				struct ieee80211_key_conf *keyconf, bool mcast,
25974615fd15SEmmanuel Grumbach 				u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
25984615fd15SEmmanuel Grumbach 				u8 key_offset)
2599e705c121SKalle Valo {
2600e705c121SKalle Valo 	struct iwl_mvm_add_sta_key_cmd cmd = {};
2601e705c121SKalle Valo 	__le16 key_flags;
2602e705c121SKalle Valo 	int ret;
2603e705c121SKalle Valo 	u32 status;
2604e705c121SKalle Valo 	u16 keyidx;
2605e705c121SKalle Valo 	int i;
2606e705c121SKalle Valo 	u8 sta_id = mvm_sta->sta_id;
2607e705c121SKalle Valo 
2608e705c121SKalle Valo 	keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2609e705c121SKalle Valo 		 STA_KEY_FLG_KEYID_MSK;
2610e705c121SKalle Valo 	key_flags = cpu_to_le16(keyidx);
2611e705c121SKalle Valo 	key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
2612e705c121SKalle Valo 
2613e705c121SKalle Valo 	switch (keyconf->cipher) {
2614e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
2615e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
2616e705c121SKalle Valo 		cmd.tkip_rx_tsc_byte2 = tkip_iv32;
2617e705c121SKalle Valo 		for (i = 0; i < 5; i++)
2618e705c121SKalle Valo 			cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
2619e705c121SKalle Valo 		memcpy(cmd.key, keyconf->key, keyconf->keylen);
2620e705c121SKalle Valo 		break;
2621e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
2622e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
2623e705c121SKalle Valo 		memcpy(cmd.key, keyconf->key, keyconf->keylen);
2624e705c121SKalle Valo 		break;
2625e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
2626e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
2627e705c121SKalle Valo 		/* fall through */
2628e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
2629e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
2630e705c121SKalle Valo 		memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
2631e705c121SKalle Valo 		break;
26322a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
26332a53d166SAyala Beker 		key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
26342a53d166SAyala Beker 		/* fall through */
26352a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
26362a53d166SAyala Beker 		key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
26372a53d166SAyala Beker 		memcpy(cmd.key, keyconf->key, keyconf->keylen);
26382a53d166SAyala Beker 		break;
2639e705c121SKalle Valo 	default:
2640e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
2641e705c121SKalle Valo 		memcpy(cmd.key, keyconf->key, keyconf->keylen);
2642e705c121SKalle Valo 	}
2643e705c121SKalle Valo 
2644e705c121SKalle Valo 	if (mcast)
2645e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2646e705c121SKalle Valo 
26474615fd15SEmmanuel Grumbach 	cmd.key_offset = key_offset;
2648e705c121SKalle Valo 	cmd.key_flags = key_flags;
2649e705c121SKalle Valo 	cmd.sta_id = sta_id;
2650e705c121SKalle Valo 
2651e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
2652e705c121SKalle Valo 	if (cmd_flags & CMD_ASYNC)
2653e705c121SKalle Valo 		ret =  iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
2654e705c121SKalle Valo 					    sizeof(cmd), &cmd);
2655e705c121SKalle Valo 	else
2656e705c121SKalle Valo 		ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
2657e705c121SKalle Valo 						  &cmd, &status);
2658e705c121SKalle Valo 
2659e705c121SKalle Valo 	switch (status) {
2660e705c121SKalle Valo 	case ADD_STA_SUCCESS:
2661e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
2662e705c121SKalle Valo 		break;
2663e705c121SKalle Valo 	default:
2664e705c121SKalle Valo 		ret = -EIO;
2665e705c121SKalle Valo 		IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
2666e705c121SKalle Valo 		break;
2667e705c121SKalle Valo 	}
2668e705c121SKalle Valo 
2669e705c121SKalle Valo 	return ret;
2670e705c121SKalle Valo }
2671e705c121SKalle Valo 
2672e705c121SKalle Valo static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
2673e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
2674e705c121SKalle Valo 				 u8 sta_id, bool remove_key)
2675e705c121SKalle Valo {
2676e705c121SKalle Valo 	struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
2677e705c121SKalle Valo 
2678e705c121SKalle Valo 	/* verify the key details match the required command's expectations */
26798e160ab8SAyala Beker 	if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
26808e160ab8SAyala Beker 		    (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
26818e160ab8SAyala Beker 		    (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
26828e160ab8SAyala Beker 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
26838e160ab8SAyala Beker 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
26848e160ab8SAyala Beker 		return -EINVAL;
26858e160ab8SAyala Beker 
26868e160ab8SAyala Beker 	if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
26878e160ab8SAyala Beker 		    keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
2688e705c121SKalle Valo 		return -EINVAL;
2689e705c121SKalle Valo 
2690e705c121SKalle Valo 	igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
2691e705c121SKalle Valo 	igtk_cmd.sta_id = cpu_to_le32(sta_id);
2692e705c121SKalle Valo 
2693e705c121SKalle Valo 	if (remove_key) {
2694e705c121SKalle Valo 		igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
2695e705c121SKalle Valo 	} else {
2696e705c121SKalle Valo 		struct ieee80211_key_seq seq;
2697e705c121SKalle Valo 		const u8 *pn;
2698e705c121SKalle Valo 
2699aa950524SAyala Beker 		switch (keyconf->cipher) {
2700aa950524SAyala Beker 		case WLAN_CIPHER_SUITE_AES_CMAC:
2701aa950524SAyala Beker 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
2702aa950524SAyala Beker 			break;
27038e160ab8SAyala Beker 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
27048e160ab8SAyala Beker 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
27058e160ab8SAyala Beker 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
27068e160ab8SAyala Beker 			break;
2707aa950524SAyala Beker 		default:
2708aa950524SAyala Beker 			return -EINVAL;
2709aa950524SAyala Beker 		}
2710aa950524SAyala Beker 
27118e160ab8SAyala Beker 		memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
27128e160ab8SAyala Beker 		if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
27138e160ab8SAyala Beker 			igtk_cmd.ctrl_flags |=
27148e160ab8SAyala Beker 				cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
2715e705c121SKalle Valo 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2716e705c121SKalle Valo 		pn = seq.aes_cmac.pn;
2717e705c121SKalle Valo 		igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
2718e705c121SKalle Valo 						       ((u64) pn[4] << 8) |
2719e705c121SKalle Valo 						       ((u64) pn[3] << 16) |
2720e705c121SKalle Valo 						       ((u64) pn[2] << 24) |
2721e705c121SKalle Valo 						       ((u64) pn[1] << 32) |
2722e705c121SKalle Valo 						       ((u64) pn[0] << 40));
2723e705c121SKalle Valo 	}
2724e705c121SKalle Valo 
2725e705c121SKalle Valo 	IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
2726e705c121SKalle Valo 		       remove_key ? "removing" : "installing",
2727e705c121SKalle Valo 		       igtk_cmd.sta_id);
2728e705c121SKalle Valo 
27298e160ab8SAyala Beker 	if (!iwl_mvm_has_new_rx_api(mvm)) {
27308e160ab8SAyala Beker 		struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
27318e160ab8SAyala Beker 			.ctrl_flags = igtk_cmd.ctrl_flags,
27328e160ab8SAyala Beker 			.key_id = igtk_cmd.key_id,
27338e160ab8SAyala Beker 			.sta_id = igtk_cmd.sta_id,
27348e160ab8SAyala Beker 			.receive_seq_cnt = igtk_cmd.receive_seq_cnt
27358e160ab8SAyala Beker 		};
27368e160ab8SAyala Beker 
27378e160ab8SAyala Beker 		memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
27388e160ab8SAyala Beker 		       ARRAY_SIZE(igtk_cmd_v1.igtk));
27398e160ab8SAyala Beker 		return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
27408e160ab8SAyala Beker 					    sizeof(igtk_cmd_v1), &igtk_cmd_v1);
27418e160ab8SAyala Beker 	}
2742e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
2743e705c121SKalle Valo 				    sizeof(igtk_cmd), &igtk_cmd);
2744e705c121SKalle Valo }
2745e705c121SKalle Valo 
2746e705c121SKalle Valo 
2747e705c121SKalle Valo static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
2748e705c121SKalle Valo 				       struct ieee80211_vif *vif,
2749e705c121SKalle Valo 				       struct ieee80211_sta *sta)
2750e705c121SKalle Valo {
2751e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2752e705c121SKalle Valo 
2753e705c121SKalle Valo 	if (sta)
2754e705c121SKalle Valo 		return sta->addr;
2755e705c121SKalle Valo 
2756e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
2757e705c121SKalle Valo 	    mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
2758e705c121SKalle Valo 		u8 sta_id = mvmvif->ap_sta_id;
2759e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
2760e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
2761e705c121SKalle Valo 		return sta->addr;
2762e705c121SKalle Valo 	}
2763e705c121SKalle Valo 
2764e705c121SKalle Valo 
2765e705c121SKalle Valo 	return NULL;
2766e705c121SKalle Valo }
2767e705c121SKalle Valo 
2768e705c121SKalle Valo static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
2769e705c121SKalle Valo 				 struct ieee80211_vif *vif,
2770e705c121SKalle Valo 				 struct ieee80211_sta *sta,
2771e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
27724615fd15SEmmanuel Grumbach 				 u8 key_offset,
2773e705c121SKalle Valo 				 bool mcast)
2774e705c121SKalle Valo {
2775e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2776e705c121SKalle Valo 	int ret;
2777e705c121SKalle Valo 	const u8 *addr;
2778e705c121SKalle Valo 	struct ieee80211_key_seq seq;
2779e705c121SKalle Valo 	u16 p1k[5];
2780e705c121SKalle Valo 
2781e705c121SKalle Valo 	switch (keyconf->cipher) {
2782e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
2783e705c121SKalle Valo 		addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
2784e705c121SKalle Valo 		/* get phase 1 key from mac80211 */
2785e705c121SKalle Valo 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2786e705c121SKalle Valo 		ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
2787e705c121SKalle Valo 		ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
27884615fd15SEmmanuel Grumbach 					   seq.tkip.iv32, p1k, 0, key_offset);
2789e705c121SKalle Valo 		break;
2790e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
2791e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
2792e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
27932a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
27942a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
2795e705c121SKalle Valo 		ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
27964615fd15SEmmanuel Grumbach 					   0, NULL, 0, key_offset);
2797e705c121SKalle Valo 		break;
2798e705c121SKalle Valo 	default:
2799e705c121SKalle Valo 		ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
28004615fd15SEmmanuel Grumbach 					   0, NULL, 0, key_offset);
2801e705c121SKalle Valo 	}
2802e705c121SKalle Valo 
2803e705c121SKalle Valo 	return ret;
2804e705c121SKalle Valo }
2805e705c121SKalle Valo 
2806e705c121SKalle Valo static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
2807e705c121SKalle Valo 				    struct ieee80211_key_conf *keyconf,
2808e705c121SKalle Valo 				    bool mcast)
2809e705c121SKalle Valo {
2810e705c121SKalle Valo 	struct iwl_mvm_add_sta_key_cmd cmd = {};
2811e705c121SKalle Valo 	__le16 key_flags;
2812e705c121SKalle Valo 	int ret;
2813e705c121SKalle Valo 	u32 status;
2814e705c121SKalle Valo 
2815e705c121SKalle Valo 	key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2816e705c121SKalle Valo 				 STA_KEY_FLG_KEYID_MSK);
2817e705c121SKalle Valo 	key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2818e705c121SKalle Valo 	key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2819e705c121SKalle Valo 
2820e705c121SKalle Valo 	if (mcast)
2821e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2822e705c121SKalle Valo 
2823e705c121SKalle Valo 	cmd.key_flags = key_flags;
2824e705c121SKalle Valo 	cmd.key_offset = keyconf->hw_key_idx;
2825e705c121SKalle Valo 	cmd.sta_id = sta_id;
2826e705c121SKalle Valo 
2827e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
2828e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
2829e705c121SKalle Valo 					  &cmd, &status);
2830e705c121SKalle Valo 
2831e705c121SKalle Valo 	switch (status) {
2832e705c121SKalle Valo 	case ADD_STA_SUCCESS:
2833e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2834e705c121SKalle Valo 		break;
2835e705c121SKalle Valo 	default:
2836e705c121SKalle Valo 		ret = -EIO;
2837e705c121SKalle Valo 		IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2838e705c121SKalle Valo 		break;
2839e705c121SKalle Valo 	}
2840e705c121SKalle Valo 
2841e705c121SKalle Valo 	return ret;
2842e705c121SKalle Valo }
2843e705c121SKalle Valo 
2844e705c121SKalle Valo int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
2845e705c121SKalle Valo 			struct ieee80211_vif *vif,
2846e705c121SKalle Valo 			struct ieee80211_sta *sta,
2847e705c121SKalle Valo 			struct ieee80211_key_conf *keyconf,
28484615fd15SEmmanuel Grumbach 			u8 key_offset)
2849e705c121SKalle Valo {
2850e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
28515f7a1847SJohannes Berg 	struct iwl_mvm_sta *mvm_sta;
2852e705c121SKalle Valo 	u8 sta_id;
2853e705c121SKalle Valo 	int ret;
2854e705c121SKalle Valo 	static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
2855e705c121SKalle Valo 
2856e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2857e705c121SKalle Valo 
2858e705c121SKalle Valo 	/* Get the station id from the mvm local station table */
28595f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
28605f7a1847SJohannes Berg 	if (!mvm_sta) {
28615f7a1847SJohannes Berg 		IWL_ERR(mvm, "Failed to find station\n");
2862e705c121SKalle Valo 		return -EINVAL;
2863e705c121SKalle Valo 	}
28645f7a1847SJohannes Berg 	sta_id = mvm_sta->sta_id;
2865e705c121SKalle Valo 
28668e160ab8SAyala Beker 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
28678e160ab8SAyala Beker 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
28688e160ab8SAyala Beker 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
2869e705c121SKalle Valo 		ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
2870e705c121SKalle Valo 		goto end;
2871e705c121SKalle Valo 	}
2872e705c121SKalle Valo 
2873e705c121SKalle Valo 	/*
2874e705c121SKalle Valo 	 * It is possible that the 'sta' parameter is NULL, and thus
2875e705c121SKalle Valo 	 * there is a need to retrieve  the sta from the local station table.
2876e705c121SKalle Valo 	 */
2877e705c121SKalle Valo 	if (!sta) {
2878e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
2879e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
2880e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta)) {
2881e705c121SKalle Valo 			IWL_ERR(mvm, "Invalid station id\n");
2882e705c121SKalle Valo 			return -EINVAL;
2883e705c121SKalle Valo 		}
2884e705c121SKalle Valo 	}
2885e705c121SKalle Valo 
2886e705c121SKalle Valo 	if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
2887e705c121SKalle Valo 		return -EINVAL;
2888e705c121SKalle Valo 
28894615fd15SEmmanuel Grumbach 	/* If the key_offset is not pre-assigned, we need to find a
28904615fd15SEmmanuel Grumbach 	 * new offset to use.  In normal cases, the offset is not
28914615fd15SEmmanuel Grumbach 	 * pre-assigned, but during HW_RESTART we want to reuse the
28924615fd15SEmmanuel Grumbach 	 * same indices, so we pass them when this function is called.
28934615fd15SEmmanuel Grumbach 	 *
28944615fd15SEmmanuel Grumbach 	 * In D3 entry, we need to hardcoded the indices (because the
28954615fd15SEmmanuel Grumbach 	 * firmware hardcodes the PTK offset to 0).  In this case, we
28964615fd15SEmmanuel Grumbach 	 * need to make sure we don't overwrite the hw_key_idx in the
28974615fd15SEmmanuel Grumbach 	 * keyconf structure, because otherwise we cannot configure
28984615fd15SEmmanuel Grumbach 	 * the original ones back when resuming.
2899e705c121SKalle Valo 	 */
29004615fd15SEmmanuel Grumbach 	if (key_offset == STA_KEY_IDX_INVALID) {
29014615fd15SEmmanuel Grumbach 		key_offset  = iwl_mvm_set_fw_key_idx(mvm);
29024615fd15SEmmanuel Grumbach 		if (key_offset == STA_KEY_IDX_INVALID)
2903e705c121SKalle Valo 			return -ENOSPC;
29044615fd15SEmmanuel Grumbach 		keyconf->hw_key_idx = key_offset;
2905e705c121SKalle Valo 	}
2906e705c121SKalle Valo 
29074615fd15SEmmanuel Grumbach 	ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
29089c3deeb5SLuca Coelho 	if (ret)
2909e705c121SKalle Valo 		goto end;
2910e705c121SKalle Valo 
2911e705c121SKalle Valo 	/*
2912e705c121SKalle Valo 	 * For WEP, the same key is used for multicast and unicast. Upload it
2913e705c121SKalle Valo 	 * again, using the same key offset, and now pointing the other one
2914e705c121SKalle Valo 	 * to the same key slot (offset).
2915e705c121SKalle Valo 	 * If this fails, remove the original as well.
2916e705c121SKalle Valo 	 */
2917e705c121SKalle Valo 	if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2918e705c121SKalle Valo 	    keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
29194615fd15SEmmanuel Grumbach 		ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
29204615fd15SEmmanuel Grumbach 					    key_offset, !mcast);
2921e705c121SKalle Valo 		if (ret) {
2922e705c121SKalle Valo 			__iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
29239c3deeb5SLuca Coelho 			goto end;
2924e705c121SKalle Valo 		}
2925e705c121SKalle Valo 	}
2926e705c121SKalle Valo 
29279c3deeb5SLuca Coelho 	__set_bit(key_offset, mvm->fw_key_table);
29289c3deeb5SLuca Coelho 
2929e705c121SKalle Valo end:
2930e705c121SKalle Valo 	IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
2931e705c121SKalle Valo 		      keyconf->cipher, keyconf->keylen, keyconf->keyidx,
2932e705c121SKalle Valo 		      sta ? sta->addr : zero_addr, ret);
2933e705c121SKalle Valo 	return ret;
2934e705c121SKalle Valo }
2935e705c121SKalle Valo 
2936e705c121SKalle Valo int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
2937e705c121SKalle Valo 			   struct ieee80211_vif *vif,
2938e705c121SKalle Valo 			   struct ieee80211_sta *sta,
2939e705c121SKalle Valo 			   struct ieee80211_key_conf *keyconf)
2940e705c121SKalle Valo {
2941e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
29425f7a1847SJohannes Berg 	struct iwl_mvm_sta *mvm_sta;
29435f7a1847SJohannes Berg 	u8 sta_id = IWL_MVM_STATION_COUNT;
2944e705c121SKalle Valo 	int ret, i;
2945e705c121SKalle Valo 
2946e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2947e705c121SKalle Valo 
29485f7a1847SJohannes Berg 	/* Get the station from the mvm local station table */
29495f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
2950e705c121SKalle Valo 
2951e705c121SKalle Valo 	IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
2952e705c121SKalle Valo 		      keyconf->keyidx, sta_id);
2953e705c121SKalle Valo 
29548e160ab8SAyala Beker 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
29558e160ab8SAyala Beker 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
29568e160ab8SAyala Beker 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
2957e705c121SKalle Valo 		return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
2958e705c121SKalle Valo 
2959e705c121SKalle Valo 	if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
2960e705c121SKalle Valo 		IWL_ERR(mvm, "offset %d not used in fw key table.\n",
2961e705c121SKalle Valo 			keyconf->hw_key_idx);
2962e705c121SKalle Valo 		return -ENOENT;
2963e705c121SKalle Valo 	}
2964e705c121SKalle Valo 
2965e705c121SKalle Valo 	/* track which key was deleted last */
2966e705c121SKalle Valo 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2967e705c121SKalle Valo 		if (mvm->fw_key_deleted[i] < U8_MAX)
2968e705c121SKalle Valo 			mvm->fw_key_deleted[i]++;
2969e705c121SKalle Valo 	}
2970e705c121SKalle Valo 	mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
2971e705c121SKalle Valo 
29725f7a1847SJohannes Berg 	if (!mvm_sta) {
2973e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
2974e705c121SKalle Valo 		return 0;
2975e705c121SKalle Valo 	}
2976e705c121SKalle Valo 
29775f7a1847SJohannes Berg 	sta_id = mvm_sta->sta_id;
29785f7a1847SJohannes Berg 
2979e705c121SKalle Valo 	ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
2980e705c121SKalle Valo 	if (ret)
2981e705c121SKalle Valo 		return ret;
2982e705c121SKalle Valo 
2983e705c121SKalle Valo 	/* delete WEP key twice to get rid of (now useless) offset */
2984e705c121SKalle Valo 	if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2985e705c121SKalle Valo 	    keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
2986e705c121SKalle Valo 		ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
2987e705c121SKalle Valo 
2988e705c121SKalle Valo 	return ret;
2989e705c121SKalle Valo }
2990e705c121SKalle Valo 
2991e705c121SKalle Valo void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
2992e705c121SKalle Valo 			     struct ieee80211_vif *vif,
2993e705c121SKalle Valo 			     struct ieee80211_key_conf *keyconf,
2994e705c121SKalle Valo 			     struct ieee80211_sta *sta, u32 iv32,
2995e705c121SKalle Valo 			     u16 *phase1key)
2996e705c121SKalle Valo {
2997e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta;
2998e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
2999e705c121SKalle Valo 
3000e705c121SKalle Valo 	rcu_read_lock();
3001e705c121SKalle Valo 
30025f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
30035f7a1847SJohannes Berg 	if (WARN_ON_ONCE(!mvm_sta))
300412f17211SEmmanuel Grumbach 		goto unlock;
3005e705c121SKalle Valo 	iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
30064615fd15SEmmanuel Grumbach 			     iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
300712f17211SEmmanuel Grumbach 
300812f17211SEmmanuel Grumbach  unlock:
3009e705c121SKalle Valo 	rcu_read_unlock();
3010e705c121SKalle Valo }
3011e705c121SKalle Valo 
3012e705c121SKalle Valo void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3013e705c121SKalle Valo 				struct ieee80211_sta *sta)
3014e705c121SKalle Valo {
3015e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3016e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3017e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3018e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3019e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_PS),
3020e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3021e705c121SKalle Valo 	};
3022e705c121SKalle Valo 	int ret;
3023e705c121SKalle Valo 
3024854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3025854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3026e705c121SKalle Valo 	if (ret)
3027e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3028e705c121SKalle Valo }
3029e705c121SKalle Valo 
3030e705c121SKalle Valo void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3031e705c121SKalle Valo 				       struct ieee80211_sta *sta,
3032e705c121SKalle Valo 				       enum ieee80211_frame_release_type reason,
3033e705c121SKalle Valo 				       u16 cnt, u16 tids, bool more_data,
3034e705c121SKalle Valo 				       bool agg)
3035e705c121SKalle Valo {
3036e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3037e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3038e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3039e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3040e705c121SKalle Valo 		.modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3041e705c121SKalle Valo 		.sleep_tx_count = cpu_to_le16(cnt),
3042e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3043e705c121SKalle Valo 	};
3044e705c121SKalle Valo 	int tid, ret;
3045e705c121SKalle Valo 	unsigned long _tids = tids;
3046e705c121SKalle Valo 
3047e705c121SKalle Valo 	/* convert TIDs to ACs - we don't support TSPEC so that's OK
3048e705c121SKalle Valo 	 * Note that this field is reserved and unused by firmware not
3049e705c121SKalle Valo 	 * supporting GO uAPSD, so it's safe to always do this.
3050e705c121SKalle Valo 	 */
3051e705c121SKalle Valo 	for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3052e705c121SKalle Valo 		cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3053e705c121SKalle Valo 
3054e705c121SKalle Valo 	/* If we're releasing frames from aggregation queues then check if the
3055e705c121SKalle Valo 	 * all queues combined that we're releasing frames from have
3056e705c121SKalle Valo 	 *  - more frames than the service period, in which case more_data
3057e705c121SKalle Valo 	 *    needs to be set
3058e705c121SKalle Valo 	 *  - fewer than 'cnt' frames, in which case we need to adjust the
3059e705c121SKalle Valo 	 *    firmware command (but do that unconditionally)
3060e705c121SKalle Valo 	 */
3061e705c121SKalle Valo 	if (agg) {
3062e705c121SKalle Valo 		int remaining = cnt;
306336be0eb6SEmmanuel Grumbach 		int sleep_tx_count;
3064e705c121SKalle Valo 
3065e705c121SKalle Valo 		spin_lock_bh(&mvmsta->lock);
3066e705c121SKalle Valo 		for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3067e705c121SKalle Valo 			struct iwl_mvm_tid_data *tid_data;
3068e705c121SKalle Valo 			u16 n_queued;
3069e705c121SKalle Valo 
3070e705c121SKalle Valo 			tid_data = &mvmsta->tid_data[tid];
3071e705c121SKalle Valo 			if (WARN(tid_data->state != IWL_AGG_ON &&
3072e705c121SKalle Valo 				 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
3073e705c121SKalle Valo 				 "TID %d state is %d\n",
3074e705c121SKalle Valo 				 tid, tid_data->state)) {
3075e705c121SKalle Valo 				spin_unlock_bh(&mvmsta->lock);
3076e705c121SKalle Valo 				ieee80211_sta_eosp(sta);
3077e705c121SKalle Valo 				return;
3078e705c121SKalle Valo 			}
3079e705c121SKalle Valo 
3080e705c121SKalle Valo 			n_queued = iwl_mvm_tid_queued(tid_data);
3081e705c121SKalle Valo 			if (n_queued > remaining) {
3082e705c121SKalle Valo 				more_data = true;
3083e705c121SKalle Valo 				remaining = 0;
3084e705c121SKalle Valo 				break;
3085e705c121SKalle Valo 			}
3086e705c121SKalle Valo 			remaining -= n_queued;
3087e705c121SKalle Valo 		}
308836be0eb6SEmmanuel Grumbach 		sleep_tx_count = cnt - remaining;
308936be0eb6SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
309036be0eb6SEmmanuel Grumbach 			mvmsta->sleep_tx_count = sleep_tx_count;
3091e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
3092e705c121SKalle Valo 
309336be0eb6SEmmanuel Grumbach 		cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3094e705c121SKalle Valo 		if (WARN_ON(cnt - remaining == 0)) {
3095e705c121SKalle Valo 			ieee80211_sta_eosp(sta);
3096e705c121SKalle Valo 			return;
3097e705c121SKalle Valo 		}
3098e705c121SKalle Valo 	}
3099e705c121SKalle Valo 
3100e705c121SKalle Valo 	/* Note: this is ignored by firmware not supporting GO uAPSD */
3101e705c121SKalle Valo 	if (more_data)
3102e705c121SKalle Valo 		cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
3103e705c121SKalle Valo 
3104e705c121SKalle Valo 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3105e705c121SKalle Valo 		mvmsta->next_status_eosp = true;
3106e705c121SKalle Valo 		cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
3107e705c121SKalle Valo 	} else {
3108e705c121SKalle Valo 		cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
3109e705c121SKalle Valo 	}
3110e705c121SKalle Valo 
3111156f92f2SEmmanuel Grumbach 	/* block the Tx queues until the FW updated the sleep Tx count */
3112156f92f2SEmmanuel Grumbach 	iwl_trans_block_txq_ptrs(mvm->trans, true);
3113156f92f2SEmmanuel Grumbach 
3114156f92f2SEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3115156f92f2SEmmanuel Grumbach 				   CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3116854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3117e705c121SKalle Valo 	if (ret)
3118e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3119e705c121SKalle Valo }
3120e705c121SKalle Valo 
3121e705c121SKalle Valo void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3122e705c121SKalle Valo 			   struct iwl_rx_cmd_buffer *rxb)
3123e705c121SKalle Valo {
3124e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3125e705c121SKalle Valo 	struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3126e705c121SKalle Valo 	struct ieee80211_sta *sta;
3127e705c121SKalle Valo 	u32 sta_id = le32_to_cpu(notif->sta_id);
3128e705c121SKalle Valo 
3129e705c121SKalle Valo 	if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
3130e705c121SKalle Valo 		return;
3131e705c121SKalle Valo 
3132e705c121SKalle Valo 	rcu_read_lock();
3133e705c121SKalle Valo 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3134e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(sta))
3135e705c121SKalle Valo 		ieee80211_sta_eosp(sta);
3136e705c121SKalle Valo 	rcu_read_unlock();
3137e705c121SKalle Valo }
3138e705c121SKalle Valo 
3139e705c121SKalle Valo void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3140e705c121SKalle Valo 				   struct iwl_mvm_sta *mvmsta, bool disable)
3141e705c121SKalle Valo {
3142e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3143e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3144e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3145e705c121SKalle Valo 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3146e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3147e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3148e705c121SKalle Valo 	};
3149e705c121SKalle Valo 	int ret;
3150e705c121SKalle Valo 
3151854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3152854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3153e705c121SKalle Valo 	if (ret)
3154e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3155e705c121SKalle Valo }
3156e705c121SKalle Valo 
3157e705c121SKalle Valo void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3158e705c121SKalle Valo 				      struct ieee80211_sta *sta,
3159e705c121SKalle Valo 				      bool disable)
3160e705c121SKalle Valo {
3161e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3162e705c121SKalle Valo 
3163e705c121SKalle Valo 	spin_lock_bh(&mvm_sta->lock);
3164e705c121SKalle Valo 
3165e705c121SKalle Valo 	if (mvm_sta->disable_tx == disable) {
3166e705c121SKalle Valo 		spin_unlock_bh(&mvm_sta->lock);
3167e705c121SKalle Valo 		return;
3168e705c121SKalle Valo 	}
3169e705c121SKalle Valo 
3170e705c121SKalle Valo 	mvm_sta->disable_tx = disable;
3171e705c121SKalle Valo 
3172e705c121SKalle Valo 	/*
3173e705c121SKalle Valo 	 * Tell mac80211 to start/stop queuing tx for this station,
3174e705c121SKalle Valo 	 * but don't stop queuing if there are still pending frames
3175e705c121SKalle Valo 	 * for this station.
3176e705c121SKalle Valo 	 */
3177e705c121SKalle Valo 	if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
3178e705c121SKalle Valo 		ieee80211_sta_block_awake(mvm->hw, sta, disable);
3179e705c121SKalle Valo 
3180e705c121SKalle Valo 	iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3181e705c121SKalle Valo 
3182e705c121SKalle Valo 	spin_unlock_bh(&mvm_sta->lock);
3183e705c121SKalle Valo }
3184e705c121SKalle Valo 
3185e705c121SKalle Valo void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3186e705c121SKalle Valo 				       struct iwl_mvm_vif *mvmvif,
3187e705c121SKalle Valo 				       bool disable)
3188e705c121SKalle Valo {
3189e705c121SKalle Valo 	struct ieee80211_sta *sta;
3190e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta;
3191e705c121SKalle Valo 	int i;
3192e705c121SKalle Valo 
3193e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3194e705c121SKalle Valo 
3195e705c121SKalle Valo 	/* Block/unblock all the stations of the given mvmvif */
3196e705c121SKalle Valo 	for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
3197e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3198e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
3199e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
3200e705c121SKalle Valo 			continue;
3201e705c121SKalle Valo 
3202e705c121SKalle Valo 		mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3203e705c121SKalle Valo 		if (mvm_sta->mac_id_n_color !=
3204e705c121SKalle Valo 		    FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3205e705c121SKalle Valo 			continue;
3206e705c121SKalle Valo 
3207e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3208e705c121SKalle Valo 	}
3209e705c121SKalle Valo }
3210e705c121SKalle Valo 
3211e705c121SKalle Valo void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3212e705c121SKalle Valo {
3213e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3214e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
3215e705c121SKalle Valo 
3216e705c121SKalle Valo 	rcu_read_lock();
3217e705c121SKalle Valo 
3218e705c121SKalle Valo 	mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3219e705c121SKalle Valo 
3220e705c121SKalle Valo 	if (!WARN_ON(!mvmsta))
3221e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3222e705c121SKalle Valo 
3223e705c121SKalle Valo 	rcu_read_unlock();
3224e705c121SKalle Valo }
3225