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 
3649794c64fSLiad Kaufman /*
3659794c64fSLiad Kaufman  * Remove a queue from a station's resources.
3669794c64fSLiad Kaufman  * Note that this only marks as free. It DOESN'T delete a BA agreement, and
3679794c64fSLiad Kaufman  * doesn't disable the queue
3689794c64fSLiad Kaufman  */
3699794c64fSLiad Kaufman static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
3709794c64fSLiad Kaufman {
3719794c64fSLiad Kaufman 	struct ieee80211_sta *sta;
3729794c64fSLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
3739794c64fSLiad Kaufman 	unsigned long tid_bitmap;
3749794c64fSLiad Kaufman 	unsigned long disable_agg_tids = 0;
3759794c64fSLiad Kaufman 	u8 sta_id;
3769794c64fSLiad Kaufman 	int tid;
3779794c64fSLiad Kaufman 
3789794c64fSLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
3799794c64fSLiad Kaufman 
3809794c64fSLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
3819794c64fSLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
3829794c64fSLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
3839794c64fSLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
3849794c64fSLiad Kaufman 
3859794c64fSLiad Kaufman 	rcu_read_lock();
3869794c64fSLiad Kaufman 
3879794c64fSLiad Kaufman 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3889794c64fSLiad Kaufman 
3899794c64fSLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
3909794c64fSLiad Kaufman 		rcu_read_unlock();
3919794c64fSLiad Kaufman 		return 0;
3929794c64fSLiad Kaufman 	}
3939794c64fSLiad Kaufman 
3949794c64fSLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
3959794c64fSLiad Kaufman 
3969794c64fSLiad Kaufman 	spin_lock_bh(&mvmsta->lock);
3979794c64fSLiad Kaufman 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
3989794c64fSLiad Kaufman 		mvmsta->tid_data[tid].txq_id = IEEE80211_INVAL_HW_QUEUE;
3999794c64fSLiad Kaufman 
4009794c64fSLiad Kaufman 		if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
4019794c64fSLiad Kaufman 			disable_agg_tids |= BIT(tid);
4029794c64fSLiad Kaufman 	}
4039794c64fSLiad Kaufman 	mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
4049794c64fSLiad Kaufman 
4059794c64fSLiad Kaufman 	spin_unlock_bh(&mvmsta->lock);
4069794c64fSLiad Kaufman 
4079794c64fSLiad Kaufman 	rcu_read_unlock();
4089794c64fSLiad Kaufman 
4099794c64fSLiad Kaufman 	spin_lock(&mvm->queue_info_lock);
4109794c64fSLiad Kaufman 	/* Unmap MAC queues and TIDs from this queue */
4119794c64fSLiad Kaufman 	mvm->queue_info[queue].hw_queue_to_mac80211 = 0;
4129794c64fSLiad Kaufman 	mvm->queue_info[queue].hw_queue_refcount = 0;
4139794c64fSLiad Kaufman 	mvm->queue_info[queue].tid_bitmap = 0;
4149794c64fSLiad Kaufman 	spin_unlock(&mvm->queue_info_lock);
4159794c64fSLiad Kaufman 
4169794c64fSLiad Kaufman 	return disable_agg_tids;
4179794c64fSLiad Kaufman }
4189794c64fSLiad Kaufman 
41924afba76SLiad Kaufman static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
42024afba76SLiad Kaufman 				   struct ieee80211_sta *sta, u8 ac, int tid,
42124afba76SLiad Kaufman 				   struct ieee80211_hdr *hdr)
42224afba76SLiad Kaufman {
42324afba76SLiad Kaufman 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
42424afba76SLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
42524afba76SLiad Kaufman 		.fifo = iwl_mvm_ac_to_tx_fifo[ac],
42624afba76SLiad Kaufman 		.sta_id = mvmsta->sta_id,
42724afba76SLiad Kaufman 		.tid = tid,
42824afba76SLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
42924afba76SLiad Kaufman 	};
43024afba76SLiad Kaufman 	unsigned int wdg_timeout =
43124afba76SLiad Kaufman 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
43224afba76SLiad Kaufman 	u8 mac_queue = mvmsta->vif->hw_queue[ac];
43324afba76SLiad Kaufman 	int queue = -1;
4349794c64fSLiad Kaufman 	bool using_inactive_queue = false;
4359794c64fSLiad Kaufman 	unsigned long disable_agg_tids = 0;
4369794c64fSLiad Kaufman 	enum iwl_mvm_agg_state queue_state;
43724afba76SLiad Kaufman 	int ssn;
438cf961e16SLiad Kaufman 	int ret;
43924afba76SLiad Kaufman 
44024afba76SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
44124afba76SLiad Kaufman 
442d2515a99SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
44324afba76SLiad Kaufman 
44424afba76SLiad Kaufman 	/*
44524afba76SLiad Kaufman 	 * Non-QoS, QoS NDP and MGMT frames should go to a MGMT queue, if one
44624afba76SLiad Kaufman 	 * exists
44724afba76SLiad Kaufman 	 */
44824afba76SLiad Kaufman 	if (!ieee80211_is_data_qos(hdr->frame_control) ||
44924afba76SLiad Kaufman 	    ieee80211_is_qos_nullfunc(hdr->frame_control)) {
4509794c64fSLiad Kaufman 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
4519794c64fSLiad Kaufman 						IWL_MVM_DQA_MIN_MGMT_QUEUE,
45224afba76SLiad Kaufman 						IWL_MVM_DQA_MAX_MGMT_QUEUE);
45324afba76SLiad Kaufman 		if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
45424afba76SLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
45524afba76SLiad Kaufman 					    queue);
45624afba76SLiad Kaufman 
45724afba76SLiad Kaufman 		/* If no such queue is found, we'll use a DATA queue instead */
45824afba76SLiad Kaufman 	}
45924afba76SLiad Kaufman 
4609794c64fSLiad Kaufman 	if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
4619794c64fSLiad Kaufman 	    (mvm->queue_info[mvmsta->reserved_queue].status ==
4629794c64fSLiad Kaufman 	     IWL_MVM_QUEUE_RESERVED ||
4639794c64fSLiad Kaufman 	     mvm->queue_info[mvmsta->reserved_queue].status ==
4649794c64fSLiad Kaufman 	     IWL_MVM_QUEUE_INACTIVE)) {
46524afba76SLiad Kaufman 		queue = mvmsta->reserved_queue;
4669794c64fSLiad Kaufman 		mvm->queue_info[queue].reserved = true;
46724afba76SLiad Kaufman 		IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
46824afba76SLiad Kaufman 	}
46924afba76SLiad Kaufman 
47024afba76SLiad Kaufman 	if (queue < 0)
4719794c64fSLiad Kaufman 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
4729794c64fSLiad Kaufman 						IWL_MVM_DQA_MIN_DATA_QUEUE,
47324afba76SLiad Kaufman 						IWL_MVM_DQA_MAX_DATA_QUEUE);
474cf961e16SLiad Kaufman 
475cf961e16SLiad Kaufman 	/*
4769794c64fSLiad Kaufman 	 * Check if this queue is already allocated but inactive.
4779794c64fSLiad Kaufman 	 * In such a case, we'll need to first free this queue before enabling
4789794c64fSLiad Kaufman 	 * it again, so we'll mark it as reserved to make sure no new traffic
4799794c64fSLiad Kaufman 	 * arrives on it
4809794c64fSLiad Kaufman 	 */
4819794c64fSLiad Kaufman 	if (queue > 0 &&
4829794c64fSLiad Kaufman 	    mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
4839794c64fSLiad Kaufman 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
4849794c64fSLiad Kaufman 		using_inactive_queue = true;
4859794c64fSLiad Kaufman 		IWL_DEBUG_TX_QUEUES(mvm,
4869794c64fSLiad Kaufman 				    "Re-assigning TXQ %d: sta_id=%d, tid=%d\n",
4879794c64fSLiad Kaufman 				    queue, mvmsta->sta_id, tid);
4889794c64fSLiad Kaufman 	}
4899794c64fSLiad Kaufman 
4909794c64fSLiad Kaufman 	/*
491cf961e16SLiad Kaufman 	 * Mark TXQ as ready, even though it hasn't been fully configured yet,
492cf961e16SLiad Kaufman 	 * to make sure no one else takes it.
493cf961e16SLiad Kaufman 	 * This will allow avoiding re-acquiring the lock at the end of the
494cf961e16SLiad Kaufman 	 * configuration. On error we'll mark it back as free.
495cf961e16SLiad Kaufman 	 */
49624afba76SLiad Kaufman 	if (queue >= 0)
497cf961e16SLiad Kaufman 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
49824afba76SLiad Kaufman 
499d2515a99SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
50024afba76SLiad Kaufman 
50124afba76SLiad Kaufman 	/* TODO: support shared queues for same RA */
50224afba76SLiad Kaufman 	if (queue < 0)
50324afba76SLiad Kaufman 		return -ENOSPC;
50424afba76SLiad Kaufman 
50524afba76SLiad Kaufman 	/*
50624afba76SLiad Kaufman 	 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
50724afba76SLiad Kaufman 	 * but for configuring the SCD to send A-MPDUs we need to mark the queue
50824afba76SLiad Kaufman 	 * as aggregatable.
50924afba76SLiad Kaufman 	 * Mark all DATA queues as allowing to be aggregated at some point
51024afba76SLiad Kaufman 	 */
511d5216a28SLiad Kaufman 	cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
512d5216a28SLiad Kaufman 			 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
51324afba76SLiad Kaufman 
5149794c64fSLiad Kaufman 	/*
5159794c64fSLiad Kaufman 	 * If this queue was previously inactive (idle) - we need to free it
5169794c64fSLiad Kaufman 	 * first
5179794c64fSLiad Kaufman 	 */
5189794c64fSLiad Kaufman 	if (using_inactive_queue) {
5199794c64fSLiad Kaufman 		struct iwl_scd_txq_cfg_cmd cmd = {
5209794c64fSLiad Kaufman 			.scd_queue = queue,
5219794c64fSLiad Kaufman 			.enable = 0,
5229794c64fSLiad Kaufman 		};
5239794c64fSLiad Kaufman 
5249794c64fSLiad Kaufman 		disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
5259794c64fSLiad Kaufman 
5269794c64fSLiad Kaufman 		/* Disable the queue */
5279794c64fSLiad Kaufman 		iwl_mvm_invalidate_sta_queue(mvm, queue, disable_agg_tids,
5289794c64fSLiad Kaufman 					     true);
5299794c64fSLiad Kaufman 		iwl_trans_txq_disable(mvm->trans, queue, false);
5309794c64fSLiad Kaufman 		ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd),
5319794c64fSLiad Kaufman 					   &cmd);
5329794c64fSLiad Kaufman 		if (ret) {
5339794c64fSLiad Kaufman 			IWL_ERR(mvm,
5349794c64fSLiad Kaufman 				"Failed to free inactive queue %d (ret=%d)\n",
5359794c64fSLiad Kaufman 				queue, ret);
5369794c64fSLiad Kaufman 
5379794c64fSLiad Kaufman 			/* Re-mark the inactive queue as inactive */
5389794c64fSLiad Kaufman 			spin_lock_bh(&mvm->queue_info_lock);
5399794c64fSLiad Kaufman 			mvm->queue_info[queue].status = IWL_MVM_QUEUE_INACTIVE;
5409794c64fSLiad Kaufman 			spin_unlock_bh(&mvm->queue_info_lock);
5419794c64fSLiad Kaufman 
5429794c64fSLiad Kaufman 			return ret;
5439794c64fSLiad Kaufman 		}
5449794c64fSLiad Kaufman 	}
5459794c64fSLiad Kaufman 
54624afba76SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Allocating queue #%d to sta %d on tid %d\n",
54724afba76SLiad Kaufman 			    queue, mvmsta->sta_id, tid);
54824afba76SLiad Kaufman 
54924afba76SLiad Kaufman 	ssn = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
55024afba76SLiad Kaufman 	iwl_mvm_enable_txq(mvm, queue, mac_queue, ssn, &cfg,
55124afba76SLiad Kaufman 			   wdg_timeout);
55224afba76SLiad Kaufman 
55324afba76SLiad Kaufman 	spin_lock_bh(&mvmsta->lock);
55424afba76SLiad Kaufman 	mvmsta->tid_data[tid].txq_id = queue;
5559794c64fSLiad Kaufman 	mvmsta->tid_data[tid].is_tid_active = true;
55624afba76SLiad Kaufman 	mvmsta->tfd_queue_msk |= BIT(queue);
5579794c64fSLiad Kaufman 	queue_state = mvmsta->tid_data[tid].state;
55824afba76SLiad Kaufman 
55924afba76SLiad Kaufman 	if (mvmsta->reserved_queue == queue)
56024afba76SLiad Kaufman 		mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
56124afba76SLiad Kaufman 	spin_unlock_bh(&mvmsta->lock);
56224afba76SLiad Kaufman 
563cf961e16SLiad Kaufman 	ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
564cf961e16SLiad Kaufman 	if (ret)
565cf961e16SLiad Kaufman 		goto out_err;
566cf961e16SLiad Kaufman 
5679794c64fSLiad Kaufman 	/* If we need to re-enable aggregations... */
5689794c64fSLiad Kaufman 	if (queue_state == IWL_AGG_ON)
5699794c64fSLiad Kaufman 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
5709794c64fSLiad Kaufman 
5719794c64fSLiad Kaufman 	return ret;
572cf961e16SLiad Kaufman 
573cf961e16SLiad Kaufman out_err:
574cf961e16SLiad Kaufman 	iwl_mvm_disable_txq(mvm, queue, mac_queue, tid, 0);
575cf961e16SLiad Kaufman 
576cf961e16SLiad Kaufman 	return ret;
57724afba76SLiad Kaufman }
57824afba76SLiad Kaufman 
57924afba76SLiad Kaufman static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
58024afba76SLiad Kaufman {
58124afba76SLiad Kaufman 	if (tid == IWL_MAX_TID_COUNT)
58224afba76SLiad Kaufman 		return IEEE80211_AC_VO; /* MGMT */
58324afba76SLiad Kaufman 
58424afba76SLiad Kaufman 	return tid_to_mac80211_ac[tid];
58524afba76SLiad Kaufman }
58624afba76SLiad Kaufman 
58724afba76SLiad Kaufman static void iwl_mvm_tx_deferred_stream(struct iwl_mvm *mvm,
58824afba76SLiad Kaufman 				       struct ieee80211_sta *sta, int tid)
58924afba76SLiad Kaufman {
59024afba76SLiad Kaufman 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
59124afba76SLiad Kaufman 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
59224afba76SLiad Kaufman 	struct sk_buff *skb;
59324afba76SLiad Kaufman 	struct ieee80211_hdr *hdr;
59424afba76SLiad Kaufman 	struct sk_buff_head deferred_tx;
59524afba76SLiad Kaufman 	u8 mac_queue;
59624afba76SLiad Kaufman 	bool no_queue = false; /* Marks if there is a problem with the queue */
59724afba76SLiad Kaufman 	u8 ac;
59824afba76SLiad Kaufman 
59924afba76SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
60024afba76SLiad Kaufman 
60124afba76SLiad Kaufman 	skb = skb_peek(&tid_data->deferred_tx_frames);
60224afba76SLiad Kaufman 	if (!skb)
60324afba76SLiad Kaufman 		return;
60424afba76SLiad Kaufman 	hdr = (void *)skb->data;
60524afba76SLiad Kaufman 
60624afba76SLiad Kaufman 	ac = iwl_mvm_tid_to_ac_queue(tid);
60724afba76SLiad Kaufman 	mac_queue = IEEE80211_SKB_CB(skb)->hw_queue;
60824afba76SLiad Kaufman 
60924afba76SLiad Kaufman 	if (tid_data->txq_id == IEEE80211_INVAL_HW_QUEUE &&
61024afba76SLiad Kaufman 	    iwl_mvm_sta_alloc_queue(mvm, sta, ac, tid, hdr)) {
61124afba76SLiad Kaufman 		IWL_ERR(mvm,
61224afba76SLiad Kaufman 			"Can't alloc TXQ for sta %d tid %d - dropping frame\n",
61324afba76SLiad Kaufman 			mvmsta->sta_id, tid);
61424afba76SLiad Kaufman 
61524afba76SLiad Kaufman 		/*
61624afba76SLiad Kaufman 		 * Mark queue as problematic so later the deferred traffic is
61724afba76SLiad Kaufman 		 * freed, as we can do nothing with it
61824afba76SLiad Kaufman 		 */
61924afba76SLiad Kaufman 		no_queue = true;
62024afba76SLiad Kaufman 	}
62124afba76SLiad Kaufman 
62224afba76SLiad Kaufman 	__skb_queue_head_init(&deferred_tx);
62324afba76SLiad Kaufman 
624d2515a99SLiad Kaufman 	/* Disable bottom-halves when entering TX path */
625d2515a99SLiad Kaufman 	local_bh_disable();
62624afba76SLiad Kaufman 	spin_lock(&mvmsta->lock);
62724afba76SLiad Kaufman 	skb_queue_splice_init(&tid_data->deferred_tx_frames, &deferred_tx);
62824afba76SLiad Kaufman 	spin_unlock(&mvmsta->lock);
62924afba76SLiad Kaufman 
63024afba76SLiad Kaufman 	while ((skb = __skb_dequeue(&deferred_tx)))
63124afba76SLiad Kaufman 		if (no_queue || iwl_mvm_tx_skb(mvm, skb, sta))
63224afba76SLiad Kaufman 			ieee80211_free_txskb(mvm->hw, skb);
63324afba76SLiad Kaufman 	local_bh_enable();
63424afba76SLiad Kaufman 
63524afba76SLiad Kaufman 	/* Wake queue */
63624afba76SLiad Kaufman 	iwl_mvm_start_mac_queues(mvm, BIT(mac_queue));
63724afba76SLiad Kaufman }
63824afba76SLiad Kaufman 
63924afba76SLiad Kaufman void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
64024afba76SLiad Kaufman {
64124afba76SLiad Kaufman 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
64224afba76SLiad Kaufman 					   add_stream_wk);
64324afba76SLiad Kaufman 	struct ieee80211_sta *sta;
64424afba76SLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
64524afba76SLiad Kaufman 	unsigned long deferred_tid_traffic;
64624afba76SLiad Kaufman 	int sta_id, tid;
64724afba76SLiad Kaufman 
6489794c64fSLiad Kaufman 	/* Check inactivity of queues */
6499794c64fSLiad Kaufman 	iwl_mvm_inactivity_check(mvm);
6509794c64fSLiad Kaufman 
65124afba76SLiad Kaufman 	mutex_lock(&mvm->mutex);
65224afba76SLiad Kaufman 
65324afba76SLiad Kaufman 	/* Go over all stations with deferred traffic */
65424afba76SLiad Kaufman 	for_each_set_bit(sta_id, mvm->sta_deferred_frames,
65524afba76SLiad Kaufman 			 IWL_MVM_STATION_COUNT) {
65624afba76SLiad Kaufman 		clear_bit(sta_id, mvm->sta_deferred_frames);
65724afba76SLiad Kaufman 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
65824afba76SLiad Kaufman 						lockdep_is_held(&mvm->mutex));
65924afba76SLiad Kaufman 		if (IS_ERR_OR_NULL(sta))
66024afba76SLiad Kaufman 			continue;
66124afba76SLiad Kaufman 
66224afba76SLiad Kaufman 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
66324afba76SLiad Kaufman 		deferred_tid_traffic = mvmsta->deferred_traffic_tid_map;
66424afba76SLiad Kaufman 
66524afba76SLiad Kaufman 		for_each_set_bit(tid, &deferred_tid_traffic,
66624afba76SLiad Kaufman 				 IWL_MAX_TID_COUNT + 1)
66724afba76SLiad Kaufman 			iwl_mvm_tx_deferred_stream(mvm, sta, tid);
66824afba76SLiad Kaufman 	}
66924afba76SLiad Kaufman 
67024afba76SLiad Kaufman 	mutex_unlock(&mvm->mutex);
67124afba76SLiad Kaufman }
67224afba76SLiad Kaufman 
67324afba76SLiad Kaufman static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
674d5216a28SLiad Kaufman 				      struct ieee80211_sta *sta,
675d5216a28SLiad Kaufman 				      enum nl80211_iftype vif_type)
67624afba76SLiad Kaufman {
67724afba76SLiad Kaufman 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
67824afba76SLiad Kaufman 	int queue;
67924afba76SLiad Kaufman 
6809794c64fSLiad Kaufman 	/*
6819794c64fSLiad Kaufman 	 * Check for inactive queues, so we don't reach a situation where we
6829794c64fSLiad Kaufman 	 * can't add a STA due to a shortage in queues that doesn't really exist
6839794c64fSLiad Kaufman 	 */
6849794c64fSLiad Kaufman 	iwl_mvm_inactivity_check(mvm);
6859794c64fSLiad Kaufman 
68624afba76SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
68724afba76SLiad Kaufman 
68824afba76SLiad Kaufman 	/* Make sure we have free resources for this STA */
689d5216a28SLiad Kaufman 	if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
690d5216a28SLiad Kaufman 	    !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].hw_queue_refcount &&
691cf961e16SLiad Kaufman 	    (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
692cf961e16SLiad Kaufman 	     IWL_MVM_QUEUE_FREE))
693d5216a28SLiad Kaufman 		queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
694d5216a28SLiad Kaufman 	else
6959794c64fSLiad Kaufman 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
6969794c64fSLiad Kaufman 						IWL_MVM_DQA_MIN_DATA_QUEUE,
69724afba76SLiad Kaufman 						IWL_MVM_DQA_MAX_DATA_QUEUE);
69824afba76SLiad Kaufman 	if (queue < 0) {
69924afba76SLiad Kaufman 		spin_unlock_bh(&mvm->queue_info_lock);
70024afba76SLiad Kaufman 		IWL_ERR(mvm, "No available queues for new station\n");
70124afba76SLiad Kaufman 		return -ENOSPC;
70224afba76SLiad Kaufman 	}
703cf961e16SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
70424afba76SLiad Kaufman 
70524afba76SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
70624afba76SLiad Kaufman 
70724afba76SLiad Kaufman 	mvmsta->reserved_queue = queue;
70824afba76SLiad Kaufman 
70924afba76SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
71024afba76SLiad Kaufman 			    queue, mvmsta->sta_id);
71124afba76SLiad Kaufman 
71224afba76SLiad Kaufman 	return 0;
71324afba76SLiad Kaufman }
71424afba76SLiad Kaufman 
715e705c121SKalle Valo int iwl_mvm_add_sta(struct iwl_mvm *mvm,
716e705c121SKalle Valo 		    struct ieee80211_vif *vif,
717e705c121SKalle Valo 		    struct ieee80211_sta *sta)
718e705c121SKalle Valo {
719e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
720e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
721a571f5f6SSara Sharon 	struct iwl_mvm_rxq_dup_data *dup_data;
722e705c121SKalle Valo 	int i, ret, sta_id;
723e705c121SKalle Valo 
724e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
725e705c121SKalle Valo 
726e705c121SKalle Valo 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
727e705c121SKalle Valo 		sta_id = iwl_mvm_find_free_sta_id(mvm,
728e705c121SKalle Valo 						  ieee80211_vif_type_p2p(vif));
729e705c121SKalle Valo 	else
730e705c121SKalle Valo 		sta_id = mvm_sta->sta_id;
731e705c121SKalle Valo 
732e705c121SKalle Valo 	if (sta_id == IWL_MVM_STATION_COUNT)
733e705c121SKalle Valo 		return -ENOSPC;
734e705c121SKalle Valo 
735e705c121SKalle Valo 	spin_lock_init(&mvm_sta->lock);
736e705c121SKalle Valo 
737e705c121SKalle Valo 	mvm_sta->sta_id = sta_id;
738e705c121SKalle Valo 	mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
739e705c121SKalle Valo 						      mvmvif->color);
740e705c121SKalle Valo 	mvm_sta->vif = vif;
741e705c121SKalle Valo 	mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
742e705c121SKalle Valo 	mvm_sta->tx_protection = 0;
743e705c121SKalle Valo 	mvm_sta->tt_tx_protection = false;
744e705c121SKalle Valo 
745e705c121SKalle Valo 	/* HW restart, don't assume the memory has been zeroed */
746e705c121SKalle Valo 	atomic_set(&mvm->pending_frames[sta_id], 0);
747e705c121SKalle Valo 	mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
748e705c121SKalle Valo 	mvm_sta->tfd_queue_msk = 0;
749e705c121SKalle Valo 
750e705c121SKalle Valo 	/* allocate new queues for a TDLS station */
751e705c121SKalle Valo 	if (sta->tdls) {
752e705c121SKalle Valo 		ret = iwl_mvm_tdls_sta_init(mvm, sta);
753e705c121SKalle Valo 		if (ret)
754e705c121SKalle Valo 			return ret;
75524afba76SLiad Kaufman 	} else if (!iwl_mvm_is_dqa_supported(mvm)) {
756e705c121SKalle Valo 		for (i = 0; i < IEEE80211_NUM_ACS; i++)
757e705c121SKalle Valo 			if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
758e705c121SKalle Valo 				mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
759e705c121SKalle Valo 	}
760e705c121SKalle Valo 
761e705c121SKalle Valo 	/* for HW restart - reset everything but the sequence number */
76224afba76SLiad Kaufman 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
763e705c121SKalle Valo 		u16 seq = mvm_sta->tid_data[i].seq_number;
764e705c121SKalle Valo 		memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
765e705c121SKalle Valo 		mvm_sta->tid_data[i].seq_number = seq;
76624afba76SLiad Kaufman 
76724afba76SLiad Kaufman 		if (!iwl_mvm_is_dqa_supported(mvm))
76824afba76SLiad Kaufman 			continue;
76924afba76SLiad Kaufman 
77024afba76SLiad Kaufman 		/*
77124afba76SLiad Kaufman 		 * Mark all queues for this STA as unallocated and defer TX
77224afba76SLiad Kaufman 		 * frames until the queue is allocated
77324afba76SLiad Kaufman 		 */
77424afba76SLiad Kaufman 		mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
77524afba76SLiad Kaufman 		skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
776e705c121SKalle Valo 	}
77724afba76SLiad Kaufman 	mvm_sta->deferred_traffic_tid_map = 0;
778e705c121SKalle Valo 	mvm_sta->agg_tids = 0;
779e705c121SKalle Valo 
780a571f5f6SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) &&
781a571f5f6SSara Sharon 	    !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
782a571f5f6SSara Sharon 		dup_data = kcalloc(mvm->trans->num_rx_queues,
783a571f5f6SSara Sharon 				   sizeof(*dup_data),
784a571f5f6SSara Sharon 				   GFP_KERNEL);
785a571f5f6SSara Sharon 		if (!dup_data)
786a571f5f6SSara Sharon 			return -ENOMEM;
787a571f5f6SSara Sharon 		mvm_sta->dup_data = dup_data;
788a571f5f6SSara Sharon 	}
789a571f5f6SSara Sharon 
79024afba76SLiad Kaufman 	if (iwl_mvm_is_dqa_supported(mvm)) {
791d5216a28SLiad Kaufman 		ret = iwl_mvm_reserve_sta_stream(mvm, sta,
792d5216a28SLiad Kaufman 						 ieee80211_vif_type_p2p(vif));
79324afba76SLiad Kaufman 		if (ret)
79424afba76SLiad Kaufman 			goto err;
79524afba76SLiad Kaufman 	}
79624afba76SLiad Kaufman 
79724afba76SLiad Kaufman 	ret = iwl_mvm_sta_send_to_fw(mvm, sta, false, 0);
798e705c121SKalle Valo 	if (ret)
799e705c121SKalle Valo 		goto err;
800e705c121SKalle Valo 
801e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION) {
802e705c121SKalle Valo 		if (!sta->tdls) {
803e705c121SKalle Valo 			WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
804e705c121SKalle Valo 			mvmvif->ap_sta_id = sta_id;
805e705c121SKalle Valo 		} else {
806e705c121SKalle Valo 			WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
807e705c121SKalle Valo 		}
808e705c121SKalle Valo 	}
809e705c121SKalle Valo 
810e705c121SKalle Valo 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
811e705c121SKalle Valo 
812e705c121SKalle Valo 	return 0;
813e705c121SKalle Valo 
814e705c121SKalle Valo err:
815e705c121SKalle Valo 	iwl_mvm_tdls_sta_deinit(mvm, sta);
816e705c121SKalle Valo 	return ret;
817e705c121SKalle Valo }
818e705c121SKalle Valo 
819e705c121SKalle Valo int iwl_mvm_update_sta(struct iwl_mvm *mvm,
820e705c121SKalle Valo 		       struct ieee80211_vif *vif,
821e705c121SKalle Valo 		       struct ieee80211_sta *sta)
822e705c121SKalle Valo {
82324afba76SLiad Kaufman 	return iwl_mvm_sta_send_to_fw(mvm, sta, true, 0);
824e705c121SKalle Valo }
825e705c121SKalle Valo 
826e705c121SKalle Valo int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
827e705c121SKalle Valo 		      bool drain)
828e705c121SKalle Valo {
829e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
830e705c121SKalle Valo 	int ret;
831e705c121SKalle Valo 	u32 status;
832e705c121SKalle Valo 
833e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
834e705c121SKalle Valo 
835e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
836e705c121SKalle Valo 	cmd.sta_id = mvmsta->sta_id;
837e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
838e705c121SKalle Valo 	cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
839e705c121SKalle Valo 	cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
840e705c121SKalle Valo 
841e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
842854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
843854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
844e705c121SKalle Valo 					  &cmd, &status);
845e705c121SKalle Valo 	if (ret)
846e705c121SKalle Valo 		return ret;
847e705c121SKalle Valo 
848837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
849e705c121SKalle Valo 	case ADD_STA_SUCCESS:
850e705c121SKalle Valo 		IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
851e705c121SKalle Valo 			       mvmsta->sta_id);
852e705c121SKalle Valo 		break;
853e705c121SKalle Valo 	default:
854e705c121SKalle Valo 		ret = -EIO;
855e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
856e705c121SKalle Valo 			mvmsta->sta_id);
857e705c121SKalle Valo 		break;
858e705c121SKalle Valo 	}
859e705c121SKalle Valo 
860e705c121SKalle Valo 	return ret;
861e705c121SKalle Valo }
862e705c121SKalle Valo 
863e705c121SKalle Valo /*
864e705c121SKalle Valo  * Remove a station from the FW table. Before sending the command to remove
865e705c121SKalle Valo  * the station validate that the station is indeed known to the driver (sanity
866e705c121SKalle Valo  * only).
867e705c121SKalle Valo  */
868e705c121SKalle Valo static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
869e705c121SKalle Valo {
870e705c121SKalle Valo 	struct ieee80211_sta *sta;
871e705c121SKalle Valo 	struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
872e705c121SKalle Valo 		.sta_id = sta_id,
873e705c121SKalle Valo 	};
874e705c121SKalle Valo 	int ret;
875e705c121SKalle Valo 
876e705c121SKalle Valo 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
877e705c121SKalle Valo 					lockdep_is_held(&mvm->mutex));
878e705c121SKalle Valo 
879e705c121SKalle Valo 	/* Note: internal stations are marked as error values */
880e705c121SKalle Valo 	if (!sta) {
881e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid station id\n");
882e705c121SKalle Valo 		return -EINVAL;
883e705c121SKalle Valo 	}
884e705c121SKalle Valo 
885e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
886e705c121SKalle Valo 				   sizeof(rm_sta_cmd), &rm_sta_cmd);
887e705c121SKalle Valo 	if (ret) {
888e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
889e705c121SKalle Valo 		return ret;
890e705c121SKalle Valo 	}
891e705c121SKalle Valo 
892e705c121SKalle Valo 	return 0;
893e705c121SKalle Valo }
894e705c121SKalle Valo 
895e705c121SKalle Valo void iwl_mvm_sta_drained_wk(struct work_struct *wk)
896e705c121SKalle Valo {
897e705c121SKalle Valo 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
898e705c121SKalle Valo 	u8 sta_id;
899e705c121SKalle Valo 
900e705c121SKalle Valo 	/*
901e705c121SKalle Valo 	 * The mutex is needed because of the SYNC cmd, but not only: if the
902e705c121SKalle Valo 	 * work would run concurrently with iwl_mvm_rm_sta, it would run before
903e705c121SKalle Valo 	 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
904e705c121SKalle Valo 	 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
905e705c121SKalle Valo 	 * that later.
906e705c121SKalle Valo 	 */
907e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
908e705c121SKalle Valo 
909e705c121SKalle Valo 	for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
910e705c121SKalle Valo 		int ret;
911e705c121SKalle Valo 		struct ieee80211_sta *sta =
912e705c121SKalle Valo 			rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
913e705c121SKalle Valo 						  lockdep_is_held(&mvm->mutex));
914e705c121SKalle Valo 
915e705c121SKalle Valo 		/*
916e705c121SKalle Valo 		 * This station is in use or RCU-removed; the latter happens in
917e705c121SKalle Valo 		 * managed mode, where mac80211 removes the station before we
918e705c121SKalle Valo 		 * can remove it from firmware (we can only do that after the
919e705c121SKalle Valo 		 * MAC is marked unassociated), and possibly while the deauth
920e705c121SKalle Valo 		 * frame to disconnect from the AP is still queued. Then, the
921e705c121SKalle Valo 		 * station pointer is -ENOENT when the last skb is reclaimed.
922e705c121SKalle Valo 		 */
923e705c121SKalle Valo 		if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
924e705c121SKalle Valo 			continue;
925e705c121SKalle Valo 
926e705c121SKalle Valo 		if (PTR_ERR(sta) == -EINVAL) {
927e705c121SKalle Valo 			IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
928e705c121SKalle Valo 				sta_id);
929e705c121SKalle Valo 			continue;
930e705c121SKalle Valo 		}
931e705c121SKalle Valo 
932e705c121SKalle Valo 		if (!sta) {
933e705c121SKalle Valo 			IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
934e705c121SKalle Valo 				sta_id);
935e705c121SKalle Valo 			continue;
936e705c121SKalle Valo 		}
937e705c121SKalle Valo 
938e705c121SKalle Valo 		WARN_ON(PTR_ERR(sta) != -EBUSY);
939e705c121SKalle Valo 		/* This station was removed and we waited until it got drained,
940e705c121SKalle Valo 		 * we can now proceed and remove it.
941e705c121SKalle Valo 		 */
942e705c121SKalle Valo 		ret = iwl_mvm_rm_sta_common(mvm, sta_id);
943e705c121SKalle Valo 		if (ret) {
944e705c121SKalle Valo 			IWL_ERR(mvm,
945e705c121SKalle Valo 				"Couldn't remove sta %d after it was drained\n",
946e705c121SKalle Valo 				sta_id);
947e705c121SKalle Valo 			continue;
948e705c121SKalle Valo 		}
949e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
950e705c121SKalle Valo 		clear_bit(sta_id, mvm->sta_drained);
951e705c121SKalle Valo 
952e705c121SKalle Valo 		if (mvm->tfd_drained[sta_id]) {
953e705c121SKalle Valo 			unsigned long i, msk = mvm->tfd_drained[sta_id];
954e705c121SKalle Valo 
955e705c121SKalle Valo 			for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
956e705c121SKalle Valo 				iwl_mvm_disable_txq(mvm, i, i,
957e705c121SKalle Valo 						    IWL_MAX_TID_COUNT, 0);
958e705c121SKalle Valo 
959e705c121SKalle Valo 			mvm->tfd_drained[sta_id] = 0;
960e705c121SKalle Valo 			IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
961e705c121SKalle Valo 				       sta_id, msk);
962e705c121SKalle Valo 		}
963e705c121SKalle Valo 	}
964e705c121SKalle Valo 
965e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
966e705c121SKalle Valo }
967e705c121SKalle Valo 
96824afba76SLiad Kaufman static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
96924afba76SLiad Kaufman 				       struct ieee80211_vif *vif,
97024afba76SLiad Kaufman 				       struct iwl_mvm_sta *mvm_sta)
97124afba76SLiad Kaufman {
97224afba76SLiad Kaufman 	int ac;
97324afba76SLiad Kaufman 	int i;
97424afba76SLiad Kaufman 
97524afba76SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
97624afba76SLiad Kaufman 
97724afba76SLiad Kaufman 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
97824afba76SLiad Kaufman 		if (mvm_sta->tid_data[i].txq_id == IEEE80211_INVAL_HW_QUEUE)
97924afba76SLiad Kaufman 			continue;
98024afba76SLiad Kaufman 
98124afba76SLiad Kaufman 		ac = iwl_mvm_tid_to_ac_queue(i);
98224afba76SLiad Kaufman 		iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
98324afba76SLiad Kaufman 				    vif->hw_queue[ac], i, 0);
98424afba76SLiad Kaufman 		mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
98524afba76SLiad Kaufman 	}
98624afba76SLiad Kaufman }
98724afba76SLiad Kaufman 
988e705c121SKalle Valo int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
989e705c121SKalle Valo 		   struct ieee80211_vif *vif,
990e705c121SKalle Valo 		   struct ieee80211_sta *sta)
991e705c121SKalle Valo {
992e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
993e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
994e705c121SKalle Valo 	int ret;
995e705c121SKalle Valo 
996e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
997e705c121SKalle Valo 
998a571f5f6SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
999a571f5f6SSara Sharon 		kfree(mvm_sta->dup_data);
1000a571f5f6SSara Sharon 
1001a6f035a0SLiad Kaufman 	if ((vif->type == NL80211_IFTYPE_STATION &&
1002a6f035a0SLiad Kaufman 	     mvmvif->ap_sta_id == mvm_sta->sta_id) ||
1003a6f035a0SLiad Kaufman 	    iwl_mvm_is_dqa_supported(mvm)){
1004e705c121SKalle Valo 		ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1005e705c121SKalle Valo 		if (ret)
1006e705c121SKalle Valo 			return ret;
1007e705c121SKalle Valo 		/* flush its queues here since we are freeing mvm_sta */
1008e705c121SKalle Valo 		ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, 0);
1009e705c121SKalle Valo 		if (ret)
1010e705c121SKalle Valo 			return ret;
1011e705c121SKalle Valo 		ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
1012e705c121SKalle Valo 						    mvm_sta->tfd_queue_msk);
1013e705c121SKalle Valo 		if (ret)
1014e705c121SKalle Valo 			return ret;
1015e705c121SKalle Valo 		ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1016e705c121SKalle Valo 
101724afba76SLiad Kaufman 		/* If DQA is supported - the queues can be disabled now */
101824afba76SLiad Kaufman 		if (iwl_mvm_is_dqa_supported(mvm))
101924afba76SLiad Kaufman 			iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
102024afba76SLiad Kaufman 
1021e705c121SKalle Valo 		/* if we are associated - we can't remove the AP STA now */
1022e705c121SKalle Valo 		if (vif->bss_conf.assoc)
1023e705c121SKalle Valo 			return ret;
1024e705c121SKalle Valo 
1025e705c121SKalle Valo 		/* unassoc - go ahead - remove the AP STA now */
1026e705c121SKalle Valo 		mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
1027e705c121SKalle Valo 
1028e705c121SKalle Valo 		/* clear d0i3_ap_sta_id if no longer relevant */
1029e705c121SKalle Valo 		if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
1030e705c121SKalle Valo 			mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
1031e705c121SKalle Valo 	}
1032e705c121SKalle Valo 
1033e705c121SKalle Valo 	/*
1034e705c121SKalle Valo 	 * This shouldn't happen - the TDLS channel switch should be canceled
1035e705c121SKalle Valo 	 * before the STA is removed.
1036e705c121SKalle Valo 	 */
1037e705c121SKalle Valo 	if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
1038e705c121SKalle Valo 		mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
1039e705c121SKalle Valo 		cancel_delayed_work(&mvm->tdls_cs.dwork);
1040e705c121SKalle Valo 	}
1041e705c121SKalle Valo 
1042e705c121SKalle Valo 	/*
1043e705c121SKalle Valo 	 * Make sure that the tx response code sees the station as -EBUSY and
1044e705c121SKalle Valo 	 * calls the drain worker.
1045e705c121SKalle Valo 	 */
1046e705c121SKalle Valo 	spin_lock_bh(&mvm_sta->lock);
1047e705c121SKalle Valo 	/*
1048e705c121SKalle Valo 	 * There are frames pending on the AC queues for this station.
1049e705c121SKalle Valo 	 * We need to wait until all the frames are drained...
1050e705c121SKalle Valo 	 */
1051e705c121SKalle Valo 	if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
1052e705c121SKalle Valo 		rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
1053e705c121SKalle Valo 				   ERR_PTR(-EBUSY));
1054e705c121SKalle Valo 		spin_unlock_bh(&mvm_sta->lock);
1055e705c121SKalle Valo 
1056e705c121SKalle Valo 		/* disable TDLS sta queues on drain complete */
1057e705c121SKalle Valo 		if (sta->tdls) {
1058e705c121SKalle Valo 			mvm->tfd_drained[mvm_sta->sta_id] =
1059e705c121SKalle Valo 							mvm_sta->tfd_queue_msk;
1060e705c121SKalle Valo 			IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
1061e705c121SKalle Valo 				       mvm_sta->sta_id);
1062e705c121SKalle Valo 		}
1063e705c121SKalle Valo 
1064e705c121SKalle Valo 		ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1065e705c121SKalle Valo 	} else {
1066e705c121SKalle Valo 		spin_unlock_bh(&mvm_sta->lock);
1067e705c121SKalle Valo 
1068e705c121SKalle Valo 		if (sta->tdls)
1069e705c121SKalle Valo 			iwl_mvm_tdls_sta_deinit(mvm, sta);
1070e705c121SKalle Valo 
1071e705c121SKalle Valo 		ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1072e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1073e705c121SKalle Valo 	}
1074e705c121SKalle Valo 
1075e705c121SKalle Valo 	return ret;
1076e705c121SKalle Valo }
1077e705c121SKalle Valo 
1078e705c121SKalle Valo int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1079e705c121SKalle Valo 		      struct ieee80211_vif *vif,
1080e705c121SKalle Valo 		      u8 sta_id)
1081e705c121SKalle Valo {
1082e705c121SKalle Valo 	int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1083e705c121SKalle Valo 
1084e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1085e705c121SKalle Valo 
1086e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1087e705c121SKalle Valo 	return ret;
1088e705c121SKalle Valo }
1089e705c121SKalle Valo 
10900e39eb03SChaya Rachel Ivgi int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1091e705c121SKalle Valo 			     struct iwl_mvm_int_sta *sta,
1092e705c121SKalle Valo 			     u32 qmask, enum nl80211_iftype iftype)
1093e705c121SKalle Valo {
1094e705c121SKalle Valo 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1095e705c121SKalle Valo 		sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
1096e705c121SKalle Valo 		if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
1097e705c121SKalle Valo 			return -ENOSPC;
1098e705c121SKalle Valo 	}
1099e705c121SKalle Valo 
1100e705c121SKalle Valo 	sta->tfd_queue_msk = qmask;
1101e705c121SKalle Valo 
1102e705c121SKalle Valo 	/* put a non-NULL value so iterating over the stations won't stop */
1103e705c121SKalle Valo 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1104e705c121SKalle Valo 	return 0;
1105e705c121SKalle Valo }
1106e705c121SKalle Valo 
1107e705c121SKalle Valo static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
1108e705c121SKalle Valo 				    struct iwl_mvm_int_sta *sta)
1109e705c121SKalle Valo {
1110e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
1111e705c121SKalle Valo 	memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
1112e705c121SKalle Valo 	sta->sta_id = IWL_MVM_STATION_COUNT;
1113e705c121SKalle Valo }
1114e705c121SKalle Valo 
1115e705c121SKalle Valo static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1116e705c121SKalle Valo 				      struct iwl_mvm_int_sta *sta,
1117e705c121SKalle Valo 				      const u8 *addr,
1118e705c121SKalle Valo 				      u16 mac_id, u16 color)
1119e705c121SKalle Valo {
1120e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd;
1121e705c121SKalle Valo 	int ret;
1122e705c121SKalle Valo 	u32 status;
1123e705c121SKalle Valo 
1124e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1125e705c121SKalle Valo 
1126e705c121SKalle Valo 	memset(&cmd, 0, sizeof(cmd));
1127e705c121SKalle Valo 	cmd.sta_id = sta->sta_id;
1128e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1129e705c121SKalle Valo 							     color));
1130e705c121SKalle Valo 
1131e705c121SKalle Valo 	cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1132cf0cda19SLiad Kaufman 	cmd.tid_disable_tx = cpu_to_le16(0xffff);
1133e705c121SKalle Valo 
1134e705c121SKalle Valo 	if (addr)
1135e705c121SKalle Valo 		memcpy(cmd.addr, addr, ETH_ALEN);
1136e705c121SKalle Valo 
1137854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1138854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
1139e705c121SKalle Valo 					  &cmd, &status);
1140e705c121SKalle Valo 	if (ret)
1141e705c121SKalle Valo 		return ret;
1142e705c121SKalle Valo 
1143837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1144e705c121SKalle Valo 	case ADD_STA_SUCCESS:
1145e705c121SKalle Valo 		IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1146e705c121SKalle Valo 		return 0;
1147e705c121SKalle Valo 	default:
1148e705c121SKalle Valo 		ret = -EIO;
1149e705c121SKalle Valo 		IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1150e705c121SKalle Valo 			status);
1151e705c121SKalle Valo 		break;
1152e705c121SKalle Valo 	}
1153e705c121SKalle Valo 	return ret;
1154e705c121SKalle Valo }
1155e705c121SKalle Valo 
1156e705c121SKalle Valo int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1157e705c121SKalle Valo {
1158e705c121SKalle Valo 	unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1159e705c121SKalle Valo 					mvm->cfg->base_params->wd_timeout :
1160e705c121SKalle Valo 					IWL_WATCHDOG_DISABLED;
1161e705c121SKalle Valo 	int ret;
1162e705c121SKalle Valo 
1163e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1164e705c121SKalle Valo 
1165e705c121SKalle Valo 	/* Map Aux queue to fifo - needs to happen before adding Aux station */
1166e705c121SKalle Valo 	iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
1167e705c121SKalle Valo 			      IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
1168e705c121SKalle Valo 
1169e705c121SKalle Valo 	/* Allocate aux station and assign to it the aux queue */
1170e705c121SKalle Valo 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
1171e705c121SKalle Valo 				       NL80211_IFTYPE_UNSPECIFIED);
1172e705c121SKalle Valo 	if (ret)
1173e705c121SKalle Valo 		return ret;
1174e705c121SKalle Valo 
1175e705c121SKalle Valo 	ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
1176e705c121SKalle Valo 					 MAC_INDEX_AUX, 0);
1177e705c121SKalle Valo 
1178e705c121SKalle Valo 	if (ret)
1179e705c121SKalle Valo 		iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1180e705c121SKalle Valo 	return ret;
1181e705c121SKalle Valo }
1182e705c121SKalle Valo 
11830e39eb03SChaya Rachel Ivgi int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
11840e39eb03SChaya Rachel Ivgi {
11850e39eb03SChaya Rachel Ivgi 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
11860e39eb03SChaya Rachel Ivgi 
11870e39eb03SChaya Rachel Ivgi 	lockdep_assert_held(&mvm->mutex);
11880e39eb03SChaya Rachel Ivgi 	return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
11890e39eb03SChaya Rachel Ivgi 					 mvmvif->id, 0);
11900e39eb03SChaya Rachel Ivgi }
11910e39eb03SChaya Rachel Ivgi 
11920e39eb03SChaya Rachel Ivgi int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
11930e39eb03SChaya Rachel Ivgi {
11940e39eb03SChaya Rachel Ivgi 	int ret;
11950e39eb03SChaya Rachel Ivgi 
11960e39eb03SChaya Rachel Ivgi 	lockdep_assert_held(&mvm->mutex);
11970e39eb03SChaya Rachel Ivgi 
11980e39eb03SChaya Rachel Ivgi 	ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
11990e39eb03SChaya Rachel Ivgi 	if (ret)
12000e39eb03SChaya Rachel Ivgi 		IWL_WARN(mvm, "Failed sending remove station\n");
12010e39eb03SChaya Rachel Ivgi 
12020e39eb03SChaya Rachel Ivgi 	return ret;
12030e39eb03SChaya Rachel Ivgi }
12040e39eb03SChaya Rachel Ivgi 
12050e39eb03SChaya Rachel Ivgi void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
12060e39eb03SChaya Rachel Ivgi {
12070e39eb03SChaya Rachel Ivgi 	iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
12080e39eb03SChaya Rachel Ivgi }
12090e39eb03SChaya Rachel Ivgi 
1210e705c121SKalle Valo void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
1211e705c121SKalle Valo {
1212e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1213e705c121SKalle Valo 
1214e705c121SKalle Valo 	iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1215e705c121SKalle Valo }
1216e705c121SKalle Valo 
1217e705c121SKalle Valo /*
1218e705c121SKalle Valo  * Send the add station command for the vif's broadcast station.
1219e705c121SKalle Valo  * Assumes that the station was already allocated.
1220e705c121SKalle Valo  *
1221e705c121SKalle Valo  * @mvm: the mvm component
1222e705c121SKalle Valo  * @vif: the interface to which the broadcast station is added
1223e705c121SKalle Valo  * @bsta: the broadcast station to add.
1224e705c121SKalle Valo  */
1225e705c121SKalle Valo int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1226e705c121SKalle Valo {
1227e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1228e705c121SKalle Valo 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
1229e705c121SKalle Valo 	static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1230e705c121SKalle Valo 	const u8 *baddr = _baddr;
1231e705c121SKalle Valo 
1232e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1233e705c121SKalle Valo 
1234de24f638SLiad Kaufman 	if (iwl_mvm_is_dqa_supported(mvm)) {
1235de24f638SLiad Kaufman 		struct iwl_trans_txq_scd_cfg cfg = {
1236de24f638SLiad Kaufman 			.fifo = IWL_MVM_TX_FIFO_VO,
1237de24f638SLiad Kaufman 			.sta_id = mvmvif->bcast_sta.sta_id,
1238de24f638SLiad Kaufman 			.tid = IWL_MAX_TID_COUNT,
1239de24f638SLiad Kaufman 			.aggregate = false,
1240de24f638SLiad Kaufman 			.frame_limit = IWL_FRAME_LIMIT,
1241de24f638SLiad Kaufman 		};
1242de24f638SLiad Kaufman 		unsigned int wdg_timeout =
1243de24f638SLiad Kaufman 			iwl_mvm_get_wd_timeout(mvm, vif, false, false);
1244de24f638SLiad Kaufman 		int queue;
1245de24f638SLiad Kaufman 
1246de24f638SLiad Kaufman 		if ((vif->type == NL80211_IFTYPE_AP) &&
1247de24f638SLiad Kaufman 		    (mvmvif->bcast_sta.tfd_queue_msk &
1248de24f638SLiad Kaufman 		     BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE)))
1249de24f638SLiad Kaufman 			queue = IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
12504c965139SLiad Kaufman 		else if ((vif->type == NL80211_IFTYPE_P2P_DEVICE) &&
12514c965139SLiad Kaufman 			 (mvmvif->bcast_sta.tfd_queue_msk &
12524c965139SLiad Kaufman 			  BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE)))
12534c965139SLiad Kaufman 			queue = IWL_MVM_DQA_P2P_DEVICE_QUEUE;
1254de24f638SLiad Kaufman 		else if (WARN(1, "Missed required TXQ for adding bcast STA\n"))
1255de24f638SLiad Kaufman 			return -EINVAL;
1256de24f638SLiad Kaufman 
1257de24f638SLiad Kaufman 		iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0, &cfg,
1258de24f638SLiad Kaufman 				   wdg_timeout);
1259de24f638SLiad Kaufman 	}
1260de24f638SLiad Kaufman 
1261e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_ADHOC)
1262e705c121SKalle Valo 		baddr = vif->bss_conf.bssid;
1263e705c121SKalle Valo 
1264e705c121SKalle Valo 	if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
1265e705c121SKalle Valo 		return -ENOSPC;
1266e705c121SKalle Valo 
1267e705c121SKalle Valo 	return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
1268e705c121SKalle Valo 					  mvmvif->id, mvmvif->color);
1269e705c121SKalle Valo }
1270e705c121SKalle Valo 
1271e705c121SKalle Valo /* Send the FW a request to remove the station from it's internal data
1272e705c121SKalle Valo  * structures, but DO NOT remove the entry from the local data structures. */
1273e705c121SKalle Valo int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1274e705c121SKalle Valo {
1275e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1276e705c121SKalle Valo 	int ret;
1277e705c121SKalle Valo 
1278e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1279e705c121SKalle Valo 
1280e705c121SKalle Valo 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
1281e705c121SKalle Valo 	if (ret)
1282e705c121SKalle Valo 		IWL_WARN(mvm, "Failed sending remove station\n");
1283e705c121SKalle Valo 	return ret;
1284e705c121SKalle Valo }
1285e705c121SKalle Valo 
1286e705c121SKalle Valo int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1287e705c121SKalle Valo {
1288e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1289de24f638SLiad Kaufman 	u32 qmask = 0;
1290e705c121SKalle Valo 
1291e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1292e705c121SKalle Valo 
1293de24f638SLiad Kaufman 	if (!iwl_mvm_is_dqa_supported(mvm))
1294e705c121SKalle Valo 		qmask = iwl_mvm_mac_get_queues_mask(vif);
1295e705c121SKalle Valo 
1296de24f638SLiad Kaufman 	if (vif->type == NL80211_IFTYPE_AP) {
1297e705c121SKalle Valo 		/*
1298e705c121SKalle Valo 		 * The firmware defines the TFD queue mask to only be relevant
1299e705c121SKalle Valo 		 * for *unicast* queues, so the multicast (CAB) queue shouldn't
1300e705c121SKalle Valo 		 * be included.
1301e705c121SKalle Valo 		 */
1302e705c121SKalle Valo 		qmask &= ~BIT(vif->cab_queue);
1303e705c121SKalle Valo 
1304de24f638SLiad Kaufman 		if (iwl_mvm_is_dqa_supported(mvm))
1305de24f638SLiad Kaufman 			qmask |= BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE);
13064c965139SLiad Kaufman 	} else if (iwl_mvm_is_dqa_supported(mvm) &&
13074c965139SLiad Kaufman 		   vif->type == NL80211_IFTYPE_P2P_DEVICE) {
13084c965139SLiad Kaufman 		qmask |= BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE);
1309de24f638SLiad Kaufman 	}
1310de24f638SLiad Kaufman 
1311e705c121SKalle Valo 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
1312e705c121SKalle Valo 					ieee80211_vif_type_p2p(vif));
1313e705c121SKalle Valo }
1314e705c121SKalle Valo 
1315e705c121SKalle Valo /* Allocate a new station entry for the broadcast station to the given vif,
1316e705c121SKalle Valo  * and send it to the FW.
1317e705c121SKalle Valo  * Note that each P2P mac should have its own broadcast station.
1318e705c121SKalle Valo  *
1319e705c121SKalle Valo  * @mvm: the mvm component
1320e705c121SKalle Valo  * @vif: the interface to which the broadcast station is added
1321e705c121SKalle Valo  * @bsta: the broadcast station to add. */
1322e705c121SKalle Valo int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1323e705c121SKalle Valo {
1324e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1325e705c121SKalle Valo 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
1326e705c121SKalle Valo 	int ret;
1327e705c121SKalle Valo 
1328e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1329e705c121SKalle Valo 
1330e705c121SKalle Valo 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1331e705c121SKalle Valo 	if (ret)
1332e705c121SKalle Valo 		return ret;
1333e705c121SKalle Valo 
1334e705c121SKalle Valo 	ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
1335e705c121SKalle Valo 
1336e705c121SKalle Valo 	if (ret)
1337e705c121SKalle Valo 		iwl_mvm_dealloc_int_sta(mvm, bsta);
1338e705c121SKalle Valo 
1339e705c121SKalle Valo 	return ret;
1340e705c121SKalle Valo }
1341e705c121SKalle Valo 
1342e705c121SKalle Valo void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1343e705c121SKalle Valo {
1344e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1345e705c121SKalle Valo 
1346e705c121SKalle Valo 	iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
1347e705c121SKalle Valo }
1348e705c121SKalle Valo 
1349e705c121SKalle Valo /*
1350e705c121SKalle Valo  * Send the FW a request to remove the station from it's internal data
1351e705c121SKalle Valo  * structures, and in addition remove it from the local data structure.
1352e705c121SKalle Valo  */
1353e705c121SKalle Valo int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1354e705c121SKalle Valo {
1355e705c121SKalle Valo 	int ret;
1356e705c121SKalle Valo 
1357e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1358e705c121SKalle Valo 
1359e705c121SKalle Valo 	ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
1360e705c121SKalle Valo 
1361e705c121SKalle Valo 	iwl_mvm_dealloc_bcast_sta(mvm, vif);
1362e705c121SKalle Valo 
1363e705c121SKalle Valo 	return ret;
1364e705c121SKalle Valo }
1365e705c121SKalle Valo 
1366e705c121SKalle Valo #define IWL_MAX_RX_BA_SESSIONS 16
1367e705c121SKalle Valo 
1368b915c101SSara Sharon static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
136910b2b201SSara Sharon {
1370b915c101SSara Sharon 	struct iwl_mvm_delba_notif notif = {
1371b915c101SSara Sharon 		.metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
1372b915c101SSara Sharon 		.metadata.sync = 1,
1373b915c101SSara Sharon 		.delba.baid = baid,
1374b915c101SSara Sharon 	};
1375b915c101SSara Sharon 	iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
137610b2b201SSara Sharon };
137710b2b201SSara Sharon 
1378b915c101SSara Sharon static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
1379b915c101SSara Sharon 				 struct iwl_mvm_baid_data *data)
1380b915c101SSara Sharon {
1381b915c101SSara Sharon 	int i;
1382b915c101SSara Sharon 
1383b915c101SSara Sharon 	iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
1384b915c101SSara Sharon 
1385b915c101SSara Sharon 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
1386b915c101SSara Sharon 		int j;
1387b915c101SSara Sharon 		struct iwl_mvm_reorder_buffer *reorder_buf =
1388b915c101SSara Sharon 			&data->reorder_buf[i];
1389b915c101SSara Sharon 
13900690405fSSara Sharon 		spin_lock_bh(&reorder_buf->lock);
13910690405fSSara Sharon 		if (likely(!reorder_buf->num_stored)) {
13920690405fSSara Sharon 			spin_unlock_bh(&reorder_buf->lock);
1393b915c101SSara Sharon 			continue;
13940690405fSSara Sharon 		}
1395b915c101SSara Sharon 
1396b915c101SSara Sharon 		/*
1397b915c101SSara Sharon 		 * This shouldn't happen in regular DELBA since the internal
1398b915c101SSara Sharon 		 * delBA notification should trigger a release of all frames in
1399b915c101SSara Sharon 		 * the reorder buffer.
1400b915c101SSara Sharon 		 */
1401b915c101SSara Sharon 		WARN_ON(1);
1402b915c101SSara Sharon 
1403b915c101SSara Sharon 		for (j = 0; j < reorder_buf->buf_size; j++)
1404b915c101SSara Sharon 			__skb_queue_purge(&reorder_buf->entries[j]);
14050690405fSSara Sharon 		/*
14060690405fSSara Sharon 		 * Prevent timer re-arm. This prevents a very far fetched case
14070690405fSSara Sharon 		 * where we timed out on the notification. There may be prior
14080690405fSSara Sharon 		 * RX frames pending in the RX queue before the notification
14090690405fSSara Sharon 		 * that might get processed between now and the actual deletion
14100690405fSSara Sharon 		 * and we would re-arm the timer although we are deleting the
14110690405fSSara Sharon 		 * reorder buffer.
14120690405fSSara Sharon 		 */
14130690405fSSara Sharon 		reorder_buf->removed = true;
14140690405fSSara Sharon 		spin_unlock_bh(&reorder_buf->lock);
14150690405fSSara Sharon 		del_timer_sync(&reorder_buf->reorder_timer);
1416b915c101SSara Sharon 	}
1417b915c101SSara Sharon }
1418b915c101SSara Sharon 
1419b915c101SSara Sharon static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
1420b915c101SSara Sharon 					u32 sta_id,
1421b915c101SSara Sharon 					struct iwl_mvm_baid_data *data,
1422b915c101SSara Sharon 					u16 ssn, u8 buf_size)
1423b915c101SSara Sharon {
1424b915c101SSara Sharon 	int i;
1425b915c101SSara Sharon 
1426b915c101SSara Sharon 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
1427b915c101SSara Sharon 		struct iwl_mvm_reorder_buffer *reorder_buf =
1428b915c101SSara Sharon 			&data->reorder_buf[i];
1429b915c101SSara Sharon 		int j;
1430b915c101SSara Sharon 
1431b915c101SSara Sharon 		reorder_buf->num_stored = 0;
1432b915c101SSara Sharon 		reorder_buf->head_sn = ssn;
1433b915c101SSara Sharon 		reorder_buf->buf_size = buf_size;
14340690405fSSara Sharon 		/* rx reorder timer */
14350690405fSSara Sharon 		reorder_buf->reorder_timer.function =
14360690405fSSara Sharon 			iwl_mvm_reorder_timer_expired;
14370690405fSSara Sharon 		reorder_buf->reorder_timer.data = (unsigned long)reorder_buf;
14380690405fSSara Sharon 		init_timer(&reorder_buf->reorder_timer);
14390690405fSSara Sharon 		spin_lock_init(&reorder_buf->lock);
14400690405fSSara Sharon 		reorder_buf->mvm = mvm;
1441b915c101SSara Sharon 		reorder_buf->queue = i;
1442b915c101SSara Sharon 		reorder_buf->sta_id = sta_id;
1443b915c101SSara Sharon 		for (j = 0; j < reorder_buf->buf_size; j++)
1444b915c101SSara Sharon 			__skb_queue_head_init(&reorder_buf->entries[j]);
1445b915c101SSara Sharon 	}
144610b2b201SSara Sharon }
144710b2b201SSara Sharon 
1448e705c121SKalle Valo int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
144910b2b201SSara Sharon 		       int tid, u16 ssn, bool start, u8 buf_size, u16 timeout)
1450e705c121SKalle Valo {
1451e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1452e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
145310b2b201SSara Sharon 	struct iwl_mvm_baid_data *baid_data = NULL;
1454e705c121SKalle Valo 	int ret;
1455e705c121SKalle Valo 	u32 status;
1456e705c121SKalle Valo 
1457e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1458e705c121SKalle Valo 
1459e705c121SKalle Valo 	if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
1460e705c121SKalle Valo 		IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
1461e705c121SKalle Valo 		return -ENOSPC;
1462e705c121SKalle Valo 	}
1463e705c121SKalle Valo 
146410b2b201SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) && start) {
146510b2b201SSara Sharon 		/*
146610b2b201SSara Sharon 		 * Allocate here so if allocation fails we can bail out early
146710b2b201SSara Sharon 		 * before starting the BA session in the firmware
146810b2b201SSara Sharon 		 */
1469b915c101SSara Sharon 		baid_data = kzalloc(sizeof(*baid_data) +
1470b915c101SSara Sharon 				    mvm->trans->num_rx_queues *
1471b915c101SSara Sharon 				    sizeof(baid_data->reorder_buf[0]),
1472b915c101SSara Sharon 				    GFP_KERNEL);
147310b2b201SSara Sharon 		if (!baid_data)
147410b2b201SSara Sharon 			return -ENOMEM;
147510b2b201SSara Sharon 	}
147610b2b201SSara Sharon 
1477e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
1478e705c121SKalle Valo 	cmd.sta_id = mvm_sta->sta_id;
1479e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
1480e705c121SKalle Valo 	if (start) {
1481e705c121SKalle Valo 		cmd.add_immediate_ba_tid = (u8) tid;
1482e705c121SKalle Valo 		cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
1483854c5705SSara Sharon 		cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
1484e705c121SKalle Valo 	} else {
1485e705c121SKalle Valo 		cmd.remove_immediate_ba_tid = (u8) tid;
1486e705c121SKalle Valo 	}
1487e705c121SKalle Valo 	cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
1488e705c121SKalle Valo 				  STA_MODIFY_REMOVE_BA_TID;
1489e705c121SKalle Valo 
1490e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
1491854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1492854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
1493e705c121SKalle Valo 					  &cmd, &status);
1494e705c121SKalle Valo 	if (ret)
149510b2b201SSara Sharon 		goto out_free;
1496e705c121SKalle Valo 
1497837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1498e705c121SKalle Valo 	case ADD_STA_SUCCESS:
1499e705c121SKalle Valo 		IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
1500e705c121SKalle Valo 			       start ? "start" : "stopp");
1501e705c121SKalle Valo 		break;
1502e705c121SKalle Valo 	case ADD_STA_IMMEDIATE_BA_FAILURE:
1503e705c121SKalle Valo 		IWL_WARN(mvm, "RX BA Session refused by fw\n");
1504e705c121SKalle Valo 		ret = -ENOSPC;
1505e705c121SKalle Valo 		break;
1506e705c121SKalle Valo 	default:
1507e705c121SKalle Valo 		ret = -EIO;
1508e705c121SKalle Valo 		IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
1509e705c121SKalle Valo 			start ? "start" : "stopp", status);
1510e705c121SKalle Valo 		break;
1511e705c121SKalle Valo 	}
1512e705c121SKalle Valo 
151310b2b201SSara Sharon 	if (ret)
151410b2b201SSara Sharon 		goto out_free;
151510b2b201SSara Sharon 
151610b2b201SSara Sharon 	if (start) {
151710b2b201SSara Sharon 		u8 baid;
151810b2b201SSara Sharon 
1519e705c121SKalle Valo 		mvm->rx_ba_sessions++;
152010b2b201SSara Sharon 
152110b2b201SSara Sharon 		if (!iwl_mvm_has_new_rx_api(mvm))
152210b2b201SSara Sharon 			return 0;
152310b2b201SSara Sharon 
152410b2b201SSara Sharon 		if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
152510b2b201SSara Sharon 			ret = -EINVAL;
152610b2b201SSara Sharon 			goto out_free;
152710b2b201SSara Sharon 		}
152810b2b201SSara Sharon 		baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
152910b2b201SSara Sharon 			    IWL_ADD_STA_BAID_SHIFT);
153010b2b201SSara Sharon 		baid_data->baid = baid;
153110b2b201SSara Sharon 		baid_data->timeout = timeout;
153210b2b201SSara Sharon 		baid_data->last_rx = jiffies;
153310b2b201SSara Sharon 		init_timer(&baid_data->session_timer);
153410b2b201SSara Sharon 		baid_data->session_timer.function =
153510b2b201SSara Sharon 			iwl_mvm_rx_agg_session_expired;
153610b2b201SSara Sharon 		baid_data->session_timer.data =
153710b2b201SSara Sharon 			(unsigned long)&mvm->baid_map[baid];
153810b2b201SSara Sharon 		baid_data->mvm = mvm;
153910b2b201SSara Sharon 		baid_data->tid = tid;
154010b2b201SSara Sharon 		baid_data->sta_id = mvm_sta->sta_id;
154110b2b201SSara Sharon 
154210b2b201SSara Sharon 		mvm_sta->tid_to_baid[tid] = baid;
154310b2b201SSara Sharon 		if (timeout)
154410b2b201SSara Sharon 			mod_timer(&baid_data->session_timer,
154510b2b201SSara Sharon 				  TU_TO_EXP_TIME(timeout * 2));
154610b2b201SSara Sharon 
1547b915c101SSara Sharon 		iwl_mvm_init_reorder_buffer(mvm, mvm_sta->sta_id,
1548b915c101SSara Sharon 					    baid_data, ssn, buf_size);
154910b2b201SSara Sharon 		/*
155010b2b201SSara Sharon 		 * protect the BA data with RCU to cover a case where our
155110b2b201SSara Sharon 		 * internal RX sync mechanism will timeout (not that it's
155210b2b201SSara Sharon 		 * supposed to happen) and we will free the session data while
155310b2b201SSara Sharon 		 * RX is being processed in parallel
155410b2b201SSara Sharon 		 */
155510b2b201SSara Sharon 		WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
155610b2b201SSara Sharon 		rcu_assign_pointer(mvm->baid_map[baid], baid_data);
155710b2b201SSara Sharon 	} else if (mvm->rx_ba_sessions > 0) {
155810b2b201SSara Sharon 		u8 baid = mvm_sta->tid_to_baid[tid];
155910b2b201SSara Sharon 
1560e705c121SKalle Valo 		/* check that restart flow didn't zero the counter */
1561e705c121SKalle Valo 		mvm->rx_ba_sessions--;
156210b2b201SSara Sharon 		if (!iwl_mvm_has_new_rx_api(mvm))
156310b2b201SSara Sharon 			return 0;
1564e705c121SKalle Valo 
156510b2b201SSara Sharon 		if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
156610b2b201SSara Sharon 			return -EINVAL;
156710b2b201SSara Sharon 
156810b2b201SSara Sharon 		baid_data = rcu_access_pointer(mvm->baid_map[baid]);
156910b2b201SSara Sharon 		if (WARN_ON(!baid_data))
157010b2b201SSara Sharon 			return -EINVAL;
157110b2b201SSara Sharon 
157210b2b201SSara Sharon 		/* synchronize all rx queues so we can safely delete */
1573b915c101SSara Sharon 		iwl_mvm_free_reorder(mvm, baid_data);
157410b2b201SSara Sharon 		del_timer_sync(&baid_data->session_timer);
157510b2b201SSara Sharon 		RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
157610b2b201SSara Sharon 		kfree_rcu(baid_data, rcu_head);
157710b2b201SSara Sharon 	}
157810b2b201SSara Sharon 	return 0;
157910b2b201SSara Sharon 
158010b2b201SSara Sharon out_free:
158110b2b201SSara Sharon 	kfree(baid_data);
1582e705c121SKalle Valo 	return ret;
1583e705c121SKalle Valo }
1584e705c121SKalle Valo 
15859794c64fSLiad Kaufman int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
1586e705c121SKalle Valo 		       int tid, u8 queue, bool start)
1587e705c121SKalle Valo {
1588e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1589e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
1590e705c121SKalle Valo 	int ret;
1591e705c121SKalle Valo 	u32 status;
1592e705c121SKalle Valo 
1593e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1594e705c121SKalle Valo 
1595e705c121SKalle Valo 	if (start) {
1596e705c121SKalle Valo 		mvm_sta->tfd_queue_msk |= BIT(queue);
1597e705c121SKalle Valo 		mvm_sta->tid_disable_agg &= ~BIT(tid);
1598e705c121SKalle Valo 	} else {
1599cf961e16SLiad Kaufman 		/* In DQA-mode the queue isn't removed on agg termination */
1600cf961e16SLiad Kaufman 		if (!iwl_mvm_is_dqa_supported(mvm))
1601e705c121SKalle Valo 			mvm_sta->tfd_queue_msk &= ~BIT(queue);
1602e705c121SKalle Valo 		mvm_sta->tid_disable_agg |= BIT(tid);
1603e705c121SKalle Valo 	}
1604e705c121SKalle Valo 
1605e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
1606e705c121SKalle Valo 	cmd.sta_id = mvm_sta->sta_id;
1607e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
1608e705c121SKalle Valo 	cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
1609e705c121SKalle Valo 	cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
1610e705c121SKalle Valo 	cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
1611e705c121SKalle Valo 
1612e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
1613854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1614854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
1615e705c121SKalle Valo 					  &cmd, &status);
1616e705c121SKalle Valo 	if (ret)
1617e705c121SKalle Valo 		return ret;
1618e705c121SKalle Valo 
1619837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1620e705c121SKalle Valo 	case ADD_STA_SUCCESS:
1621e705c121SKalle Valo 		break;
1622e705c121SKalle Valo 	default:
1623e705c121SKalle Valo 		ret = -EIO;
1624e705c121SKalle Valo 		IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
1625e705c121SKalle Valo 			start ? "start" : "stopp", status);
1626e705c121SKalle Valo 		break;
1627e705c121SKalle Valo 	}
1628e705c121SKalle Valo 
1629e705c121SKalle Valo 	return ret;
1630e705c121SKalle Valo }
1631e705c121SKalle Valo 
1632e705c121SKalle Valo const u8 tid_to_mac80211_ac[] = {
1633e705c121SKalle Valo 	IEEE80211_AC_BE,
1634e705c121SKalle Valo 	IEEE80211_AC_BK,
1635e705c121SKalle Valo 	IEEE80211_AC_BK,
1636e705c121SKalle Valo 	IEEE80211_AC_BE,
1637e705c121SKalle Valo 	IEEE80211_AC_VI,
1638e705c121SKalle Valo 	IEEE80211_AC_VI,
1639e705c121SKalle Valo 	IEEE80211_AC_VO,
1640e705c121SKalle Valo 	IEEE80211_AC_VO,
16419794c64fSLiad Kaufman 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
1642e705c121SKalle Valo };
1643e705c121SKalle Valo 
1644e705c121SKalle Valo static const u8 tid_to_ucode_ac[] = {
1645e705c121SKalle Valo 	AC_BE,
1646e705c121SKalle Valo 	AC_BK,
1647e705c121SKalle Valo 	AC_BK,
1648e705c121SKalle Valo 	AC_BE,
1649e705c121SKalle Valo 	AC_VI,
1650e705c121SKalle Valo 	AC_VI,
1651e705c121SKalle Valo 	AC_VO,
1652e705c121SKalle Valo 	AC_VO,
1653e705c121SKalle Valo };
1654e705c121SKalle Valo 
1655e705c121SKalle Valo int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1656e705c121SKalle Valo 			     struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1657e705c121SKalle Valo {
1658e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1659e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data;
1660e705c121SKalle Valo 	int txq_id;
1661e705c121SKalle Valo 	int ret;
1662e705c121SKalle Valo 
1663e705c121SKalle Valo 	if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
1664e705c121SKalle Valo 		return -EINVAL;
1665e705c121SKalle Valo 
1666e705c121SKalle Valo 	if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
1667e705c121SKalle Valo 		IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
1668e705c121SKalle Valo 			mvmsta->tid_data[tid].state);
1669e705c121SKalle Valo 		return -ENXIO;
1670e705c121SKalle Valo 	}
1671e705c121SKalle Valo 
1672e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1673e705c121SKalle Valo 
1674e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
1675e705c121SKalle Valo 
1676e705c121SKalle Valo 	/* possible race condition - we entered D0i3 while starting agg */
1677e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
1678e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
1679e705c121SKalle Valo 		IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
1680e705c121SKalle Valo 		return -EIO;
1681e705c121SKalle Valo 	}
1682e705c121SKalle Valo 
1683e705c121SKalle Valo 	spin_lock_bh(&mvm->queue_info_lock);
1684e705c121SKalle Valo 
1685cf961e16SLiad Kaufman 	/*
1686cf961e16SLiad Kaufman 	 * Note the possible cases:
1687cf961e16SLiad Kaufman 	 *  1. In DQA mode with an enabled TXQ - TXQ needs to become agg'ed
1688cf961e16SLiad Kaufman 	 *  2. Non-DQA mode: the TXQ hasn't yet been enabled, so find a free
1689cf961e16SLiad Kaufman 	 *	one and mark it as reserved
1690cf961e16SLiad Kaufman 	 *  3. In DQA mode, but no traffic yet on this TID: same treatment as in
1691cf961e16SLiad Kaufman 	 *	non-DQA mode, since the TXQ hasn't yet been allocated
1692cf961e16SLiad Kaufman 	 */
1693cf961e16SLiad Kaufman 	txq_id = mvmsta->tid_data[tid].txq_id;
1694cf961e16SLiad Kaufman 	if (!iwl_mvm_is_dqa_supported(mvm) ||
1695cf961e16SLiad Kaufman 	    mvm->queue_info[txq_id].status != IWL_MVM_QUEUE_READY) {
16969794c64fSLiad Kaufman 		txq_id = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
16979794c64fSLiad Kaufman 						 mvm->first_agg_queue,
1698e705c121SKalle Valo 						 mvm->last_agg_queue);
1699e705c121SKalle Valo 		if (txq_id < 0) {
1700e705c121SKalle Valo 			ret = txq_id;
1701e705c121SKalle Valo 			spin_unlock_bh(&mvm->queue_info_lock);
1702e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to allocate agg queue\n");
1703e705c121SKalle Valo 			goto release_locks;
1704e705c121SKalle Valo 		}
1705cf961e16SLiad Kaufman 
1706cf961e16SLiad Kaufman 		/* TXQ hasn't yet been enabled, so mark it only as reserved */
1707cf961e16SLiad Kaufman 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
1708cf961e16SLiad Kaufman 	}
1709e705c121SKalle Valo 	spin_unlock_bh(&mvm->queue_info_lock);
1710e705c121SKalle Valo 
1711cf961e16SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm,
1712cf961e16SLiad Kaufman 			    "AGG for tid %d will be on queue #%d\n",
1713cf961e16SLiad Kaufman 			    tid, txq_id);
1714cf961e16SLiad Kaufman 
1715e705c121SKalle Valo 	tid_data = &mvmsta->tid_data[tid];
1716e705c121SKalle Valo 	tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1717e705c121SKalle Valo 	tid_data->txq_id = txq_id;
1718e705c121SKalle Valo 	*ssn = tid_data->ssn;
1719e705c121SKalle Valo 
1720e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm,
1721e705c121SKalle Valo 			    "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
1722e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->ssn,
1723e705c121SKalle Valo 			    tid_data->next_reclaimed);
1724e705c121SKalle Valo 
1725e705c121SKalle Valo 	if (tid_data->ssn == tid_data->next_reclaimed) {
1726e705c121SKalle Valo 		tid_data->state = IWL_AGG_STARTING;
1727e705c121SKalle Valo 		ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1728e705c121SKalle Valo 	} else {
1729e705c121SKalle Valo 		tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
1730e705c121SKalle Valo 	}
1731e705c121SKalle Valo 
1732e705c121SKalle Valo 	ret = 0;
1733e705c121SKalle Valo 
1734e705c121SKalle Valo release_locks:
1735e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
1736e705c121SKalle Valo 
1737e705c121SKalle Valo 	return ret;
1738e705c121SKalle Valo }
1739e705c121SKalle Valo 
1740e705c121SKalle Valo int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1741bb81bb68SEmmanuel Grumbach 			    struct ieee80211_sta *sta, u16 tid, u8 buf_size,
1742bb81bb68SEmmanuel Grumbach 			    bool amsdu)
1743e705c121SKalle Valo {
1744e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1745e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1746e705c121SKalle Valo 	unsigned int wdg_timeout =
1747e705c121SKalle Valo 		iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
1748eea76c36SEmmanuel Grumbach 	int queue, ret;
1749cf961e16SLiad Kaufman 	bool alloc_queue = true;
1750e705c121SKalle Valo 	u16 ssn;
1751e705c121SKalle Valo 
1752eea76c36SEmmanuel Grumbach 	struct iwl_trans_txq_scd_cfg cfg = {
1753eea76c36SEmmanuel Grumbach 		.sta_id = mvmsta->sta_id,
1754eea76c36SEmmanuel Grumbach 		.tid = tid,
1755eea76c36SEmmanuel Grumbach 		.frame_limit = buf_size,
1756eea76c36SEmmanuel Grumbach 		.aggregate = true,
1757eea76c36SEmmanuel Grumbach 	};
1758eea76c36SEmmanuel Grumbach 
1759e705c121SKalle Valo 	BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
1760e705c121SKalle Valo 		     != IWL_MAX_TID_COUNT);
1761e705c121SKalle Valo 
1762e705c121SKalle Valo 	buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
1763e705c121SKalle Valo 
1764e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
1765e705c121SKalle Valo 	ssn = tid_data->ssn;
1766e705c121SKalle Valo 	queue = tid_data->txq_id;
1767e705c121SKalle Valo 	tid_data->state = IWL_AGG_ON;
1768e705c121SKalle Valo 	mvmsta->agg_tids |= BIT(tid);
1769e705c121SKalle Valo 	tid_data->ssn = 0xffff;
1770bb81bb68SEmmanuel Grumbach 	tid_data->amsdu_in_ampdu_allowed = amsdu;
1771e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
1772e705c121SKalle Valo 
1773eea76c36SEmmanuel Grumbach 	cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
1774e705c121SKalle Valo 
1775cf961e16SLiad Kaufman 	/* In DQA mode, the existing queue might need to be reconfigured */
1776cf961e16SLiad Kaufman 	if (iwl_mvm_is_dqa_supported(mvm)) {
1777cf961e16SLiad Kaufman 		spin_lock_bh(&mvm->queue_info_lock);
1778cf961e16SLiad Kaufman 		/* Maybe there is no need to even alloc a queue... */
1779cf961e16SLiad Kaufman 		if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
1780cf961e16SLiad Kaufman 			alloc_queue = false;
1781cf961e16SLiad Kaufman 		spin_unlock_bh(&mvm->queue_info_lock);
1782cf961e16SLiad Kaufman 
1783cf961e16SLiad Kaufman 		/*
1784cf961e16SLiad Kaufman 		 * Only reconfig the SCD for the queue if the window size has
1785cf961e16SLiad Kaufman 		 * changed from current (become smaller)
1786cf961e16SLiad Kaufman 		 */
1787cf961e16SLiad Kaufman 		if (!alloc_queue && buf_size < mvmsta->max_agg_bufsize) {
1788cf961e16SLiad Kaufman 			/*
1789cf961e16SLiad Kaufman 			 * If reconfiguring an existing queue, it first must be
1790cf961e16SLiad Kaufman 			 * drained
1791cf961e16SLiad Kaufman 			 */
1792cf961e16SLiad Kaufman 			ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
1793cf961e16SLiad Kaufman 							    BIT(queue));
1794cf961e16SLiad Kaufman 			if (ret) {
1795cf961e16SLiad Kaufman 				IWL_ERR(mvm,
1796cf961e16SLiad Kaufman 					"Error draining queue before reconfig\n");
1797cf961e16SLiad Kaufman 				return ret;
1798cf961e16SLiad Kaufman 			}
1799cf961e16SLiad Kaufman 
1800cf961e16SLiad Kaufman 			ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
1801cf961e16SLiad Kaufman 						   mvmsta->sta_id, tid,
1802cf961e16SLiad Kaufman 						   buf_size, ssn);
1803cf961e16SLiad Kaufman 			if (ret) {
1804cf961e16SLiad Kaufman 				IWL_ERR(mvm,
1805cf961e16SLiad Kaufman 					"Error reconfiguring TXQ #%d\n", queue);
1806cf961e16SLiad Kaufman 				return ret;
1807cf961e16SLiad Kaufman 			}
1808cf961e16SLiad Kaufman 		}
1809cf961e16SLiad Kaufman 	}
1810cf961e16SLiad Kaufman 
1811cf961e16SLiad Kaufman 	if (alloc_queue)
1812cf961e16SLiad Kaufman 		iwl_mvm_enable_txq(mvm, queue,
1813cf961e16SLiad Kaufman 				   vif->hw_queue[tid_to_mac80211_ac[tid]], ssn,
1814cf961e16SLiad Kaufman 				   &cfg, wdg_timeout);
1815e705c121SKalle Valo 
1816e705c121SKalle Valo 	ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1817e705c121SKalle Valo 	if (ret)
1818e705c121SKalle Valo 		return -EIO;
1819e705c121SKalle Valo 
1820e705c121SKalle Valo 	/* No need to mark as reserved */
1821e705c121SKalle Valo 	spin_lock_bh(&mvm->queue_info_lock);
1822cf961e16SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1823e705c121SKalle Valo 	spin_unlock_bh(&mvm->queue_info_lock);
1824e705c121SKalle Valo 
1825e705c121SKalle Valo 	/*
1826e705c121SKalle Valo 	 * Even though in theory the peer could have different
1827e705c121SKalle Valo 	 * aggregation reorder buffer sizes for different sessions,
1828e705c121SKalle Valo 	 * our ucode doesn't allow for that and has a global limit
1829e705c121SKalle Valo 	 * for each station. Therefore, use the minimum of all the
1830e705c121SKalle Valo 	 * aggregation sessions and our default value.
1831e705c121SKalle Valo 	 */
1832e705c121SKalle Valo 	mvmsta->max_agg_bufsize =
1833e705c121SKalle Valo 		min(mvmsta->max_agg_bufsize, buf_size);
1834e705c121SKalle Valo 	mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
1835e705c121SKalle Valo 
1836e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
1837e705c121SKalle Valo 		     sta->addr, tid);
1838e705c121SKalle Valo 
1839e705c121SKalle Valo 	return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
1840e705c121SKalle Valo }
1841e705c121SKalle Valo 
1842e705c121SKalle Valo int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1843e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid)
1844e705c121SKalle Valo {
1845e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1846e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1847e705c121SKalle Valo 	u16 txq_id;
1848e705c121SKalle Valo 	int err;
1849e705c121SKalle Valo 
1850e705c121SKalle Valo 
1851e705c121SKalle Valo 	/*
1852e705c121SKalle Valo 	 * If mac80211 is cleaning its state, then say that we finished since
1853e705c121SKalle Valo 	 * our state has been cleared anyway.
1854e705c121SKalle Valo 	 */
1855e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1856e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1857e705c121SKalle Valo 		return 0;
1858e705c121SKalle Valo 	}
1859e705c121SKalle Valo 
1860e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
1861e705c121SKalle Valo 
1862e705c121SKalle Valo 	txq_id = tid_data->txq_id;
1863e705c121SKalle Valo 
1864e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
1865e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
1866e705c121SKalle Valo 
1867e705c121SKalle Valo 	mvmsta->agg_tids &= ~BIT(tid);
1868e705c121SKalle Valo 
1869e705c121SKalle Valo 	spin_lock_bh(&mvm->queue_info_lock);
1870cf961e16SLiad Kaufman 	/*
1871cf961e16SLiad Kaufman 	 * The TXQ is marked as reserved only if no traffic came through yet
1872cf961e16SLiad Kaufman 	 * This means no traffic has been sent on this TID (agg'd or not), so
1873cf961e16SLiad Kaufman 	 * we no longer have use for the queue. Since it hasn't even been
1874cf961e16SLiad Kaufman 	 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
1875cf961e16SLiad Kaufman 	 * free.
1876cf961e16SLiad Kaufman 	 */
1877cf961e16SLiad Kaufman 	if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
1878cf961e16SLiad Kaufman 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
1879e705c121SKalle Valo 	spin_unlock_bh(&mvm->queue_info_lock);
1880e705c121SKalle Valo 
1881e705c121SKalle Valo 	switch (tid_data->state) {
1882e705c121SKalle Valo 	case IWL_AGG_ON:
1883e705c121SKalle Valo 		tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1884e705c121SKalle Valo 
1885e705c121SKalle Valo 		IWL_DEBUG_TX_QUEUES(mvm,
1886e705c121SKalle Valo 				    "ssn = %d, next_recl = %d\n",
1887e705c121SKalle Valo 				    tid_data->ssn, tid_data->next_reclaimed);
1888e705c121SKalle Valo 
1889e705c121SKalle Valo 		/* There are still packets for this RA / TID in the HW */
1890e705c121SKalle Valo 		if (tid_data->ssn != tid_data->next_reclaimed) {
1891e705c121SKalle Valo 			tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
1892e705c121SKalle Valo 			err = 0;
1893e705c121SKalle Valo 			break;
1894e705c121SKalle Valo 		}
1895e705c121SKalle Valo 
1896e705c121SKalle Valo 		tid_data->ssn = 0xffff;
1897e705c121SKalle Valo 		tid_data->state = IWL_AGG_OFF;
1898e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
1899e705c121SKalle Valo 
1900e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1901e705c121SKalle Valo 
1902e705c121SKalle Valo 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1903e705c121SKalle Valo 
1904cf961e16SLiad Kaufman 		if (!iwl_mvm_is_dqa_supported(mvm)) {
1905cf961e16SLiad Kaufman 			int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
1906cf961e16SLiad Kaufman 
1907cf961e16SLiad Kaufman 			iwl_mvm_disable_txq(mvm, txq_id, mac_queue, tid, 0);
1908cf961e16SLiad Kaufman 		}
1909e705c121SKalle Valo 		return 0;
1910e705c121SKalle Valo 	case IWL_AGG_STARTING:
1911e705c121SKalle Valo 	case IWL_EMPTYING_HW_QUEUE_ADDBA:
1912e705c121SKalle Valo 		/*
1913e705c121SKalle Valo 		 * The agg session has been stopped before it was set up. This
1914e705c121SKalle Valo 		 * can happen when the AddBA timer times out for example.
1915e705c121SKalle Valo 		 */
1916e705c121SKalle Valo 
1917e705c121SKalle Valo 		/* No barriers since we are under mutex */
1918e705c121SKalle Valo 		lockdep_assert_held(&mvm->mutex);
1919e705c121SKalle Valo 
1920e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1921e705c121SKalle Valo 		tid_data->state = IWL_AGG_OFF;
1922e705c121SKalle Valo 		err = 0;
1923e705c121SKalle Valo 		break;
1924e705c121SKalle Valo 	default:
1925e705c121SKalle Valo 		IWL_ERR(mvm,
1926e705c121SKalle Valo 			"Stopping AGG while state not ON or starting for %d on %d (%d)\n",
1927e705c121SKalle Valo 			mvmsta->sta_id, tid, tid_data->state);
1928e705c121SKalle Valo 		IWL_ERR(mvm,
1929e705c121SKalle Valo 			"\ttid_data->txq_id = %d\n", tid_data->txq_id);
1930e705c121SKalle Valo 		err = -EINVAL;
1931e705c121SKalle Valo 	}
1932e705c121SKalle Valo 
1933e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
1934e705c121SKalle Valo 
1935e705c121SKalle Valo 	return err;
1936e705c121SKalle Valo }
1937e705c121SKalle Valo 
1938e705c121SKalle Valo int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1939e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid)
1940e705c121SKalle Valo {
1941e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1942e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1943e705c121SKalle Valo 	u16 txq_id;
1944e705c121SKalle Valo 	enum iwl_mvm_agg_state old_state;
1945e705c121SKalle Valo 
1946e705c121SKalle Valo 	/*
1947e705c121SKalle Valo 	 * First set the agg state to OFF to avoid calling
1948e705c121SKalle Valo 	 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
1949e705c121SKalle Valo 	 */
1950e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
1951e705c121SKalle Valo 	txq_id = tid_data->txq_id;
1952e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
1953e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
1954e705c121SKalle Valo 	old_state = tid_data->state;
1955e705c121SKalle Valo 	tid_data->state = IWL_AGG_OFF;
1956e705c121SKalle Valo 	mvmsta->agg_tids &= ~BIT(tid);
1957e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
1958e705c121SKalle Valo 
1959e705c121SKalle Valo 	spin_lock_bh(&mvm->queue_info_lock);
1960cf961e16SLiad Kaufman 	/*
1961cf961e16SLiad Kaufman 	 * The TXQ is marked as reserved only if no traffic came through yet
1962cf961e16SLiad Kaufman 	 * This means no traffic has been sent on this TID (agg'd or not), so
1963cf961e16SLiad Kaufman 	 * we no longer have use for the queue. Since it hasn't even been
1964cf961e16SLiad Kaufman 	 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
1965cf961e16SLiad Kaufman 	 * free.
1966cf961e16SLiad Kaufman 	 */
1967cf961e16SLiad Kaufman 	if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
1968cf961e16SLiad Kaufman 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
1969e705c121SKalle Valo 	spin_unlock_bh(&mvm->queue_info_lock);
1970e705c121SKalle Valo 
1971e705c121SKalle Valo 	if (old_state >= IWL_AGG_ON) {
1972e705c121SKalle Valo 		iwl_mvm_drain_sta(mvm, mvmsta, true);
1973e705c121SKalle Valo 		if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
1974e705c121SKalle Valo 			IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
1975e705c121SKalle Valo 		iwl_trans_wait_tx_queue_empty(mvm->trans,
1976e705c121SKalle Valo 					      mvmsta->tfd_queue_msk);
1977e705c121SKalle Valo 		iwl_mvm_drain_sta(mvm, mvmsta, false);
1978e705c121SKalle Valo 
1979e705c121SKalle Valo 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1980e705c121SKalle Valo 
1981cf961e16SLiad Kaufman 		if (!iwl_mvm_is_dqa_supported(mvm)) {
1982cf961e16SLiad Kaufman 			int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
1983cf961e16SLiad Kaufman 
1984cf961e16SLiad Kaufman 			iwl_mvm_disable_txq(mvm, tid_data->txq_id, mac_queue,
1985cf961e16SLiad Kaufman 					    tid, 0);
1986cf961e16SLiad Kaufman 		}
1987e705c121SKalle Valo 	}
1988e705c121SKalle Valo 
1989e705c121SKalle Valo 	return 0;
1990e705c121SKalle Valo }
1991e705c121SKalle Valo 
1992e705c121SKalle Valo static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
1993e705c121SKalle Valo {
1994e705c121SKalle Valo 	int i, max = -1, max_offs = -1;
1995e705c121SKalle Valo 
1996e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1997e705c121SKalle Valo 
1998e705c121SKalle Valo 	/* Pick the unused key offset with the highest 'deleted'
1999e705c121SKalle Valo 	 * counter. Every time a key is deleted, all the counters
2000e705c121SKalle Valo 	 * are incremented and the one that was just deleted is
2001e705c121SKalle Valo 	 * reset to zero. Thus, the highest counter is the one
2002e705c121SKalle Valo 	 * that was deleted longest ago. Pick that one.
2003e705c121SKalle Valo 	 */
2004e705c121SKalle Valo 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2005e705c121SKalle Valo 		if (test_bit(i, mvm->fw_key_table))
2006e705c121SKalle Valo 			continue;
2007e705c121SKalle Valo 		if (mvm->fw_key_deleted[i] > max) {
2008e705c121SKalle Valo 			max = mvm->fw_key_deleted[i];
2009e705c121SKalle Valo 			max_offs = i;
2010e705c121SKalle Valo 		}
2011e705c121SKalle Valo 	}
2012e705c121SKalle Valo 
2013e705c121SKalle Valo 	if (max_offs < 0)
2014e705c121SKalle Valo 		return STA_KEY_IDX_INVALID;
2015e705c121SKalle Valo 
2016e705c121SKalle Valo 	return max_offs;
2017e705c121SKalle Valo }
2018e705c121SKalle Valo 
20195f7a1847SJohannes Berg static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
20204615fd15SEmmanuel Grumbach 					       struct ieee80211_vif *vif,
2021e705c121SKalle Valo 					       struct ieee80211_sta *sta)
2022e705c121SKalle Valo {
2023e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2024e705c121SKalle Valo 
20255f7a1847SJohannes Berg 	if (sta)
20265f7a1847SJohannes Berg 		return iwl_mvm_sta_from_mac80211(sta);
2027e705c121SKalle Valo 
2028e705c121SKalle Valo 	/*
2029e705c121SKalle Valo 	 * The device expects GTKs for station interfaces to be
2030e705c121SKalle Valo 	 * installed as GTKs for the AP station. If we have no
2031e705c121SKalle Valo 	 * station ID, then use AP's station ID.
2032e705c121SKalle Valo 	 */
2033e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
20344615fd15SEmmanuel Grumbach 	    mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
20354615fd15SEmmanuel Grumbach 		u8 sta_id = mvmvif->ap_sta_id;
20364615fd15SEmmanuel Grumbach 
20374615fd15SEmmanuel Grumbach 		/*
20384615fd15SEmmanuel Grumbach 		 * It is possible that the 'sta' parameter is NULL,
20394615fd15SEmmanuel Grumbach 		 * for example when a GTK is removed - the sta_id will then
20404615fd15SEmmanuel Grumbach 		 * be the AP ID, and no station was passed by mac80211.
20414615fd15SEmmanuel Grumbach 		 */
204213303c0fSSara Sharon 		return iwl_mvm_sta_from_staid_protected(mvm, sta_id);
20434615fd15SEmmanuel Grumbach 	}
2044e705c121SKalle Valo 
20455f7a1847SJohannes Berg 	return NULL;
2046e705c121SKalle Valo }
2047e705c121SKalle Valo 
2048e705c121SKalle Valo static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
2049e705c121SKalle Valo 				struct iwl_mvm_sta *mvm_sta,
2050e705c121SKalle Valo 				struct ieee80211_key_conf *keyconf, bool mcast,
20514615fd15SEmmanuel Grumbach 				u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
20524615fd15SEmmanuel Grumbach 				u8 key_offset)
2053e705c121SKalle Valo {
2054e705c121SKalle Valo 	struct iwl_mvm_add_sta_key_cmd cmd = {};
2055e705c121SKalle Valo 	__le16 key_flags;
2056e705c121SKalle Valo 	int ret;
2057e705c121SKalle Valo 	u32 status;
2058e705c121SKalle Valo 	u16 keyidx;
2059e705c121SKalle Valo 	int i;
2060e705c121SKalle Valo 	u8 sta_id = mvm_sta->sta_id;
2061e705c121SKalle Valo 
2062e705c121SKalle Valo 	keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2063e705c121SKalle Valo 		 STA_KEY_FLG_KEYID_MSK;
2064e705c121SKalle Valo 	key_flags = cpu_to_le16(keyidx);
2065e705c121SKalle Valo 	key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
2066e705c121SKalle Valo 
2067e705c121SKalle Valo 	switch (keyconf->cipher) {
2068e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
2069e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
2070e705c121SKalle Valo 		cmd.tkip_rx_tsc_byte2 = tkip_iv32;
2071e705c121SKalle Valo 		for (i = 0; i < 5; i++)
2072e705c121SKalle Valo 			cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
2073e705c121SKalle Valo 		memcpy(cmd.key, keyconf->key, keyconf->keylen);
2074e705c121SKalle Valo 		break;
2075e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
2076e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
2077e705c121SKalle Valo 		memcpy(cmd.key, keyconf->key, keyconf->keylen);
2078e705c121SKalle Valo 		break;
2079e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
2080e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
2081e705c121SKalle Valo 		/* fall through */
2082e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
2083e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
2084e705c121SKalle Valo 		memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
2085e705c121SKalle Valo 		break;
2086e705c121SKalle Valo 	default:
2087e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
2088e705c121SKalle Valo 		memcpy(cmd.key, keyconf->key, keyconf->keylen);
2089e705c121SKalle Valo 	}
2090e705c121SKalle Valo 
2091e705c121SKalle Valo 	if (mcast)
2092e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2093e705c121SKalle Valo 
20944615fd15SEmmanuel Grumbach 	cmd.key_offset = key_offset;
2095e705c121SKalle Valo 	cmd.key_flags = key_flags;
2096e705c121SKalle Valo 	cmd.sta_id = sta_id;
2097e705c121SKalle Valo 
2098e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
2099e705c121SKalle Valo 	if (cmd_flags & CMD_ASYNC)
2100e705c121SKalle Valo 		ret =  iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
2101e705c121SKalle Valo 					    sizeof(cmd), &cmd);
2102e705c121SKalle Valo 	else
2103e705c121SKalle Valo 		ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
2104e705c121SKalle Valo 						  &cmd, &status);
2105e705c121SKalle Valo 
2106e705c121SKalle Valo 	switch (status) {
2107e705c121SKalle Valo 	case ADD_STA_SUCCESS:
2108e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
2109e705c121SKalle Valo 		break;
2110e705c121SKalle Valo 	default:
2111e705c121SKalle Valo 		ret = -EIO;
2112e705c121SKalle Valo 		IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
2113e705c121SKalle Valo 		break;
2114e705c121SKalle Valo 	}
2115e705c121SKalle Valo 
2116e705c121SKalle Valo 	return ret;
2117e705c121SKalle Valo }
2118e705c121SKalle Valo 
2119e705c121SKalle Valo static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
2120e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
2121e705c121SKalle Valo 				 u8 sta_id, bool remove_key)
2122e705c121SKalle Valo {
2123e705c121SKalle Valo 	struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
2124e705c121SKalle Valo 
2125e705c121SKalle Valo 	/* verify the key details match the required command's expectations */
2126e705c121SKalle Valo 	if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
2127e705c121SKalle Valo 		    (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
2128e705c121SKalle Valo 		    (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
2129e705c121SKalle Valo 		return -EINVAL;
2130e705c121SKalle Valo 
2131e705c121SKalle Valo 	igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
2132e705c121SKalle Valo 	igtk_cmd.sta_id = cpu_to_le32(sta_id);
2133e705c121SKalle Valo 
2134e705c121SKalle Valo 	if (remove_key) {
2135e705c121SKalle Valo 		igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
2136e705c121SKalle Valo 	} else {
2137e705c121SKalle Valo 		struct ieee80211_key_seq seq;
2138e705c121SKalle Valo 		const u8 *pn;
2139e705c121SKalle Valo 
2140e705c121SKalle Valo 		memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
2141e705c121SKalle Valo 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2142e705c121SKalle Valo 		pn = seq.aes_cmac.pn;
2143e705c121SKalle Valo 		igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
2144e705c121SKalle Valo 						       ((u64) pn[4] << 8) |
2145e705c121SKalle Valo 						       ((u64) pn[3] << 16) |
2146e705c121SKalle Valo 						       ((u64) pn[2] << 24) |
2147e705c121SKalle Valo 						       ((u64) pn[1] << 32) |
2148e705c121SKalle Valo 						       ((u64) pn[0] << 40));
2149e705c121SKalle Valo 	}
2150e705c121SKalle Valo 
2151e705c121SKalle Valo 	IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
2152e705c121SKalle Valo 		       remove_key ? "removing" : "installing",
2153e705c121SKalle Valo 		       igtk_cmd.sta_id);
2154e705c121SKalle Valo 
2155e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
2156e705c121SKalle Valo 				    sizeof(igtk_cmd), &igtk_cmd);
2157e705c121SKalle Valo }
2158e705c121SKalle Valo 
2159e705c121SKalle Valo 
2160e705c121SKalle Valo static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
2161e705c121SKalle Valo 				       struct ieee80211_vif *vif,
2162e705c121SKalle Valo 				       struct ieee80211_sta *sta)
2163e705c121SKalle Valo {
2164e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2165e705c121SKalle Valo 
2166e705c121SKalle Valo 	if (sta)
2167e705c121SKalle Valo 		return sta->addr;
2168e705c121SKalle Valo 
2169e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
2170e705c121SKalle Valo 	    mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
2171e705c121SKalle Valo 		u8 sta_id = mvmvif->ap_sta_id;
2172e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
2173e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
2174e705c121SKalle Valo 		return sta->addr;
2175e705c121SKalle Valo 	}
2176e705c121SKalle Valo 
2177e705c121SKalle Valo 
2178e705c121SKalle Valo 	return NULL;
2179e705c121SKalle Valo }
2180e705c121SKalle Valo 
2181e705c121SKalle Valo static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
2182e705c121SKalle Valo 				 struct ieee80211_vif *vif,
2183e705c121SKalle Valo 				 struct ieee80211_sta *sta,
2184e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
21854615fd15SEmmanuel Grumbach 				 u8 key_offset,
2186e705c121SKalle Valo 				 bool mcast)
2187e705c121SKalle Valo {
2188e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2189e705c121SKalle Valo 	int ret;
2190e705c121SKalle Valo 	const u8 *addr;
2191e705c121SKalle Valo 	struct ieee80211_key_seq seq;
2192e705c121SKalle Valo 	u16 p1k[5];
2193e705c121SKalle Valo 
2194e705c121SKalle Valo 	switch (keyconf->cipher) {
2195e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
2196e705c121SKalle Valo 		addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
2197e705c121SKalle Valo 		/* get phase 1 key from mac80211 */
2198e705c121SKalle Valo 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2199e705c121SKalle Valo 		ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
2200e705c121SKalle Valo 		ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
22014615fd15SEmmanuel Grumbach 					   seq.tkip.iv32, p1k, 0, key_offset);
2202e705c121SKalle Valo 		break;
2203e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
2204e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
2205e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
2206e705c121SKalle Valo 		ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
22074615fd15SEmmanuel Grumbach 					   0, NULL, 0, key_offset);
2208e705c121SKalle Valo 		break;
2209e705c121SKalle Valo 	default:
2210e705c121SKalle Valo 		ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
22114615fd15SEmmanuel Grumbach 					   0, NULL, 0, key_offset);
2212e705c121SKalle Valo 	}
2213e705c121SKalle Valo 
2214e705c121SKalle Valo 	return ret;
2215e705c121SKalle Valo }
2216e705c121SKalle Valo 
2217e705c121SKalle Valo static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
2218e705c121SKalle Valo 				    struct ieee80211_key_conf *keyconf,
2219e705c121SKalle Valo 				    bool mcast)
2220e705c121SKalle Valo {
2221e705c121SKalle Valo 	struct iwl_mvm_add_sta_key_cmd cmd = {};
2222e705c121SKalle Valo 	__le16 key_flags;
2223e705c121SKalle Valo 	int ret;
2224e705c121SKalle Valo 	u32 status;
2225e705c121SKalle Valo 
2226e705c121SKalle Valo 	key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2227e705c121SKalle Valo 				 STA_KEY_FLG_KEYID_MSK);
2228e705c121SKalle Valo 	key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2229e705c121SKalle Valo 	key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2230e705c121SKalle Valo 
2231e705c121SKalle Valo 	if (mcast)
2232e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2233e705c121SKalle Valo 
2234e705c121SKalle Valo 	cmd.key_flags = key_flags;
2235e705c121SKalle Valo 	cmd.key_offset = keyconf->hw_key_idx;
2236e705c121SKalle Valo 	cmd.sta_id = sta_id;
2237e705c121SKalle Valo 
2238e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
2239e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
2240e705c121SKalle Valo 					  &cmd, &status);
2241e705c121SKalle Valo 
2242e705c121SKalle Valo 	switch (status) {
2243e705c121SKalle Valo 	case ADD_STA_SUCCESS:
2244e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2245e705c121SKalle Valo 		break;
2246e705c121SKalle Valo 	default:
2247e705c121SKalle Valo 		ret = -EIO;
2248e705c121SKalle Valo 		IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2249e705c121SKalle Valo 		break;
2250e705c121SKalle Valo 	}
2251e705c121SKalle Valo 
2252e705c121SKalle Valo 	return ret;
2253e705c121SKalle Valo }
2254e705c121SKalle Valo 
2255e705c121SKalle Valo int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
2256e705c121SKalle Valo 			struct ieee80211_vif *vif,
2257e705c121SKalle Valo 			struct ieee80211_sta *sta,
2258e705c121SKalle Valo 			struct ieee80211_key_conf *keyconf,
22594615fd15SEmmanuel Grumbach 			u8 key_offset)
2260e705c121SKalle Valo {
2261e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
22625f7a1847SJohannes Berg 	struct iwl_mvm_sta *mvm_sta;
2263e705c121SKalle Valo 	u8 sta_id;
2264e705c121SKalle Valo 	int ret;
2265e705c121SKalle Valo 	static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
2266e705c121SKalle Valo 
2267e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2268e705c121SKalle Valo 
2269e705c121SKalle Valo 	/* Get the station id from the mvm local station table */
22705f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
22715f7a1847SJohannes Berg 	if (!mvm_sta) {
22725f7a1847SJohannes Berg 		IWL_ERR(mvm, "Failed to find station\n");
2273e705c121SKalle Valo 		return -EINVAL;
2274e705c121SKalle Valo 	}
22755f7a1847SJohannes Berg 	sta_id = mvm_sta->sta_id;
2276e705c121SKalle Valo 
2277e705c121SKalle Valo 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
2278e705c121SKalle Valo 		ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
2279e705c121SKalle Valo 		goto end;
2280e705c121SKalle Valo 	}
2281e705c121SKalle Valo 
2282e705c121SKalle Valo 	/*
2283e705c121SKalle Valo 	 * It is possible that the 'sta' parameter is NULL, and thus
2284e705c121SKalle Valo 	 * there is a need to retrieve  the sta from the local station table.
2285e705c121SKalle Valo 	 */
2286e705c121SKalle Valo 	if (!sta) {
2287e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
2288e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
2289e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta)) {
2290e705c121SKalle Valo 			IWL_ERR(mvm, "Invalid station id\n");
2291e705c121SKalle Valo 			return -EINVAL;
2292e705c121SKalle Valo 		}
2293e705c121SKalle Valo 	}
2294e705c121SKalle Valo 
2295e705c121SKalle Valo 	if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
2296e705c121SKalle Valo 		return -EINVAL;
2297e705c121SKalle Valo 
22984615fd15SEmmanuel Grumbach 	/* If the key_offset is not pre-assigned, we need to find a
22994615fd15SEmmanuel Grumbach 	 * new offset to use.  In normal cases, the offset is not
23004615fd15SEmmanuel Grumbach 	 * pre-assigned, but during HW_RESTART we want to reuse the
23014615fd15SEmmanuel Grumbach 	 * same indices, so we pass them when this function is called.
23024615fd15SEmmanuel Grumbach 	 *
23034615fd15SEmmanuel Grumbach 	 * In D3 entry, we need to hardcoded the indices (because the
23044615fd15SEmmanuel Grumbach 	 * firmware hardcodes the PTK offset to 0).  In this case, we
23054615fd15SEmmanuel Grumbach 	 * need to make sure we don't overwrite the hw_key_idx in the
23064615fd15SEmmanuel Grumbach 	 * keyconf structure, because otherwise we cannot configure
23074615fd15SEmmanuel Grumbach 	 * the original ones back when resuming.
2308e705c121SKalle Valo 	 */
23094615fd15SEmmanuel Grumbach 	if (key_offset == STA_KEY_IDX_INVALID) {
23104615fd15SEmmanuel Grumbach 		key_offset  = iwl_mvm_set_fw_key_idx(mvm);
23114615fd15SEmmanuel Grumbach 		if (key_offset == STA_KEY_IDX_INVALID)
2312e705c121SKalle Valo 			return -ENOSPC;
23134615fd15SEmmanuel Grumbach 		keyconf->hw_key_idx = key_offset;
2314e705c121SKalle Valo 	}
2315e705c121SKalle Valo 
23164615fd15SEmmanuel Grumbach 	ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
23179c3deeb5SLuca Coelho 	if (ret)
2318e705c121SKalle Valo 		goto end;
2319e705c121SKalle Valo 
2320e705c121SKalle Valo 	/*
2321e705c121SKalle Valo 	 * For WEP, the same key is used for multicast and unicast. Upload it
2322e705c121SKalle Valo 	 * again, using the same key offset, and now pointing the other one
2323e705c121SKalle Valo 	 * to the same key slot (offset).
2324e705c121SKalle Valo 	 * If this fails, remove the original as well.
2325e705c121SKalle Valo 	 */
2326e705c121SKalle Valo 	if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2327e705c121SKalle Valo 	    keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
23284615fd15SEmmanuel Grumbach 		ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
23294615fd15SEmmanuel Grumbach 					    key_offset, !mcast);
2330e705c121SKalle Valo 		if (ret) {
2331e705c121SKalle Valo 			__iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
23329c3deeb5SLuca Coelho 			goto end;
2333e705c121SKalle Valo 		}
2334e705c121SKalle Valo 	}
2335e705c121SKalle Valo 
23369c3deeb5SLuca Coelho 	__set_bit(key_offset, mvm->fw_key_table);
23379c3deeb5SLuca Coelho 
2338e705c121SKalle Valo end:
2339e705c121SKalle Valo 	IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
2340e705c121SKalle Valo 		      keyconf->cipher, keyconf->keylen, keyconf->keyidx,
2341e705c121SKalle Valo 		      sta ? sta->addr : zero_addr, ret);
2342e705c121SKalle Valo 	return ret;
2343e705c121SKalle Valo }
2344e705c121SKalle Valo 
2345e705c121SKalle Valo int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
2346e705c121SKalle Valo 			   struct ieee80211_vif *vif,
2347e705c121SKalle Valo 			   struct ieee80211_sta *sta,
2348e705c121SKalle Valo 			   struct ieee80211_key_conf *keyconf)
2349e705c121SKalle Valo {
2350e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
23515f7a1847SJohannes Berg 	struct iwl_mvm_sta *mvm_sta;
23525f7a1847SJohannes Berg 	u8 sta_id = IWL_MVM_STATION_COUNT;
2353e705c121SKalle Valo 	int ret, i;
2354e705c121SKalle Valo 
2355e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2356e705c121SKalle Valo 
23575f7a1847SJohannes Berg 	/* Get the station from the mvm local station table */
23585f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
2359e705c121SKalle Valo 
2360e705c121SKalle Valo 	IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
2361e705c121SKalle Valo 		      keyconf->keyidx, sta_id);
2362e705c121SKalle Valo 
2363e705c121SKalle Valo 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
2364e705c121SKalle Valo 		return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
2365e705c121SKalle Valo 
2366e705c121SKalle Valo 	if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
2367e705c121SKalle Valo 		IWL_ERR(mvm, "offset %d not used in fw key table.\n",
2368e705c121SKalle Valo 			keyconf->hw_key_idx);
2369e705c121SKalle Valo 		return -ENOENT;
2370e705c121SKalle Valo 	}
2371e705c121SKalle Valo 
2372e705c121SKalle Valo 	/* track which key was deleted last */
2373e705c121SKalle Valo 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2374e705c121SKalle Valo 		if (mvm->fw_key_deleted[i] < U8_MAX)
2375e705c121SKalle Valo 			mvm->fw_key_deleted[i]++;
2376e705c121SKalle Valo 	}
2377e705c121SKalle Valo 	mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
2378e705c121SKalle Valo 
23795f7a1847SJohannes Berg 	if (!mvm_sta) {
2380e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
2381e705c121SKalle Valo 		return 0;
2382e705c121SKalle Valo 	}
2383e705c121SKalle Valo 
23845f7a1847SJohannes Berg 	sta_id = mvm_sta->sta_id;
23855f7a1847SJohannes Berg 
2386e705c121SKalle Valo 	ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
2387e705c121SKalle Valo 	if (ret)
2388e705c121SKalle Valo 		return ret;
2389e705c121SKalle Valo 
2390e705c121SKalle Valo 	/* delete WEP key twice to get rid of (now useless) offset */
2391e705c121SKalle Valo 	if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2392e705c121SKalle Valo 	    keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
2393e705c121SKalle Valo 		ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
2394e705c121SKalle Valo 
2395e705c121SKalle Valo 	return ret;
2396e705c121SKalle Valo }
2397e705c121SKalle Valo 
2398e705c121SKalle Valo void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
2399e705c121SKalle Valo 			     struct ieee80211_vif *vif,
2400e705c121SKalle Valo 			     struct ieee80211_key_conf *keyconf,
2401e705c121SKalle Valo 			     struct ieee80211_sta *sta, u32 iv32,
2402e705c121SKalle Valo 			     u16 *phase1key)
2403e705c121SKalle Valo {
2404e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta;
2405e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
2406e705c121SKalle Valo 
2407e705c121SKalle Valo 	rcu_read_lock();
2408e705c121SKalle Valo 
24095f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
24105f7a1847SJohannes Berg 	if (WARN_ON_ONCE(!mvm_sta))
241112f17211SEmmanuel Grumbach 		goto unlock;
2412e705c121SKalle Valo 	iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
24134615fd15SEmmanuel Grumbach 			     iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
241412f17211SEmmanuel Grumbach 
241512f17211SEmmanuel Grumbach  unlock:
2416e705c121SKalle Valo 	rcu_read_unlock();
2417e705c121SKalle Valo }
2418e705c121SKalle Valo 
2419e705c121SKalle Valo void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
2420e705c121SKalle Valo 				struct ieee80211_sta *sta)
2421e705c121SKalle Valo {
2422e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2423e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
2424e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
2425e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
2426e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_PS),
2427e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
2428e705c121SKalle Valo 	};
2429e705c121SKalle Valo 	int ret;
2430e705c121SKalle Valo 
2431854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
2432854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
2433e705c121SKalle Valo 	if (ret)
2434e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
2435e705c121SKalle Valo }
2436e705c121SKalle Valo 
2437e705c121SKalle Valo void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
2438e705c121SKalle Valo 				       struct ieee80211_sta *sta,
2439e705c121SKalle Valo 				       enum ieee80211_frame_release_type reason,
2440e705c121SKalle Valo 				       u16 cnt, u16 tids, bool more_data,
2441e705c121SKalle Valo 				       bool agg)
2442e705c121SKalle Valo {
2443e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2444e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
2445e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
2446e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
2447e705c121SKalle Valo 		.modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
2448e705c121SKalle Valo 		.sleep_tx_count = cpu_to_le16(cnt),
2449e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
2450e705c121SKalle Valo 	};
2451e705c121SKalle Valo 	int tid, ret;
2452e705c121SKalle Valo 	unsigned long _tids = tids;
2453e705c121SKalle Valo 
2454e705c121SKalle Valo 	/* convert TIDs to ACs - we don't support TSPEC so that's OK
2455e705c121SKalle Valo 	 * Note that this field is reserved and unused by firmware not
2456e705c121SKalle Valo 	 * supporting GO uAPSD, so it's safe to always do this.
2457e705c121SKalle Valo 	 */
2458e705c121SKalle Valo 	for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
2459e705c121SKalle Valo 		cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
2460e705c121SKalle Valo 
2461e705c121SKalle Valo 	/* If we're releasing frames from aggregation queues then check if the
2462e705c121SKalle Valo 	 * all queues combined that we're releasing frames from have
2463e705c121SKalle Valo 	 *  - more frames than the service period, in which case more_data
2464e705c121SKalle Valo 	 *    needs to be set
2465e705c121SKalle Valo 	 *  - fewer than 'cnt' frames, in which case we need to adjust the
2466e705c121SKalle Valo 	 *    firmware command (but do that unconditionally)
2467e705c121SKalle Valo 	 */
2468e705c121SKalle Valo 	if (agg) {
2469e705c121SKalle Valo 		int remaining = cnt;
247036be0eb6SEmmanuel Grumbach 		int sleep_tx_count;
2471e705c121SKalle Valo 
2472e705c121SKalle Valo 		spin_lock_bh(&mvmsta->lock);
2473e705c121SKalle Valo 		for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
2474e705c121SKalle Valo 			struct iwl_mvm_tid_data *tid_data;
2475e705c121SKalle Valo 			u16 n_queued;
2476e705c121SKalle Valo 
2477e705c121SKalle Valo 			tid_data = &mvmsta->tid_data[tid];
2478e705c121SKalle Valo 			if (WARN(tid_data->state != IWL_AGG_ON &&
2479e705c121SKalle Valo 				 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
2480e705c121SKalle Valo 				 "TID %d state is %d\n",
2481e705c121SKalle Valo 				 tid, tid_data->state)) {
2482e705c121SKalle Valo 				spin_unlock_bh(&mvmsta->lock);
2483e705c121SKalle Valo 				ieee80211_sta_eosp(sta);
2484e705c121SKalle Valo 				return;
2485e705c121SKalle Valo 			}
2486e705c121SKalle Valo 
2487e705c121SKalle Valo 			n_queued = iwl_mvm_tid_queued(tid_data);
2488e705c121SKalle Valo 			if (n_queued > remaining) {
2489e705c121SKalle Valo 				more_data = true;
2490e705c121SKalle Valo 				remaining = 0;
2491e705c121SKalle Valo 				break;
2492e705c121SKalle Valo 			}
2493e705c121SKalle Valo 			remaining -= n_queued;
2494e705c121SKalle Valo 		}
249536be0eb6SEmmanuel Grumbach 		sleep_tx_count = cnt - remaining;
249636be0eb6SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
249736be0eb6SEmmanuel Grumbach 			mvmsta->sleep_tx_count = sleep_tx_count;
2498e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
2499e705c121SKalle Valo 
250036be0eb6SEmmanuel Grumbach 		cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
2501e705c121SKalle Valo 		if (WARN_ON(cnt - remaining == 0)) {
2502e705c121SKalle Valo 			ieee80211_sta_eosp(sta);
2503e705c121SKalle Valo 			return;
2504e705c121SKalle Valo 		}
2505e705c121SKalle Valo 	}
2506e705c121SKalle Valo 
2507e705c121SKalle Valo 	/* Note: this is ignored by firmware not supporting GO uAPSD */
2508e705c121SKalle Valo 	if (more_data)
2509e705c121SKalle Valo 		cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
2510e705c121SKalle Valo 
2511e705c121SKalle Valo 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
2512e705c121SKalle Valo 		mvmsta->next_status_eosp = true;
2513e705c121SKalle Valo 		cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
2514e705c121SKalle Valo 	} else {
2515e705c121SKalle Valo 		cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
2516e705c121SKalle Valo 	}
2517e705c121SKalle Valo 
2518156f92f2SEmmanuel Grumbach 	/* block the Tx queues until the FW updated the sleep Tx count */
2519156f92f2SEmmanuel Grumbach 	iwl_trans_block_txq_ptrs(mvm->trans, true);
2520156f92f2SEmmanuel Grumbach 
2521156f92f2SEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
2522156f92f2SEmmanuel Grumbach 				   CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
2523854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
2524e705c121SKalle Valo 	if (ret)
2525e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
2526e705c121SKalle Valo }
2527e705c121SKalle Valo 
2528e705c121SKalle Valo void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
2529e705c121SKalle Valo 			   struct iwl_rx_cmd_buffer *rxb)
2530e705c121SKalle Valo {
2531e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2532e705c121SKalle Valo 	struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
2533e705c121SKalle Valo 	struct ieee80211_sta *sta;
2534e705c121SKalle Valo 	u32 sta_id = le32_to_cpu(notif->sta_id);
2535e705c121SKalle Valo 
2536e705c121SKalle Valo 	if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
2537e705c121SKalle Valo 		return;
2538e705c121SKalle Valo 
2539e705c121SKalle Valo 	rcu_read_lock();
2540e705c121SKalle Valo 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
2541e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(sta))
2542e705c121SKalle Valo 		ieee80211_sta_eosp(sta);
2543e705c121SKalle Valo 	rcu_read_unlock();
2544e705c121SKalle Valo }
2545e705c121SKalle Valo 
2546e705c121SKalle Valo void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
2547e705c121SKalle Valo 				   struct iwl_mvm_sta *mvmsta, bool disable)
2548e705c121SKalle Valo {
2549e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
2550e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
2551e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
2552e705c121SKalle Valo 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
2553e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
2554e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
2555e705c121SKalle Valo 	};
2556e705c121SKalle Valo 	int ret;
2557e705c121SKalle Valo 
2558854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
2559854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
2560e705c121SKalle Valo 	if (ret)
2561e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
2562e705c121SKalle Valo }
2563e705c121SKalle Valo 
2564e705c121SKalle Valo void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
2565e705c121SKalle Valo 				      struct ieee80211_sta *sta,
2566e705c121SKalle Valo 				      bool disable)
2567e705c121SKalle Valo {
2568e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2569e705c121SKalle Valo 
2570e705c121SKalle Valo 	spin_lock_bh(&mvm_sta->lock);
2571e705c121SKalle Valo 
2572e705c121SKalle Valo 	if (mvm_sta->disable_tx == disable) {
2573e705c121SKalle Valo 		spin_unlock_bh(&mvm_sta->lock);
2574e705c121SKalle Valo 		return;
2575e705c121SKalle Valo 	}
2576e705c121SKalle Valo 
2577e705c121SKalle Valo 	mvm_sta->disable_tx = disable;
2578e705c121SKalle Valo 
2579e705c121SKalle Valo 	/*
2580e705c121SKalle Valo 	 * Tell mac80211 to start/stop queuing tx for this station,
2581e705c121SKalle Valo 	 * but don't stop queuing if there are still pending frames
2582e705c121SKalle Valo 	 * for this station.
2583e705c121SKalle Valo 	 */
2584e705c121SKalle Valo 	if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
2585e705c121SKalle Valo 		ieee80211_sta_block_awake(mvm->hw, sta, disable);
2586e705c121SKalle Valo 
2587e705c121SKalle Valo 	iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
2588e705c121SKalle Valo 
2589e705c121SKalle Valo 	spin_unlock_bh(&mvm_sta->lock);
2590e705c121SKalle Valo }
2591e705c121SKalle Valo 
2592e705c121SKalle Valo void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
2593e705c121SKalle Valo 				       struct iwl_mvm_vif *mvmvif,
2594e705c121SKalle Valo 				       bool disable)
2595e705c121SKalle Valo {
2596e705c121SKalle Valo 	struct ieee80211_sta *sta;
2597e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta;
2598e705c121SKalle Valo 	int i;
2599e705c121SKalle Valo 
2600e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2601e705c121SKalle Valo 
2602e705c121SKalle Valo 	/* Block/unblock all the stations of the given mvmvif */
2603e705c121SKalle Valo 	for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
2604e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
2605e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
2606e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
2607e705c121SKalle Valo 			continue;
2608e705c121SKalle Valo 
2609e705c121SKalle Valo 		mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2610e705c121SKalle Valo 		if (mvm_sta->mac_id_n_color !=
2611e705c121SKalle Valo 		    FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
2612e705c121SKalle Valo 			continue;
2613e705c121SKalle Valo 
2614e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
2615e705c121SKalle Valo 	}
2616e705c121SKalle Valo }
2617e705c121SKalle Valo 
2618e705c121SKalle Valo void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2619e705c121SKalle Valo {
2620e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2621e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
2622e705c121SKalle Valo 
2623e705c121SKalle Valo 	rcu_read_lock();
2624e705c121SKalle Valo 
2625e705c121SKalle Valo 	mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
2626e705c121SKalle Valo 
2627e705c121SKalle Valo 	if (!WARN_ON(!mvmsta))
2628e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
2629e705c121SKalle Valo 
2630e705c121SKalle Valo 	rcu_read_unlock();
2631e705c121SKalle Valo }
2632