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
1026d6c16bSSara Sharon  * Copyright(c) 2016 - 2017 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
3726d6c16bSSara Sharon  * Copyright(c) 2016 - 2017 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 {
80ced19f26SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) ||
81ced19f26SSara Sharon 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
82ced19f26SSara Sharon 		return sizeof(struct iwl_mvm_add_sta_cmd);
83ced19f26SSara Sharon 	else
84ced19f26SSara Sharon 		return sizeof(struct iwl_mvm_add_sta_cmd_v7);
85854c5705SSara Sharon }
86854c5705SSara Sharon 
87e705c121SKalle Valo static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
88e705c121SKalle Valo 				    enum nl80211_iftype iftype)
89e705c121SKalle Valo {
90e705c121SKalle Valo 	int sta_id;
91e705c121SKalle Valo 	u32 reserved_ids = 0;
92e705c121SKalle Valo 
93e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
94e705c121SKalle Valo 	WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
95e705c121SKalle Valo 
96e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
97e705c121SKalle Valo 
98e705c121SKalle Valo 	/* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
99e705c121SKalle Valo 	if (iftype != NL80211_IFTYPE_STATION)
100e705c121SKalle Valo 		reserved_ids = BIT(0);
101e705c121SKalle Valo 
102e705c121SKalle Valo 	/* Don't take rcu_read_lock() since we are protected by mvm->mutex */
1030ae98812SSara Sharon 	for (sta_id = 0; sta_id < ARRAY_SIZE(mvm->fw_id_to_mac_id); sta_id++) {
104e705c121SKalle Valo 		if (BIT(sta_id) & reserved_ids)
105e705c121SKalle Valo 			continue;
106e705c121SKalle Valo 
107e705c121SKalle Valo 		if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
108e705c121SKalle Valo 					       lockdep_is_held(&mvm->mutex)))
109e705c121SKalle Valo 			return sta_id;
110e705c121SKalle Valo 	}
1110ae98812SSara Sharon 	return IWL_MVM_INVALID_STA;
112e705c121SKalle Valo }
113e705c121SKalle Valo 
114e705c121SKalle Valo /* send station add/update command to firmware */
115e705c121SKalle Valo int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
11624afba76SLiad Kaufman 			   bool update, unsigned int flags)
117e705c121SKalle Valo {
118e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
119e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd add_sta_cmd = {
120e705c121SKalle Valo 		.sta_id = mvm_sta->sta_id,
121e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
122e705c121SKalle Valo 		.add_modify = update ? 1 : 0,
123e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
124e705c121SKalle Valo 						 STA_FLG_MIMO_EN_MSK),
125cf0cda19SLiad Kaufman 		.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
126e705c121SKalle Valo 	};
127e705c121SKalle Valo 	int ret;
128e705c121SKalle Valo 	u32 status;
129e705c121SKalle Valo 	u32 agg_size = 0, mpdu_dens = 0;
130e705c121SKalle Valo 
131ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
132ced19f26SSara Sharon 		add_sta_cmd.station_type = mvm_sta->sta_type;
133ced19f26SSara Sharon 
13424afba76SLiad Kaufman 	if (!update || (flags & STA_MODIFY_QUEUES)) {
135e705c121SKalle Valo 		memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
13624afba76SLiad Kaufman 
137bb49701bSSara Sharon 		if (!iwl_mvm_has_new_tx_api(mvm)) {
138bb49701bSSara Sharon 			add_sta_cmd.tfd_queue_msk =
139bb49701bSSara Sharon 				cpu_to_le32(mvm_sta->tfd_queue_msk);
140bb49701bSSara Sharon 
14124afba76SLiad Kaufman 			if (flags & STA_MODIFY_QUEUES)
14224afba76SLiad Kaufman 				add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
143bb49701bSSara Sharon 		} else {
144bb49701bSSara Sharon 			WARN_ON(flags & STA_MODIFY_QUEUES);
145bb49701bSSara Sharon 		}
146e705c121SKalle Valo 	}
147e705c121SKalle Valo 
148e705c121SKalle Valo 	switch (sta->bandwidth) {
149e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_160:
150e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
151e705c121SKalle Valo 		/* fall through */
152e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_80:
153e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
154e705c121SKalle Valo 		/* fall through */
155e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_40:
156e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
157e705c121SKalle Valo 		/* fall through */
158e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_20:
159e705c121SKalle Valo 		if (sta->ht_cap.ht_supported)
160e705c121SKalle Valo 			add_sta_cmd.station_flags |=
161e705c121SKalle Valo 				cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
162e705c121SKalle Valo 		break;
163e705c121SKalle Valo 	}
164e705c121SKalle Valo 
165e705c121SKalle Valo 	switch (sta->rx_nss) {
166e705c121SKalle Valo 	case 1:
167e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
168e705c121SKalle Valo 		break;
169e705c121SKalle Valo 	case 2:
170e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
171e705c121SKalle Valo 		break;
172e705c121SKalle Valo 	case 3 ... 8:
173e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
174e705c121SKalle Valo 		break;
175e705c121SKalle Valo 	}
176e705c121SKalle Valo 
177e705c121SKalle Valo 	switch (sta->smps_mode) {
178e705c121SKalle Valo 	case IEEE80211_SMPS_AUTOMATIC:
179e705c121SKalle Valo 	case IEEE80211_SMPS_NUM_MODES:
180e705c121SKalle Valo 		WARN_ON(1);
181e705c121SKalle Valo 		break;
182e705c121SKalle Valo 	case IEEE80211_SMPS_STATIC:
183e705c121SKalle Valo 		/* override NSS */
184e705c121SKalle Valo 		add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
185e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
186e705c121SKalle Valo 		break;
187e705c121SKalle Valo 	case IEEE80211_SMPS_DYNAMIC:
188e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
189e705c121SKalle Valo 		break;
190e705c121SKalle Valo 	case IEEE80211_SMPS_OFF:
191e705c121SKalle Valo 		/* nothing */
192e705c121SKalle Valo 		break;
193e705c121SKalle Valo 	}
194e705c121SKalle Valo 
195e705c121SKalle Valo 	if (sta->ht_cap.ht_supported) {
196e705c121SKalle Valo 		add_sta_cmd.station_flags_msk |=
197e705c121SKalle Valo 			cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
198e705c121SKalle Valo 				    STA_FLG_AGG_MPDU_DENS_MSK);
199e705c121SKalle Valo 
200e705c121SKalle Valo 		mpdu_dens = sta->ht_cap.ampdu_density;
201e705c121SKalle Valo 	}
202e705c121SKalle Valo 
203e705c121SKalle Valo 	if (sta->vht_cap.vht_supported) {
204e705c121SKalle Valo 		agg_size = sta->vht_cap.cap &
205e705c121SKalle Valo 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
206e705c121SKalle Valo 		agg_size >>=
207e705c121SKalle Valo 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
208e705c121SKalle Valo 	} else if (sta->ht_cap.ht_supported) {
209e705c121SKalle Valo 		agg_size = sta->ht_cap.ampdu_factor;
210e705c121SKalle Valo 	}
211e705c121SKalle Valo 
212e705c121SKalle Valo 	add_sta_cmd.station_flags |=
213e705c121SKalle Valo 		cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
214e705c121SKalle Valo 	add_sta_cmd.station_flags |=
215e705c121SKalle Valo 		cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
2166ea29ce5SJohannes Berg 	if (mvm_sta->associated)
21719c52f46SEmmanuel Grumbach 		add_sta_cmd.assoc_id = cpu_to_le16(sta->aid);
218e705c121SKalle Valo 
21965e25482SJohannes Berg 	if (sta->wme) {
22065e25482SJohannes Berg 		add_sta_cmd.modify_mask |= STA_MODIFY_UAPSD_ACS;
22165e25482SJohannes Berg 
22265e25482SJohannes Berg 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
223c80eb570SEmmanuel Grumbach 			add_sta_cmd.uapsd_acs |= BIT(AC_BK);
22465e25482SJohannes Berg 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
225c80eb570SEmmanuel Grumbach 			add_sta_cmd.uapsd_acs |= BIT(AC_BE);
22665e25482SJohannes Berg 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
227c80eb570SEmmanuel Grumbach 			add_sta_cmd.uapsd_acs |= BIT(AC_VI);
22865e25482SJohannes Berg 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
229c80eb570SEmmanuel Grumbach 			add_sta_cmd.uapsd_acs |= BIT(AC_VO);
230c80eb570SEmmanuel Grumbach 		add_sta_cmd.uapsd_acs |= add_sta_cmd.uapsd_acs << 4;
231e71ca5eaSEmmanuel Grumbach 		add_sta_cmd.sp_length = sta->max_sp ? sta->max_sp * 2 : 128;
23265e25482SJohannes Berg 	}
23365e25482SJohannes Berg 
234e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
235854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
236854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
237e705c121SKalle Valo 					  &add_sta_cmd, &status);
238e705c121SKalle Valo 	if (ret)
239e705c121SKalle Valo 		return ret;
240e705c121SKalle Valo 
241837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
242e705c121SKalle Valo 	case ADD_STA_SUCCESS:
243e705c121SKalle Valo 		IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
244e705c121SKalle Valo 		break;
245e705c121SKalle Valo 	default:
246e705c121SKalle Valo 		ret = -EIO;
247e705c121SKalle Valo 		IWL_ERR(mvm, "ADD_STA failed\n");
248e705c121SKalle Valo 		break;
249e705c121SKalle Valo 	}
250e705c121SKalle Valo 
251e705c121SKalle Valo 	return ret;
252e705c121SKalle Valo }
253e705c121SKalle Valo 
25410b2b201SSara Sharon static void iwl_mvm_rx_agg_session_expired(unsigned long data)
25510b2b201SSara Sharon {
25610b2b201SSara Sharon 	struct iwl_mvm_baid_data __rcu **rcu_ptr = (void *)data;
25710b2b201SSara Sharon 	struct iwl_mvm_baid_data *ba_data;
25810b2b201SSara Sharon 	struct ieee80211_sta *sta;
25910b2b201SSara Sharon 	struct iwl_mvm_sta *mvm_sta;
26010b2b201SSara Sharon 	unsigned long timeout;
26110b2b201SSara Sharon 
26210b2b201SSara Sharon 	rcu_read_lock();
26310b2b201SSara Sharon 
26410b2b201SSara Sharon 	ba_data = rcu_dereference(*rcu_ptr);
26510b2b201SSara Sharon 
26610b2b201SSara Sharon 	if (WARN_ON(!ba_data))
26710b2b201SSara Sharon 		goto unlock;
26810b2b201SSara Sharon 
26910b2b201SSara Sharon 	if (!ba_data->timeout)
27010b2b201SSara Sharon 		goto unlock;
27110b2b201SSara Sharon 
27210b2b201SSara Sharon 	timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
27310b2b201SSara Sharon 	if (time_is_after_jiffies(timeout)) {
27410b2b201SSara Sharon 		mod_timer(&ba_data->session_timer, timeout);
27510b2b201SSara Sharon 		goto unlock;
27610b2b201SSara Sharon 	}
27710b2b201SSara Sharon 
27810b2b201SSara Sharon 	/* Timer expired */
27910b2b201SSara Sharon 	sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
28010b2b201SSara Sharon 	mvm_sta = iwl_mvm_sta_from_mac80211(sta);
28110b2b201SSara Sharon 	ieee80211_stop_rx_ba_session_offl(mvm_sta->vif,
28210b2b201SSara Sharon 					  sta->addr, ba_data->tid);
28310b2b201SSara Sharon unlock:
28410b2b201SSara Sharon 	rcu_read_unlock();
28510b2b201SSara Sharon }
28610b2b201SSara Sharon 
287e705c121SKalle Valo static int iwl_mvm_tdls_sta_init(struct iwl_mvm *mvm,
288e705c121SKalle Valo 				 struct ieee80211_sta *sta)
289e705c121SKalle Valo {
290e705c121SKalle Valo 	unsigned long used_hw_queues;
291e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
292e705c121SKalle Valo 	unsigned int wdg_timeout =
293e705c121SKalle Valo 		iwl_mvm_get_wd_timeout(mvm, NULL, true, false);
294e705c121SKalle Valo 	u32 ac;
295e705c121SKalle Valo 
296e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
297e705c121SKalle Valo 
298e705c121SKalle Valo 	used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, NULL);
299e705c121SKalle Valo 
300e705c121SKalle Valo 	/* Find available queues, and allocate them to the ACs */
301e705c121SKalle Valo 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
302e705c121SKalle Valo 		u8 queue = find_first_zero_bit(&used_hw_queues,
303e705c121SKalle Valo 					       mvm->first_agg_queue);
304e705c121SKalle Valo 
305e705c121SKalle Valo 		if (queue >= mvm->first_agg_queue) {
306e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to allocate STA queue\n");
307e705c121SKalle Valo 			return -EBUSY;
308e705c121SKalle Valo 		}
309e705c121SKalle Valo 
310e705c121SKalle Valo 		__set_bit(queue, &used_hw_queues);
311e705c121SKalle Valo 		mvmsta->hw_queue[ac] = queue;
312e705c121SKalle Valo 	}
313e705c121SKalle Valo 
314e705c121SKalle Valo 	/* Found a place for all queues - enable them */
315e705c121SKalle Valo 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
316e705c121SKalle Valo 		iwl_mvm_enable_ac_txq(mvm, mvmsta->hw_queue[ac],
317e705c121SKalle Valo 				      mvmsta->hw_queue[ac],
318e705c121SKalle Valo 				      iwl_mvm_ac_to_tx_fifo[ac], 0,
319e705c121SKalle Valo 				      wdg_timeout);
320e705c121SKalle Valo 		mvmsta->tfd_queue_msk |= BIT(mvmsta->hw_queue[ac]);
321e705c121SKalle Valo 	}
322e705c121SKalle Valo 
323e705c121SKalle Valo 	return 0;
324e705c121SKalle Valo }
325e705c121SKalle Valo 
326e705c121SKalle Valo static void iwl_mvm_tdls_sta_deinit(struct iwl_mvm *mvm,
327e705c121SKalle Valo 				    struct ieee80211_sta *sta)
328e705c121SKalle Valo {
329e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
330e705c121SKalle Valo 	unsigned long sta_msk;
331e705c121SKalle Valo 	int i;
332e705c121SKalle Valo 
333e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
334e705c121SKalle Valo 
335e705c121SKalle Valo 	/* disable the TDLS STA-specific queues */
336e705c121SKalle Valo 	sta_msk = mvmsta->tfd_queue_msk;
337e705c121SKalle Valo 	for_each_set_bit(i, &sta_msk, sizeof(sta_msk) * BITS_PER_BYTE)
338e705c121SKalle Valo 		iwl_mvm_disable_txq(mvm, i, i, IWL_MAX_TID_COUNT, 0);
339e705c121SKalle Valo }
340e705c121SKalle Valo 
3419794c64fSLiad Kaufman /* Disable aggregations for a bitmap of TIDs for a given station */
3429794c64fSLiad Kaufman static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
3439794c64fSLiad Kaufman 					unsigned long disable_agg_tids,
3449794c64fSLiad Kaufman 					bool remove_queue)
3459794c64fSLiad Kaufman {
3469794c64fSLiad Kaufman 	struct iwl_mvm_add_sta_cmd cmd = {};
3479794c64fSLiad Kaufman 	struct ieee80211_sta *sta;
3489794c64fSLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
3499794c64fSLiad Kaufman 	u32 status;
3509794c64fSLiad Kaufman 	u8 sta_id;
3519794c64fSLiad Kaufman 	int ret;
3529794c64fSLiad Kaufman 
353bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
354bb49701bSSara Sharon 		return -EINVAL;
355bb49701bSSara Sharon 
3569794c64fSLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
3579794c64fSLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
3589794c64fSLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
3599794c64fSLiad Kaufman 
3609794c64fSLiad Kaufman 	rcu_read_lock();
3619794c64fSLiad Kaufman 
3629794c64fSLiad Kaufman 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3639794c64fSLiad Kaufman 
3649794c64fSLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
3659794c64fSLiad Kaufman 		rcu_read_unlock();
3669794c64fSLiad Kaufman 		return -EINVAL;
3679794c64fSLiad Kaufman 	}
3689794c64fSLiad Kaufman 
3699794c64fSLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
3709794c64fSLiad Kaufman 
3719794c64fSLiad Kaufman 	mvmsta->tid_disable_agg |= disable_agg_tids;
3729794c64fSLiad Kaufman 
3739794c64fSLiad Kaufman 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
3749794c64fSLiad Kaufman 	cmd.sta_id = mvmsta->sta_id;
3759794c64fSLiad Kaufman 	cmd.add_modify = STA_MODE_MODIFY;
3769794c64fSLiad Kaufman 	cmd.modify_mask = STA_MODIFY_QUEUES;
3779794c64fSLiad Kaufman 	if (disable_agg_tids)
3789794c64fSLiad Kaufman 		cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
3799794c64fSLiad Kaufman 	if (remove_queue)
3809794c64fSLiad Kaufman 		cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
3819794c64fSLiad Kaufman 	cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
3829794c64fSLiad Kaufman 	cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
3839794c64fSLiad Kaufman 
3849794c64fSLiad Kaufman 	rcu_read_unlock();
3859794c64fSLiad Kaufman 
3869794c64fSLiad Kaufman 	/* Notify FW of queue removal from the STA queues */
3879794c64fSLiad Kaufman 	status = ADD_STA_SUCCESS;
3889794c64fSLiad Kaufman 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
3899794c64fSLiad Kaufman 					  iwl_mvm_add_sta_cmd_size(mvm),
3909794c64fSLiad Kaufman 					  &cmd, &status);
3919794c64fSLiad Kaufman 
3929794c64fSLiad Kaufman 	return ret;
3939794c64fSLiad Kaufman }
3949794c64fSLiad Kaufman 
39542db09c1SLiad Kaufman static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
39642db09c1SLiad Kaufman {
39742db09c1SLiad Kaufman 	struct ieee80211_sta *sta;
39842db09c1SLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
39942db09c1SLiad Kaufman 	unsigned long tid_bitmap;
40042db09c1SLiad Kaufman 	unsigned long agg_tids = 0;
40142db09c1SLiad Kaufman 	s8 sta_id;
40242db09c1SLiad Kaufman 	int tid;
40342db09c1SLiad Kaufman 
40442db09c1SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
40542db09c1SLiad Kaufman 
406bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
407bb49701bSSara Sharon 		return -EINVAL;
408bb49701bSSara Sharon 
40942db09c1SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
41042db09c1SLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
41142db09c1SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
41242db09c1SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
41342db09c1SLiad Kaufman 
41442db09c1SLiad Kaufman 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
41542db09c1SLiad Kaufman 					lockdep_is_held(&mvm->mutex));
41642db09c1SLiad Kaufman 
41742db09c1SLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
41842db09c1SLiad Kaufman 		return -EINVAL;
41942db09c1SLiad Kaufman 
42042db09c1SLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
42142db09c1SLiad Kaufman 
42242db09c1SLiad Kaufman 	spin_lock_bh(&mvmsta->lock);
42342db09c1SLiad Kaufman 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
42442db09c1SLiad Kaufman 		if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
42542db09c1SLiad Kaufman 			agg_tids |= BIT(tid);
42642db09c1SLiad Kaufman 	}
42742db09c1SLiad Kaufman 	spin_unlock_bh(&mvmsta->lock);
42842db09c1SLiad Kaufman 
42942db09c1SLiad Kaufman 	return agg_tids;
43042db09c1SLiad Kaufman }
43142db09c1SLiad Kaufman 
4329794c64fSLiad Kaufman /*
4339794c64fSLiad Kaufman  * Remove a queue from a station's resources.
4349794c64fSLiad Kaufman  * Note that this only marks as free. It DOESN'T delete a BA agreement, and
4359794c64fSLiad Kaufman  * doesn't disable the queue
4369794c64fSLiad Kaufman  */
4379794c64fSLiad Kaufman static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
4389794c64fSLiad Kaufman {
4399794c64fSLiad Kaufman 	struct ieee80211_sta *sta;
4409794c64fSLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
4419794c64fSLiad Kaufman 	unsigned long tid_bitmap;
4429794c64fSLiad Kaufman 	unsigned long disable_agg_tids = 0;
4439794c64fSLiad Kaufman 	u8 sta_id;
4449794c64fSLiad Kaufman 	int tid;
4459794c64fSLiad Kaufman 
4469794c64fSLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
4479794c64fSLiad Kaufman 
448bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
449bb49701bSSara Sharon 		return -EINVAL;
450bb49701bSSara Sharon 
4519794c64fSLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
4529794c64fSLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
4539794c64fSLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
4549794c64fSLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
4559794c64fSLiad Kaufman 
4569794c64fSLiad Kaufman 	rcu_read_lock();
4579794c64fSLiad Kaufman 
4589794c64fSLiad Kaufman 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
4599794c64fSLiad Kaufman 
4609794c64fSLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
4619794c64fSLiad Kaufman 		rcu_read_unlock();
4629794c64fSLiad Kaufman 		return 0;
4639794c64fSLiad Kaufman 	}
4649794c64fSLiad Kaufman 
4659794c64fSLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
4669794c64fSLiad Kaufman 
4679794c64fSLiad Kaufman 	spin_lock_bh(&mvmsta->lock);
46842db09c1SLiad Kaufman 	/* Unmap MAC queues and TIDs from this queue */
4699794c64fSLiad Kaufman 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
4709794c64fSLiad Kaufman 		if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
4719794c64fSLiad Kaufman 			disable_agg_tids |= BIT(tid);
4726862fceeSSara Sharon 		mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
4739794c64fSLiad Kaufman 	}
4749794c64fSLiad Kaufman 
47542db09c1SLiad Kaufman 	mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
4769794c64fSLiad Kaufman 	spin_unlock_bh(&mvmsta->lock);
4779794c64fSLiad Kaufman 
4789794c64fSLiad Kaufman 	rcu_read_unlock();
4799794c64fSLiad Kaufman 
4809794c64fSLiad Kaufman 	return disable_agg_tids;
4819794c64fSLiad Kaufman }
4829794c64fSLiad Kaufman 
48301796ff2SSara Sharon static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
48401796ff2SSara Sharon 				       bool same_sta)
48501796ff2SSara Sharon {
48601796ff2SSara Sharon 	struct iwl_mvm_sta *mvmsta;
48701796ff2SSara Sharon 	u8 txq_curr_ac, sta_id, tid;
48801796ff2SSara Sharon 	unsigned long disable_agg_tids = 0;
48901796ff2SSara Sharon 	int ret;
49001796ff2SSara Sharon 
49101796ff2SSara Sharon 	lockdep_assert_held(&mvm->mutex);
49201796ff2SSara Sharon 
493bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
494bb49701bSSara Sharon 		return -EINVAL;
495bb49701bSSara Sharon 
49601796ff2SSara Sharon 	spin_lock_bh(&mvm->queue_info_lock);
49701796ff2SSara Sharon 	txq_curr_ac = mvm->queue_info[queue].mac80211_ac;
49801796ff2SSara Sharon 	sta_id = mvm->queue_info[queue].ra_sta_id;
49901796ff2SSara Sharon 	tid = mvm->queue_info[queue].txq_tid;
50001796ff2SSara Sharon 	spin_unlock_bh(&mvm->queue_info_lock);
50101796ff2SSara Sharon 
50201796ff2SSara Sharon 	mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
503e3df1e4bSSharon Dvir 	if (WARN_ON(!mvmsta))
504e3df1e4bSSharon Dvir 		return -EINVAL;
50501796ff2SSara Sharon 
50601796ff2SSara Sharon 	disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
50701796ff2SSara Sharon 	/* Disable the queue */
50801796ff2SSara Sharon 	if (disable_agg_tids)
50901796ff2SSara Sharon 		iwl_mvm_invalidate_sta_queue(mvm, queue,
51001796ff2SSara Sharon 					     disable_agg_tids, false);
51101796ff2SSara Sharon 
51201796ff2SSara Sharon 	ret = iwl_mvm_disable_txq(mvm, queue,
51301796ff2SSara Sharon 				  mvmsta->vif->hw_queue[txq_curr_ac],
51401796ff2SSara Sharon 				  tid, 0);
51501796ff2SSara Sharon 	if (ret) {
51601796ff2SSara Sharon 		/* Re-mark the inactive queue as inactive */
51701796ff2SSara Sharon 		spin_lock_bh(&mvm->queue_info_lock);
51801796ff2SSara Sharon 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_INACTIVE;
51901796ff2SSara Sharon 		spin_unlock_bh(&mvm->queue_info_lock);
52001796ff2SSara Sharon 		IWL_ERR(mvm,
52101796ff2SSara Sharon 			"Failed to free inactive queue %d (ret=%d)\n",
52201796ff2SSara Sharon 			queue, ret);
52301796ff2SSara Sharon 
52401796ff2SSara Sharon 		return ret;
52501796ff2SSara Sharon 	}
52601796ff2SSara Sharon 
52701796ff2SSara Sharon 	/* If TXQ is allocated to another STA, update removal in FW */
52801796ff2SSara Sharon 	if (!same_sta)
52901796ff2SSara Sharon 		iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
53001796ff2SSara Sharon 
53101796ff2SSara Sharon 	return 0;
53201796ff2SSara Sharon }
53301796ff2SSara Sharon 
53442db09c1SLiad Kaufman static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
53542db09c1SLiad Kaufman 				    unsigned long tfd_queue_mask, u8 ac)
53642db09c1SLiad Kaufman {
53742db09c1SLiad Kaufman 	int queue = 0;
53842db09c1SLiad Kaufman 	u8 ac_to_queue[IEEE80211_NUM_ACS];
53942db09c1SLiad Kaufman 	int i;
54042db09c1SLiad Kaufman 
54142db09c1SLiad Kaufman 	lockdep_assert_held(&mvm->queue_info_lock);
542bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
543bb49701bSSara Sharon 		return -EINVAL;
54442db09c1SLiad Kaufman 
54542db09c1SLiad Kaufman 	memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
54642db09c1SLiad Kaufman 
54742db09c1SLiad Kaufman 	/* See what ACs the existing queues for this STA have */
54842db09c1SLiad Kaufman 	for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
54942db09c1SLiad Kaufman 		/* Only DATA queues can be shared */
55042db09c1SLiad Kaufman 		if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
55142db09c1SLiad Kaufman 		    i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
55242db09c1SLiad Kaufman 			continue;
55342db09c1SLiad Kaufman 
5549f9af3d7SLiad Kaufman 		/* Don't try and take queues being reconfigured */
5559f9af3d7SLiad Kaufman 		if (mvm->queue_info[queue].status ==
5569f9af3d7SLiad Kaufman 		    IWL_MVM_QUEUE_RECONFIGURING)
5579f9af3d7SLiad Kaufman 			continue;
5589f9af3d7SLiad Kaufman 
55942db09c1SLiad Kaufman 		ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
56042db09c1SLiad Kaufman 	}
56142db09c1SLiad Kaufman 
56242db09c1SLiad Kaufman 	/*
56342db09c1SLiad Kaufman 	 * The queue to share is chosen only from DATA queues as follows (in
56442db09c1SLiad Kaufman 	 * descending priority):
56542db09c1SLiad Kaufman 	 * 1. An AC_BE queue
56642db09c1SLiad Kaufman 	 * 2. Same AC queue
56742db09c1SLiad Kaufman 	 * 3. Highest AC queue that is lower than new AC
56842db09c1SLiad Kaufman 	 * 4. Any existing AC (there always is at least 1 DATA queue)
56942db09c1SLiad Kaufman 	 */
57042db09c1SLiad Kaufman 
57142db09c1SLiad Kaufman 	/* Priority 1: An AC_BE queue */
57242db09c1SLiad Kaufman 	if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
57342db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_BE];
57442db09c1SLiad Kaufman 	/* Priority 2: Same AC queue */
57542db09c1SLiad Kaufman 	else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
57642db09c1SLiad Kaufman 		queue = ac_to_queue[ac];
57742db09c1SLiad Kaufman 	/* Priority 3a: If new AC is VO and VI exists - use VI */
57842db09c1SLiad Kaufman 	else if (ac == IEEE80211_AC_VO &&
57942db09c1SLiad Kaufman 		 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
58042db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VI];
58142db09c1SLiad Kaufman 	/* Priority 3b: No BE so only AC less than the new one is BK */
58242db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
58342db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_BK];
58442db09c1SLiad Kaufman 	/* Priority 4a: No BE nor BK - use VI if exists */
58542db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
58642db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VI];
58742db09c1SLiad Kaufman 	/* Priority 4b: No BE, BK nor VI - use VO if exists */
58842db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
58942db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VO];
59042db09c1SLiad Kaufman 
59142db09c1SLiad Kaufman 	/* Make sure queue found (or not) is legal */
5929f9af3d7SLiad Kaufman 	if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
5939f9af3d7SLiad Kaufman 	    !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
5949f9af3d7SLiad Kaufman 	    (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
59542db09c1SLiad Kaufman 		IWL_ERR(mvm, "No DATA queues available to share\n");
5969f9af3d7SLiad Kaufman 		return -ENOSPC;
5979f9af3d7SLiad Kaufman 	}
5989f9af3d7SLiad Kaufman 
5999f9af3d7SLiad Kaufman 	/* Make sure the queue isn't in the middle of being reconfigured */
6009f9af3d7SLiad Kaufman 	if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_RECONFIGURING) {
6019f9af3d7SLiad Kaufman 		IWL_ERR(mvm,
6029f9af3d7SLiad Kaufman 			"TXQ %d is in the middle of re-config - try again\n",
6039f9af3d7SLiad Kaufman 			queue);
6049f9af3d7SLiad Kaufman 		return -EBUSY;
60542db09c1SLiad Kaufman 	}
60642db09c1SLiad Kaufman 
60742db09c1SLiad Kaufman 	return queue;
60842db09c1SLiad Kaufman }
60942db09c1SLiad Kaufman 
61058f2cc57SLiad Kaufman /*
6119f9af3d7SLiad Kaufman  * If a given queue has a higher AC than the TID stream that is being compared
6129f9af3d7SLiad Kaufman  * to, the queue needs to be redirected to the lower AC. This function does that
61358f2cc57SLiad Kaufman  * in such a case, otherwise - if no redirection required - it does nothing,
61458f2cc57SLiad Kaufman  * unless the %force param is true.
61558f2cc57SLiad Kaufman  */
6169f9af3d7SLiad Kaufman int iwl_mvm_scd_queue_redirect(struct iwl_mvm *mvm, int queue, int tid,
61758f2cc57SLiad Kaufman 			       int ac, int ssn, unsigned int wdg_timeout,
61858f2cc57SLiad Kaufman 			       bool force)
61958f2cc57SLiad Kaufman {
62058f2cc57SLiad Kaufman 	struct iwl_scd_txq_cfg_cmd cmd = {
62158f2cc57SLiad Kaufman 		.scd_queue = queue,
622f7c692deSLiad Kaufman 		.action = SCD_CFG_DISABLE_QUEUE,
62358f2cc57SLiad Kaufman 	};
62458f2cc57SLiad Kaufman 	bool shared_queue;
62558f2cc57SLiad Kaufman 	unsigned long mq;
62658f2cc57SLiad Kaufman 	int ret;
62758f2cc57SLiad Kaufman 
628bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
629bb49701bSSara Sharon 		return -EINVAL;
630bb49701bSSara Sharon 
63158f2cc57SLiad Kaufman 	/*
63258f2cc57SLiad Kaufman 	 * If the AC is lower than current one - FIFO needs to be redirected to
63358f2cc57SLiad Kaufman 	 * the lowest one of the streams in the queue. Check if this is needed
63458f2cc57SLiad Kaufman 	 * here.
63558f2cc57SLiad Kaufman 	 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
63658f2cc57SLiad Kaufman 	 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
63758f2cc57SLiad Kaufman 	 * we need to check if the numerical value of X is LARGER than of Y.
63858f2cc57SLiad Kaufman 	 */
63958f2cc57SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
64058f2cc57SLiad Kaufman 	if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
64158f2cc57SLiad Kaufman 		spin_unlock_bh(&mvm->queue_info_lock);
64258f2cc57SLiad Kaufman 
64358f2cc57SLiad Kaufman 		IWL_DEBUG_TX_QUEUES(mvm,
64458f2cc57SLiad Kaufman 				    "No redirection needed on TXQ #%d\n",
64558f2cc57SLiad Kaufman 				    queue);
64658f2cc57SLiad Kaufman 		return 0;
64758f2cc57SLiad Kaufman 	}
64858f2cc57SLiad Kaufman 
64958f2cc57SLiad Kaufman 	cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
65058f2cc57SLiad Kaufman 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
651edbe961cSLiad Kaufman 	cmd.tid = mvm->queue_info[queue].txq_tid;
65234e10860SSara Sharon 	mq = mvm->hw_queue_to_mac80211[queue];
65358f2cc57SLiad Kaufman 	shared_queue = (mvm->queue_info[queue].hw_queue_refcount > 1);
65458f2cc57SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
65558f2cc57SLiad Kaufman 
6569f9af3d7SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
65758f2cc57SLiad Kaufman 			    queue, iwl_mvm_ac_to_tx_fifo[ac]);
65858f2cc57SLiad Kaufman 
65958f2cc57SLiad Kaufman 	/* Stop MAC queues and wait for this queue to empty */
66058f2cc57SLiad Kaufman 	iwl_mvm_stop_mac_queues(mvm, mq);
661a1a57877SSara Sharon 	ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue));
66258f2cc57SLiad Kaufman 	if (ret) {
66358f2cc57SLiad Kaufman 		IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
66458f2cc57SLiad Kaufman 			queue);
66558f2cc57SLiad Kaufman 		ret = -EIO;
66658f2cc57SLiad Kaufman 		goto out;
66758f2cc57SLiad Kaufman 	}
66858f2cc57SLiad Kaufman 
66958f2cc57SLiad Kaufman 	/* Before redirecting the queue we need to de-activate it */
67058f2cc57SLiad Kaufman 	iwl_trans_txq_disable(mvm->trans, queue, false);
67158f2cc57SLiad Kaufman 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
67258f2cc57SLiad Kaufman 	if (ret)
67358f2cc57SLiad Kaufman 		IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
67458f2cc57SLiad Kaufman 			ret);
67558f2cc57SLiad Kaufman 
67658f2cc57SLiad Kaufman 	/* Make sure the SCD wrptr is correctly set before reconfiguring */
677ca3b9c6bSSara Sharon 	iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
67858f2cc57SLiad Kaufman 
679edbe961cSLiad Kaufman 	/* Update the TID "owner" of the queue */
680edbe961cSLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
681edbe961cSLiad Kaufman 	mvm->queue_info[queue].txq_tid = tid;
682edbe961cSLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
683edbe961cSLiad Kaufman 
68458f2cc57SLiad Kaufman 	/* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
68558f2cc57SLiad Kaufman 
68658f2cc57SLiad Kaufman 	/* Redirect to lower AC */
68758f2cc57SLiad Kaufman 	iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
68858f2cc57SLiad Kaufman 			     cmd.sta_id, tid, LINK_QUAL_AGG_FRAME_LIMIT_DEF,
68958f2cc57SLiad Kaufman 			     ssn);
69058f2cc57SLiad Kaufman 
69158f2cc57SLiad Kaufman 	/* Update AC marking of the queue */
69258f2cc57SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
69358f2cc57SLiad Kaufman 	mvm->queue_info[queue].mac80211_ac = ac;
69458f2cc57SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
69558f2cc57SLiad Kaufman 
69658f2cc57SLiad Kaufman 	/*
69758f2cc57SLiad Kaufman 	 * Mark queue as shared in transport if shared
69858f2cc57SLiad Kaufman 	 * Note this has to be done after queue enablement because enablement
69958f2cc57SLiad Kaufman 	 * can also set this value, and there is no indication there to shared
70058f2cc57SLiad Kaufman 	 * queues
70158f2cc57SLiad Kaufman 	 */
70258f2cc57SLiad Kaufman 	if (shared_queue)
70358f2cc57SLiad Kaufman 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
70458f2cc57SLiad Kaufman 
70558f2cc57SLiad Kaufman out:
70658f2cc57SLiad Kaufman 	/* Continue using the MAC queues */
70758f2cc57SLiad Kaufman 	iwl_mvm_start_mac_queues(mvm, mq);
70858f2cc57SLiad Kaufman 
70958f2cc57SLiad Kaufman 	return ret;
71058f2cc57SLiad Kaufman }
71158f2cc57SLiad Kaufman 
712310181ecSSara Sharon static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
713310181ecSSara Sharon 					struct ieee80211_sta *sta, u8 ac,
714310181ecSSara Sharon 					int tid)
715310181ecSSara Sharon {
716310181ecSSara Sharon 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
717310181ecSSara Sharon 	unsigned int wdg_timeout =
718310181ecSSara Sharon 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
719310181ecSSara Sharon 	u8 mac_queue = mvmsta->vif->hw_queue[ac];
720310181ecSSara Sharon 	int queue = -1;
721310181ecSSara Sharon 
722310181ecSSara Sharon 	lockdep_assert_held(&mvm->mutex);
723310181ecSSara Sharon 
724310181ecSSara Sharon 	IWL_DEBUG_TX_QUEUES(mvm,
725310181ecSSara Sharon 			    "Allocating queue for sta %d on tid %d\n",
726310181ecSSara Sharon 			    mvmsta->sta_id, tid);
727310181ecSSara Sharon 	queue = iwl_mvm_tvqm_enable_txq(mvm, mac_queue, mvmsta->sta_id, tid,
728310181ecSSara Sharon 					wdg_timeout);
729310181ecSSara Sharon 	if (queue < 0)
730310181ecSSara Sharon 		return queue;
731310181ecSSara Sharon 
732310181ecSSara Sharon 	IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
733310181ecSSara Sharon 
734310181ecSSara Sharon 	spin_lock_bh(&mvmsta->lock);
735310181ecSSara Sharon 	mvmsta->tid_data[tid].txq_id = queue;
736310181ecSSara Sharon 	mvmsta->tid_data[tid].is_tid_active = true;
737310181ecSSara Sharon 	mvmsta->tfd_queue_msk |= BIT(queue);
738310181ecSSara Sharon 	spin_unlock_bh(&mvmsta->lock);
739310181ecSSara Sharon 
740310181ecSSara Sharon 	return 0;
741310181ecSSara Sharon }
742310181ecSSara Sharon 
74324afba76SLiad Kaufman static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
74424afba76SLiad Kaufman 				   struct ieee80211_sta *sta, u8 ac, int tid,
74524afba76SLiad Kaufman 				   struct ieee80211_hdr *hdr)
74624afba76SLiad Kaufman {
74724afba76SLiad Kaufman 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
74824afba76SLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
74924afba76SLiad Kaufman 		.fifo = iwl_mvm_ac_to_tx_fifo[ac],
75024afba76SLiad Kaufman 		.sta_id = mvmsta->sta_id,
75124afba76SLiad Kaufman 		.tid = tid,
75224afba76SLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
75324afba76SLiad Kaufman 	};
75424afba76SLiad Kaufman 	unsigned int wdg_timeout =
75524afba76SLiad Kaufman 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
75624afba76SLiad Kaufman 	u8 mac_queue = mvmsta->vif->hw_queue[ac];
75724afba76SLiad Kaufman 	int queue = -1;
75801796ff2SSara Sharon 	bool using_inactive_queue = false, same_sta = false;
7599794c64fSLiad Kaufman 	unsigned long disable_agg_tids = 0;
7609794c64fSLiad Kaufman 	enum iwl_mvm_agg_state queue_state;
76142db09c1SLiad Kaufman 	bool shared_queue = false;
76224afba76SLiad Kaufman 	int ssn;
76342db09c1SLiad Kaufman 	unsigned long tfd_queue_mask;
764cf961e16SLiad Kaufman 	int ret;
76524afba76SLiad Kaufman 
76624afba76SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
76724afba76SLiad Kaufman 
768310181ecSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm))
769310181ecSSara Sharon 		return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
770310181ecSSara Sharon 
77142db09c1SLiad Kaufman 	spin_lock_bh(&mvmsta->lock);
77242db09c1SLiad Kaufman 	tfd_queue_mask = mvmsta->tfd_queue_msk;
77342db09c1SLiad Kaufman 	spin_unlock_bh(&mvmsta->lock);
77442db09c1SLiad Kaufman 
775d2515a99SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
77624afba76SLiad Kaufman 
77724afba76SLiad Kaufman 	/*
77824afba76SLiad Kaufman 	 * Non-QoS, QoS NDP and MGMT frames should go to a MGMT queue, if one
77924afba76SLiad Kaufman 	 * exists
78024afba76SLiad Kaufman 	 */
78124afba76SLiad Kaufman 	if (!ieee80211_is_data_qos(hdr->frame_control) ||
78224afba76SLiad Kaufman 	    ieee80211_is_qos_nullfunc(hdr->frame_control)) {
7839794c64fSLiad Kaufman 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
7849794c64fSLiad Kaufman 						IWL_MVM_DQA_MIN_MGMT_QUEUE,
78524afba76SLiad Kaufman 						IWL_MVM_DQA_MAX_MGMT_QUEUE);
78624afba76SLiad Kaufman 		if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
78724afba76SLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
78824afba76SLiad Kaufman 					    queue);
78924afba76SLiad Kaufman 
79024afba76SLiad Kaufman 		/* If no such queue is found, we'll use a DATA queue instead */
79124afba76SLiad Kaufman 	}
79224afba76SLiad Kaufman 
7939794c64fSLiad Kaufman 	if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
7949794c64fSLiad Kaufman 	    (mvm->queue_info[mvmsta->reserved_queue].status ==
7959794c64fSLiad Kaufman 	     IWL_MVM_QUEUE_RESERVED ||
7969794c64fSLiad Kaufman 	     mvm->queue_info[mvmsta->reserved_queue].status ==
7979794c64fSLiad Kaufman 	     IWL_MVM_QUEUE_INACTIVE)) {
79824afba76SLiad Kaufman 		queue = mvmsta->reserved_queue;
7999794c64fSLiad Kaufman 		mvm->queue_info[queue].reserved = true;
80024afba76SLiad Kaufman 		IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
80124afba76SLiad Kaufman 	}
80224afba76SLiad Kaufman 
80324afba76SLiad Kaufman 	if (queue < 0)
8049794c64fSLiad Kaufman 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
8059794c64fSLiad Kaufman 						IWL_MVM_DQA_MIN_DATA_QUEUE,
80624afba76SLiad Kaufman 						IWL_MVM_DQA_MAX_DATA_QUEUE);
807cf961e16SLiad Kaufman 
808cf961e16SLiad Kaufman 	/*
8099794c64fSLiad Kaufman 	 * Check if this queue is already allocated but inactive.
8109794c64fSLiad Kaufman 	 * In such a case, we'll need to first free this queue before enabling
8119794c64fSLiad Kaufman 	 * it again, so we'll mark it as reserved to make sure no new traffic
8129794c64fSLiad Kaufman 	 * arrives on it
8139794c64fSLiad Kaufman 	 */
8149794c64fSLiad Kaufman 	if (queue > 0 &&
8159794c64fSLiad Kaufman 	    mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
8169794c64fSLiad Kaufman 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
8179794c64fSLiad Kaufman 		using_inactive_queue = true;
81801796ff2SSara Sharon 		same_sta = mvm->queue_info[queue].ra_sta_id == mvmsta->sta_id;
8199794c64fSLiad Kaufman 		IWL_DEBUG_TX_QUEUES(mvm,
8209794c64fSLiad Kaufman 				    "Re-assigning TXQ %d: sta_id=%d, tid=%d\n",
8219794c64fSLiad Kaufman 				    queue, mvmsta->sta_id, tid);
8229794c64fSLiad Kaufman 	}
8239794c64fSLiad Kaufman 
82442db09c1SLiad Kaufman 	/* No free queue - we'll have to share */
82542db09c1SLiad Kaufman 	if (queue <= 0) {
82642db09c1SLiad Kaufman 		queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
82742db09c1SLiad Kaufman 		if (queue > 0) {
82842db09c1SLiad Kaufman 			shared_queue = true;
82942db09c1SLiad Kaufman 			mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
83042db09c1SLiad Kaufman 		}
83142db09c1SLiad Kaufman 	}
83242db09c1SLiad Kaufman 
8339794c64fSLiad Kaufman 	/*
834cf961e16SLiad Kaufman 	 * Mark TXQ as ready, even though it hasn't been fully configured yet,
835cf961e16SLiad Kaufman 	 * to make sure no one else takes it.
836cf961e16SLiad Kaufman 	 * This will allow avoiding re-acquiring the lock at the end of the
837cf961e16SLiad Kaufman 	 * configuration. On error we'll mark it back as free.
838cf961e16SLiad Kaufman 	 */
83942db09c1SLiad Kaufman 	if ((queue > 0) && !shared_queue)
840cf961e16SLiad Kaufman 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
84124afba76SLiad Kaufman 
842d2515a99SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
84324afba76SLiad Kaufman 
84442db09c1SLiad Kaufman 	/* This shouldn't happen - out of queues */
84542db09c1SLiad Kaufman 	if (WARN_ON(queue <= 0)) {
84642db09c1SLiad Kaufman 		IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
84742db09c1SLiad Kaufman 			tid, cfg.sta_id);
8489f9af3d7SLiad Kaufman 		return queue;
84942db09c1SLiad Kaufman 	}
85024afba76SLiad Kaufman 
85124afba76SLiad Kaufman 	/*
85224afba76SLiad Kaufman 	 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
85324afba76SLiad Kaufman 	 * but for configuring the SCD to send A-MPDUs we need to mark the queue
85424afba76SLiad Kaufman 	 * as aggregatable.
85524afba76SLiad Kaufman 	 * Mark all DATA queues as allowing to be aggregated at some point
85624afba76SLiad Kaufman 	 */
857d5216a28SLiad Kaufman 	cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
858d5216a28SLiad Kaufman 			 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
85924afba76SLiad Kaufman 
8609794c64fSLiad Kaufman 	/*
8619794c64fSLiad Kaufman 	 * If this queue was previously inactive (idle) - we need to free it
8629794c64fSLiad Kaufman 	 * first
8639794c64fSLiad Kaufman 	 */
8649794c64fSLiad Kaufman 	if (using_inactive_queue) {
86501796ff2SSara Sharon 		ret = iwl_mvm_free_inactive_queue(mvm, queue, same_sta);
86601796ff2SSara Sharon 		if (ret)
8679794c64fSLiad Kaufman 			return ret;
8689794c64fSLiad Kaufman 	}
8698d98ae6eSLiad Kaufman 
87042db09c1SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm,
87142db09c1SLiad Kaufman 			    "Allocating %squeue #%d to sta %d on tid %d\n",
87242db09c1SLiad Kaufman 			    shared_queue ? "shared " : "", queue,
87342db09c1SLiad Kaufman 			    mvmsta->sta_id, tid);
87442db09c1SLiad Kaufman 
87542db09c1SLiad Kaufman 	if (shared_queue) {
87642db09c1SLiad Kaufman 		/* Disable any open aggs on this queue */
87742db09c1SLiad Kaufman 		disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
87842db09c1SLiad Kaufman 
87942db09c1SLiad Kaufman 		if (disable_agg_tids) {
88042db09c1SLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
88142db09c1SLiad Kaufman 					    queue);
88242db09c1SLiad Kaufman 			iwl_mvm_invalidate_sta_queue(mvm, queue,
88342db09c1SLiad Kaufman 						     disable_agg_tids, false);
88442db09c1SLiad Kaufman 		}
88542db09c1SLiad Kaufman 	}
88624afba76SLiad Kaufman 
88724afba76SLiad Kaufman 	ssn = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
88824afba76SLiad Kaufman 	iwl_mvm_enable_txq(mvm, queue, mac_queue, ssn, &cfg,
88924afba76SLiad Kaufman 			   wdg_timeout);
89024afba76SLiad Kaufman 
89158f2cc57SLiad Kaufman 	/*
89258f2cc57SLiad Kaufman 	 * Mark queue as shared in transport if shared
89358f2cc57SLiad Kaufman 	 * Note this has to be done after queue enablement because enablement
89458f2cc57SLiad Kaufman 	 * can also set this value, and there is no indication there to shared
89558f2cc57SLiad Kaufman 	 * queues
89658f2cc57SLiad Kaufman 	 */
89758f2cc57SLiad Kaufman 	if (shared_queue)
89858f2cc57SLiad Kaufman 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
89958f2cc57SLiad Kaufman 
90024afba76SLiad Kaufman 	spin_lock_bh(&mvmsta->lock);
90124afba76SLiad Kaufman 	mvmsta->tid_data[tid].txq_id = queue;
9029794c64fSLiad Kaufman 	mvmsta->tid_data[tid].is_tid_active = true;
90324afba76SLiad Kaufman 	mvmsta->tfd_queue_msk |= BIT(queue);
9049794c64fSLiad Kaufman 	queue_state = mvmsta->tid_data[tid].state;
90524afba76SLiad Kaufman 
90624afba76SLiad Kaufman 	if (mvmsta->reserved_queue == queue)
90724afba76SLiad Kaufman 		mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
90824afba76SLiad Kaufman 	spin_unlock_bh(&mvmsta->lock);
90924afba76SLiad Kaufman 
91042db09c1SLiad Kaufman 	if (!shared_queue) {
911cf961e16SLiad Kaufman 		ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
912cf961e16SLiad Kaufman 		if (ret)
913cf961e16SLiad Kaufman 			goto out_err;
914cf961e16SLiad Kaufman 
9159794c64fSLiad Kaufman 		/* If we need to re-enable aggregations... */
91642db09c1SLiad Kaufman 		if (queue_state == IWL_AGG_ON) {
9179794c64fSLiad Kaufman 			ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
91842db09c1SLiad Kaufman 			if (ret)
91942db09c1SLiad Kaufman 				goto out_err;
92042db09c1SLiad Kaufman 		}
92158f2cc57SLiad Kaufman 	} else {
92258f2cc57SLiad Kaufman 		/* Redirect queue, if needed */
92358f2cc57SLiad Kaufman 		ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid, ac, ssn,
92458f2cc57SLiad Kaufman 						 wdg_timeout, false);
92558f2cc57SLiad Kaufman 		if (ret)
92658f2cc57SLiad Kaufman 			goto out_err;
92742db09c1SLiad Kaufman 	}
9289794c64fSLiad Kaufman 
92942db09c1SLiad Kaufman 	return 0;
930cf961e16SLiad Kaufman 
931cf961e16SLiad Kaufman out_err:
932cf961e16SLiad Kaufman 	iwl_mvm_disable_txq(mvm, queue, mac_queue, tid, 0);
933cf961e16SLiad Kaufman 
934cf961e16SLiad Kaufman 	return ret;
93524afba76SLiad Kaufman }
93624afba76SLiad Kaufman 
93719aefa45SLiad Kaufman static void iwl_mvm_change_queue_owner(struct iwl_mvm *mvm, int queue)
93819aefa45SLiad Kaufman {
93919aefa45SLiad Kaufman 	struct iwl_scd_txq_cfg_cmd cmd = {
94019aefa45SLiad Kaufman 		.scd_queue = queue,
94119aefa45SLiad Kaufman 		.action = SCD_CFG_UPDATE_QUEUE_TID,
94219aefa45SLiad Kaufman 	};
94319aefa45SLiad Kaufman 	int tid;
94419aefa45SLiad Kaufman 	unsigned long tid_bitmap;
94519aefa45SLiad Kaufman 	int ret;
94619aefa45SLiad Kaufman 
94719aefa45SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
94819aefa45SLiad Kaufman 
949bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
950bb49701bSSara Sharon 		return;
951bb49701bSSara Sharon 
95219aefa45SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
95319aefa45SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
95419aefa45SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
95519aefa45SLiad Kaufman 
95619aefa45SLiad Kaufman 	if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
95719aefa45SLiad Kaufman 		return;
95819aefa45SLiad Kaufman 
95919aefa45SLiad Kaufman 	/* Find any TID for queue */
96019aefa45SLiad Kaufman 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
96119aefa45SLiad Kaufman 	cmd.tid = tid;
96219aefa45SLiad Kaufman 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
96319aefa45SLiad Kaufman 
96419aefa45SLiad Kaufman 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
965341ca402SLiad Kaufman 	if (ret) {
96619aefa45SLiad Kaufman 		IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
96719aefa45SLiad Kaufman 			queue, ret);
968341ca402SLiad Kaufman 		return;
969341ca402SLiad Kaufman 	}
970341ca402SLiad Kaufman 
971341ca402SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
972341ca402SLiad Kaufman 	mvm->queue_info[queue].txq_tid = tid;
973341ca402SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
97419aefa45SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
97519aefa45SLiad Kaufman 			    queue, tid);
97619aefa45SLiad Kaufman }
97719aefa45SLiad Kaufman 
9789f9af3d7SLiad Kaufman static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
9799f9af3d7SLiad Kaufman {
9809f9af3d7SLiad Kaufman 	struct ieee80211_sta *sta;
9819f9af3d7SLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
9829f9af3d7SLiad Kaufman 	s8 sta_id;
9839f9af3d7SLiad Kaufman 	int tid = -1;
9849f9af3d7SLiad Kaufman 	unsigned long tid_bitmap;
9859f9af3d7SLiad Kaufman 	unsigned int wdg_timeout;
9869f9af3d7SLiad Kaufman 	int ssn;
9879f9af3d7SLiad Kaufman 	int ret = true;
9889f9af3d7SLiad Kaufman 
989bb49701bSSara Sharon 	/* queue sharing is disabled on new TX path */
990bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
991bb49701bSSara Sharon 		return;
992bb49701bSSara Sharon 
9939f9af3d7SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
9949f9af3d7SLiad Kaufman 
9959f9af3d7SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
9969f9af3d7SLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
9979f9af3d7SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
9989f9af3d7SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
9999f9af3d7SLiad Kaufman 
10009f9af3d7SLiad Kaufman 	/* Find TID for queue, and make sure it is the only one on the queue */
10019f9af3d7SLiad Kaufman 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
10029f9af3d7SLiad Kaufman 	if (tid_bitmap != BIT(tid)) {
10039f9af3d7SLiad Kaufman 		IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
10049f9af3d7SLiad Kaufman 			queue, tid_bitmap);
10059f9af3d7SLiad Kaufman 		return;
10069f9af3d7SLiad Kaufman 	}
10079f9af3d7SLiad Kaufman 
10089f9af3d7SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
10099f9af3d7SLiad Kaufman 			    tid);
10109f9af3d7SLiad Kaufman 
10119f9af3d7SLiad Kaufman 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
10129f9af3d7SLiad Kaufman 					lockdep_is_held(&mvm->mutex));
10139f9af3d7SLiad Kaufman 
10149f9af3d7SLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
10159f9af3d7SLiad Kaufman 		return;
10169f9af3d7SLiad Kaufman 
10179f9af3d7SLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
10189f9af3d7SLiad Kaufman 	wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
10199f9af3d7SLiad Kaufman 
10209f9af3d7SLiad Kaufman 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
10219f9af3d7SLiad Kaufman 
10229f9af3d7SLiad Kaufman 	ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid,
10239f9af3d7SLiad Kaufman 					 tid_to_mac80211_ac[tid], ssn,
10249f9af3d7SLiad Kaufman 					 wdg_timeout, true);
10259f9af3d7SLiad Kaufman 	if (ret) {
10269f9af3d7SLiad Kaufman 		IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
10279f9af3d7SLiad Kaufman 		return;
10289f9af3d7SLiad Kaufman 	}
10299f9af3d7SLiad Kaufman 
10309f9af3d7SLiad Kaufman 	/* If aggs should be turned back on - do it */
10319f9af3d7SLiad Kaufman 	if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
10329cd70e80SEmmanuel Grumbach 		struct iwl_mvm_add_sta_cmd cmd = {0};
10339f9af3d7SLiad Kaufman 
10349f9af3d7SLiad Kaufman 		mvmsta->tid_disable_agg &= ~BIT(tid);
10359f9af3d7SLiad Kaufman 
10369f9af3d7SLiad Kaufman 		cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
10379f9af3d7SLiad Kaufman 		cmd.sta_id = mvmsta->sta_id;
10389f9af3d7SLiad Kaufman 		cmd.add_modify = STA_MODE_MODIFY;
10399f9af3d7SLiad Kaufman 		cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
10409f9af3d7SLiad Kaufman 		cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
10419f9af3d7SLiad Kaufman 		cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
10429f9af3d7SLiad Kaufman 
10439f9af3d7SLiad Kaufman 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
10449f9af3d7SLiad Kaufman 					   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
10459f9af3d7SLiad Kaufman 		if (!ret) {
10469f9af3d7SLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm,
10479f9af3d7SLiad Kaufman 					    "TXQ #%d is now aggregated again\n",
10489f9af3d7SLiad Kaufman 					    queue);
10499f9af3d7SLiad Kaufman 
10509f9af3d7SLiad Kaufman 			/* Mark queue intenally as aggregating again */
10519f9af3d7SLiad Kaufman 			iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
10529f9af3d7SLiad Kaufman 		}
10539f9af3d7SLiad Kaufman 	}
10549f9af3d7SLiad Kaufman 
10559f9af3d7SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
10569f9af3d7SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
10579f9af3d7SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
10589f9af3d7SLiad Kaufman }
10599f9af3d7SLiad Kaufman 
106024afba76SLiad Kaufman static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
106124afba76SLiad Kaufman {
106224afba76SLiad Kaufman 	if (tid == IWL_MAX_TID_COUNT)
106324afba76SLiad Kaufman 		return IEEE80211_AC_VO; /* MGMT */
106424afba76SLiad Kaufman 
106524afba76SLiad Kaufman 	return tid_to_mac80211_ac[tid];
106624afba76SLiad Kaufman }
106724afba76SLiad Kaufman 
106824afba76SLiad Kaufman static void iwl_mvm_tx_deferred_stream(struct iwl_mvm *mvm,
106924afba76SLiad Kaufman 				       struct ieee80211_sta *sta, int tid)
107024afba76SLiad Kaufman {
107124afba76SLiad Kaufman 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
107224afba76SLiad Kaufman 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
107324afba76SLiad Kaufman 	struct sk_buff *skb;
107424afba76SLiad Kaufman 	struct ieee80211_hdr *hdr;
107524afba76SLiad Kaufman 	struct sk_buff_head deferred_tx;
107624afba76SLiad Kaufman 	u8 mac_queue;
107724afba76SLiad Kaufman 	bool no_queue = false; /* Marks if there is a problem with the queue */
107824afba76SLiad Kaufman 	u8 ac;
107924afba76SLiad Kaufman 
108024afba76SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
108124afba76SLiad Kaufman 
108224afba76SLiad Kaufman 	skb = skb_peek(&tid_data->deferred_tx_frames);
108324afba76SLiad Kaufman 	if (!skb)
108424afba76SLiad Kaufman 		return;
108524afba76SLiad Kaufman 	hdr = (void *)skb->data;
108624afba76SLiad Kaufman 
108724afba76SLiad Kaufman 	ac = iwl_mvm_tid_to_ac_queue(tid);
108824afba76SLiad Kaufman 	mac_queue = IEEE80211_SKB_CB(skb)->hw_queue;
108924afba76SLiad Kaufman 
10906862fceeSSara Sharon 	if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE &&
109124afba76SLiad Kaufman 	    iwl_mvm_sta_alloc_queue(mvm, sta, ac, tid, hdr)) {
109224afba76SLiad Kaufman 		IWL_ERR(mvm,
109324afba76SLiad Kaufman 			"Can't alloc TXQ for sta %d tid %d - dropping frame\n",
109424afba76SLiad Kaufman 			mvmsta->sta_id, tid);
109524afba76SLiad Kaufman 
109624afba76SLiad Kaufman 		/*
109724afba76SLiad Kaufman 		 * Mark queue as problematic so later the deferred traffic is
109824afba76SLiad Kaufman 		 * freed, as we can do nothing with it
109924afba76SLiad Kaufman 		 */
110024afba76SLiad Kaufman 		no_queue = true;
110124afba76SLiad Kaufman 	}
110224afba76SLiad Kaufman 
110324afba76SLiad Kaufman 	__skb_queue_head_init(&deferred_tx);
110424afba76SLiad Kaufman 
1105d2515a99SLiad Kaufman 	/* Disable bottom-halves when entering TX path */
1106d2515a99SLiad Kaufman 	local_bh_disable();
110724afba76SLiad Kaufman 	spin_lock(&mvmsta->lock);
110824afba76SLiad Kaufman 	skb_queue_splice_init(&tid_data->deferred_tx_frames, &deferred_tx);
1109ad5de737SLiad Kaufman 	mvmsta->deferred_traffic_tid_map &= ~BIT(tid);
111024afba76SLiad Kaufman 	spin_unlock(&mvmsta->lock);
111124afba76SLiad Kaufman 
111224afba76SLiad Kaufman 	while ((skb = __skb_dequeue(&deferred_tx)))
111324afba76SLiad Kaufman 		if (no_queue || iwl_mvm_tx_skb(mvm, skb, sta))
111424afba76SLiad Kaufman 			ieee80211_free_txskb(mvm->hw, skb);
111524afba76SLiad Kaufman 	local_bh_enable();
111624afba76SLiad Kaufman 
111724afba76SLiad Kaufman 	/* Wake queue */
111824afba76SLiad Kaufman 	iwl_mvm_start_mac_queues(mvm, BIT(mac_queue));
111924afba76SLiad Kaufman }
112024afba76SLiad Kaufman 
112124afba76SLiad Kaufman void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
112224afba76SLiad Kaufman {
112324afba76SLiad Kaufman 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
112424afba76SLiad Kaufman 					   add_stream_wk);
112524afba76SLiad Kaufman 	struct ieee80211_sta *sta;
112624afba76SLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
112724afba76SLiad Kaufman 	unsigned long deferred_tid_traffic;
11289f9af3d7SLiad Kaufman 	int queue, sta_id, tid;
112924afba76SLiad Kaufman 
11309794c64fSLiad Kaufman 	/* Check inactivity of queues */
11319794c64fSLiad Kaufman 	iwl_mvm_inactivity_check(mvm);
11329794c64fSLiad Kaufman 
113324afba76SLiad Kaufman 	mutex_lock(&mvm->mutex);
113424afba76SLiad Kaufman 
113534e10860SSara Sharon 	/* No queue reconfiguration in TVQM mode */
113634e10860SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm))
113734e10860SSara Sharon 		goto alloc_queues;
113834e10860SSara Sharon 
11399f9af3d7SLiad Kaufman 	/* Reconfigure queues requiring reconfiguation */
114034e10860SSara Sharon 	for (queue = 0; queue < ARRAY_SIZE(mvm->queue_info); queue++) {
11419f9af3d7SLiad Kaufman 		bool reconfig;
114219aefa45SLiad Kaufman 		bool change_owner;
11439f9af3d7SLiad Kaufman 
11449f9af3d7SLiad Kaufman 		spin_lock_bh(&mvm->queue_info_lock);
11459f9af3d7SLiad Kaufman 		reconfig = (mvm->queue_info[queue].status ==
11469f9af3d7SLiad Kaufman 			    IWL_MVM_QUEUE_RECONFIGURING);
114719aefa45SLiad Kaufman 
114819aefa45SLiad Kaufman 		/*
114919aefa45SLiad Kaufman 		 * We need to take into account a situation in which a TXQ was
115019aefa45SLiad Kaufman 		 * allocated to TID x, and then turned shared by adding TIDs y
115119aefa45SLiad Kaufman 		 * and z. If TID x becomes inactive and is removed from the TXQ,
115219aefa45SLiad Kaufman 		 * ownership must be given to one of the remaining TIDs.
115319aefa45SLiad Kaufman 		 * This is mainly because if TID x continues - a new queue can't
115419aefa45SLiad Kaufman 		 * be allocated for it as long as it is an owner of another TXQ.
115519aefa45SLiad Kaufman 		 */
115619aefa45SLiad Kaufman 		change_owner = !(mvm->queue_info[queue].tid_bitmap &
115719aefa45SLiad Kaufman 				 BIT(mvm->queue_info[queue].txq_tid)) &&
115819aefa45SLiad Kaufman 			       (mvm->queue_info[queue].status ==
115919aefa45SLiad Kaufman 				IWL_MVM_QUEUE_SHARED);
11609f9af3d7SLiad Kaufman 		spin_unlock_bh(&mvm->queue_info_lock);
11619f9af3d7SLiad Kaufman 
11629f9af3d7SLiad Kaufman 		if (reconfig)
11639f9af3d7SLiad Kaufman 			iwl_mvm_unshare_queue(mvm, queue);
116419aefa45SLiad Kaufman 		else if (change_owner)
116519aefa45SLiad Kaufman 			iwl_mvm_change_queue_owner(mvm, queue);
11669f9af3d7SLiad Kaufman 	}
11679f9af3d7SLiad Kaufman 
116834e10860SSara Sharon alloc_queues:
116924afba76SLiad Kaufman 	/* Go over all stations with deferred traffic */
117024afba76SLiad Kaufman 	for_each_set_bit(sta_id, mvm->sta_deferred_frames,
117124afba76SLiad Kaufman 			 IWL_MVM_STATION_COUNT) {
117224afba76SLiad Kaufman 		clear_bit(sta_id, mvm->sta_deferred_frames);
117324afba76SLiad Kaufman 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
117424afba76SLiad Kaufman 						lockdep_is_held(&mvm->mutex));
117524afba76SLiad Kaufman 		if (IS_ERR_OR_NULL(sta))
117624afba76SLiad Kaufman 			continue;
117724afba76SLiad Kaufman 
117824afba76SLiad Kaufman 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
117924afba76SLiad Kaufman 		deferred_tid_traffic = mvmsta->deferred_traffic_tid_map;
118024afba76SLiad Kaufman 
118124afba76SLiad Kaufman 		for_each_set_bit(tid, &deferred_tid_traffic,
118224afba76SLiad Kaufman 				 IWL_MAX_TID_COUNT + 1)
118324afba76SLiad Kaufman 			iwl_mvm_tx_deferred_stream(mvm, sta, tid);
118424afba76SLiad Kaufman 	}
118524afba76SLiad Kaufman 
118624afba76SLiad Kaufman 	mutex_unlock(&mvm->mutex);
118724afba76SLiad Kaufman }
118824afba76SLiad Kaufman 
118924afba76SLiad Kaufman static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1190d5216a28SLiad Kaufman 				      struct ieee80211_sta *sta,
1191d5216a28SLiad Kaufman 				      enum nl80211_iftype vif_type)
119224afba76SLiad Kaufman {
119324afba76SLiad Kaufman 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
119424afba76SLiad Kaufman 	int queue;
119501796ff2SSara Sharon 	bool using_inactive_queue = false, same_sta = false;
119624afba76SLiad Kaufman 
1197396952eeSSara Sharon 	/* queue reserving is disabled on new TX path */
1198396952eeSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1199396952eeSSara Sharon 		return 0;
1200396952eeSSara Sharon 
12019794c64fSLiad Kaufman 	/*
12029794c64fSLiad Kaufman 	 * Check for inactive queues, so we don't reach a situation where we
12039794c64fSLiad Kaufman 	 * can't add a STA due to a shortage in queues that doesn't really exist
12049794c64fSLiad Kaufman 	 */
12059794c64fSLiad Kaufman 	iwl_mvm_inactivity_check(mvm);
12069794c64fSLiad Kaufman 
120724afba76SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
120824afba76SLiad Kaufman 
120924afba76SLiad Kaufman 	/* Make sure we have free resources for this STA */
1210d5216a28SLiad Kaufman 	if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1211d5216a28SLiad Kaufman 	    !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].hw_queue_refcount &&
1212cf961e16SLiad Kaufman 	    (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1213cf961e16SLiad Kaufman 	     IWL_MVM_QUEUE_FREE))
1214d5216a28SLiad Kaufman 		queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1215d5216a28SLiad Kaufman 	else
12169794c64fSLiad Kaufman 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
12179794c64fSLiad Kaufman 						IWL_MVM_DQA_MIN_DATA_QUEUE,
121824afba76SLiad Kaufman 						IWL_MVM_DQA_MAX_DATA_QUEUE);
121924afba76SLiad Kaufman 	if (queue < 0) {
122024afba76SLiad Kaufman 		spin_unlock_bh(&mvm->queue_info_lock);
122124afba76SLiad Kaufman 		IWL_ERR(mvm, "No available queues for new station\n");
122224afba76SLiad Kaufman 		return -ENOSPC;
122301796ff2SSara Sharon 	} else if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
122401796ff2SSara Sharon 		/*
122501796ff2SSara Sharon 		 * If this queue is already allocated but inactive we'll need to
122601796ff2SSara Sharon 		 * first free this queue before enabling it again, we'll mark
122701796ff2SSara Sharon 		 * it as reserved to make sure no new traffic arrives on it
122801796ff2SSara Sharon 		 */
122901796ff2SSara Sharon 		using_inactive_queue = true;
123001796ff2SSara Sharon 		same_sta = mvm->queue_info[queue].ra_sta_id == mvmsta->sta_id;
123124afba76SLiad Kaufman 	}
1232cf961e16SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
123324afba76SLiad Kaufman 
123424afba76SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
123524afba76SLiad Kaufman 
123624afba76SLiad Kaufman 	mvmsta->reserved_queue = queue;
123724afba76SLiad Kaufman 
123801796ff2SSara Sharon 	if (using_inactive_queue)
123901796ff2SSara Sharon 		iwl_mvm_free_inactive_queue(mvm, queue, same_sta);
124001796ff2SSara Sharon 
124124afba76SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
124224afba76SLiad Kaufman 			    queue, mvmsta->sta_id);
124324afba76SLiad Kaufman 
124424afba76SLiad Kaufman 	return 0;
124524afba76SLiad Kaufman }
124624afba76SLiad Kaufman 
12478d98ae6eSLiad Kaufman /*
12488d98ae6eSLiad Kaufman  * In DQA mode, after a HW restart the queues should be allocated as before, in
12498d98ae6eSLiad Kaufman  * order to avoid race conditions when there are shared queues. This function
12508d98ae6eSLiad Kaufman  * does the re-mapping and queue allocation.
12518d98ae6eSLiad Kaufman  *
12528d98ae6eSLiad Kaufman  * Note that re-enabling aggregations isn't done in this function.
12538d98ae6eSLiad Kaufman  */
12548d98ae6eSLiad Kaufman static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
12558d98ae6eSLiad Kaufman 						 struct iwl_mvm_sta *mvm_sta)
12568d98ae6eSLiad Kaufman {
12578d98ae6eSLiad Kaufman 	unsigned int wdg_timeout =
12588d98ae6eSLiad Kaufman 			iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
12598d98ae6eSLiad Kaufman 	int i;
12608d98ae6eSLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
12618d98ae6eSLiad Kaufman 		.sta_id = mvm_sta->sta_id,
12628d98ae6eSLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
12638d98ae6eSLiad Kaufman 	};
12648d98ae6eSLiad Kaufman 
126503c902bfSJohannes Berg 	/* Make sure reserved queue is still marked as such (if allocated) */
126603c902bfSJohannes Berg 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
12678d98ae6eSLiad Kaufman 		mvm->queue_info[mvm_sta->reserved_queue].status =
12688d98ae6eSLiad Kaufman 			IWL_MVM_QUEUE_RESERVED;
12698d98ae6eSLiad Kaufman 
12708d98ae6eSLiad Kaufman 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
12718d98ae6eSLiad Kaufman 		struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
12728d98ae6eSLiad Kaufman 		int txq_id = tid_data->txq_id;
12738d98ae6eSLiad Kaufman 		int ac;
12748d98ae6eSLiad Kaufman 		u8 mac_queue;
12758d98ae6eSLiad Kaufman 
12766862fceeSSara Sharon 		if (txq_id == IWL_MVM_INVALID_QUEUE)
12778d98ae6eSLiad Kaufman 			continue;
12788d98ae6eSLiad Kaufman 
12798d98ae6eSLiad Kaufman 		skb_queue_head_init(&tid_data->deferred_tx_frames);
12808d98ae6eSLiad Kaufman 
12818d98ae6eSLiad Kaufman 		ac = tid_to_mac80211_ac[i];
12828d98ae6eSLiad Kaufman 		mac_queue = mvm_sta->vif->hw_queue[ac];
12838d98ae6eSLiad Kaufman 
1284310181ecSSara Sharon 		if (iwl_mvm_has_new_tx_api(mvm)) {
1285310181ecSSara Sharon 			IWL_DEBUG_TX_QUEUES(mvm,
1286310181ecSSara Sharon 					    "Re-mapping sta %d tid %d\n",
1287310181ecSSara Sharon 					    mvm_sta->sta_id, i);
1288310181ecSSara Sharon 			txq_id = iwl_mvm_tvqm_enable_txq(mvm, mac_queue,
1289310181ecSSara Sharon 							 mvm_sta->sta_id,
1290310181ecSSara Sharon 							 i, wdg_timeout);
1291310181ecSSara Sharon 			tid_data->txq_id = txq_id;
1292310181ecSSara Sharon 		} else {
1293310181ecSSara Sharon 			u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1294310181ecSSara Sharon 
12958d98ae6eSLiad Kaufman 			cfg.tid = i;
12968d98ae6eSLiad Kaufman 			cfg.fifo = iwl_mvm_ac_to_tx_fifo[ac];
12978d98ae6eSLiad Kaufman 			cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1298310181ecSSara Sharon 					 txq_id ==
1299310181ecSSara Sharon 					 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
13008d98ae6eSLiad Kaufman 
13018d98ae6eSLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm,
13028d98ae6eSLiad Kaufman 					    "Re-mapping sta %d tid %d to queue %d\n",
13038d98ae6eSLiad Kaufman 					    mvm_sta->sta_id, i, txq_id);
13048d98ae6eSLiad Kaufman 
1305310181ecSSara Sharon 			iwl_mvm_enable_txq(mvm, txq_id, mac_queue, seq, &cfg,
1306310181ecSSara Sharon 					   wdg_timeout);
13078d98ae6eSLiad Kaufman 			mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
13088d98ae6eSLiad Kaufman 		}
130934e10860SSara Sharon 	}
13108d98ae6eSLiad Kaufman 
13118d98ae6eSLiad Kaufman 	atomic_set(&mvm->pending_frames[mvm_sta->sta_id], 0);
13128d98ae6eSLiad Kaufman }
13138d98ae6eSLiad Kaufman 
1314e705c121SKalle Valo int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1315e705c121SKalle Valo 		    struct ieee80211_vif *vif,
1316e705c121SKalle Valo 		    struct ieee80211_sta *sta)
1317e705c121SKalle Valo {
1318e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1319e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1320a571f5f6SSara Sharon 	struct iwl_mvm_rxq_dup_data *dup_data;
1321e705c121SKalle Valo 	int i, ret, sta_id;
1322e705c121SKalle Valo 
1323e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1324e705c121SKalle Valo 
1325e705c121SKalle Valo 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1326e705c121SKalle Valo 		sta_id = iwl_mvm_find_free_sta_id(mvm,
1327e705c121SKalle Valo 						  ieee80211_vif_type_p2p(vif));
1328e705c121SKalle Valo 	else
1329e705c121SKalle Valo 		sta_id = mvm_sta->sta_id;
1330e705c121SKalle Valo 
13310ae98812SSara Sharon 	if (sta_id == IWL_MVM_INVALID_STA)
1332e705c121SKalle Valo 		return -ENOSPC;
1333e705c121SKalle Valo 
1334e705c121SKalle Valo 	spin_lock_init(&mvm_sta->lock);
1335e705c121SKalle Valo 
13368d98ae6eSLiad Kaufman 	/* In DQA mode, if this is a HW restart, re-alloc existing queues */
13378d98ae6eSLiad Kaufman 	if (iwl_mvm_is_dqa_supported(mvm) &&
13388d98ae6eSLiad Kaufman 	    test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
13398d98ae6eSLiad Kaufman 		iwl_mvm_realloc_queues_after_restart(mvm, mvm_sta);
13408d98ae6eSLiad Kaufman 		goto update_fw;
13418d98ae6eSLiad Kaufman 	}
13428d98ae6eSLiad Kaufman 
1343e705c121SKalle Valo 	mvm_sta->sta_id = sta_id;
1344e705c121SKalle Valo 	mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1345e705c121SKalle Valo 						      mvmvif->color);
1346e705c121SKalle Valo 	mvm_sta->vif = vif;
1347e705c121SKalle Valo 	mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1348e705c121SKalle Valo 	mvm_sta->tx_protection = 0;
1349e705c121SKalle Valo 	mvm_sta->tt_tx_protection = false;
1350ced19f26SSara Sharon 	mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
1351e705c121SKalle Valo 
1352e705c121SKalle Valo 	/* HW restart, don't assume the memory has been zeroed */
1353e705c121SKalle Valo 	atomic_set(&mvm->pending_frames[sta_id], 0);
1354e705c121SKalle Valo 	mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1355e705c121SKalle Valo 	mvm_sta->tfd_queue_msk = 0;
1356e705c121SKalle Valo 
1357e3118ad7SLiad Kaufman 	/*
1358e3118ad7SLiad Kaufman 	 * Allocate new queues for a TDLS station, unless we're in DQA mode,
1359e3118ad7SLiad Kaufman 	 * and then they'll be allocated dynamically
1360e3118ad7SLiad Kaufman 	 */
1361e3118ad7SLiad Kaufman 	if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls) {
1362e705c121SKalle Valo 		ret = iwl_mvm_tdls_sta_init(mvm, sta);
1363e705c121SKalle Valo 		if (ret)
1364e705c121SKalle Valo 			return ret;
136524afba76SLiad Kaufman 	} else if (!iwl_mvm_is_dqa_supported(mvm)) {
1366e705c121SKalle Valo 		for (i = 0; i < IEEE80211_NUM_ACS; i++)
1367e705c121SKalle Valo 			if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
1368e705c121SKalle Valo 				mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
1369e705c121SKalle Valo 	}
1370e705c121SKalle Valo 
1371e705c121SKalle Valo 	/* for HW restart - reset everything but the sequence number */
137224afba76SLiad Kaufman 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1373e705c121SKalle Valo 		u16 seq = mvm_sta->tid_data[i].seq_number;
1374e705c121SKalle Valo 		memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1375e705c121SKalle Valo 		mvm_sta->tid_data[i].seq_number = seq;
137624afba76SLiad Kaufman 
137724afba76SLiad Kaufman 		if (!iwl_mvm_is_dqa_supported(mvm))
137824afba76SLiad Kaufman 			continue;
137924afba76SLiad Kaufman 
138024afba76SLiad Kaufman 		/*
138124afba76SLiad Kaufman 		 * Mark all queues for this STA as unallocated and defer TX
138224afba76SLiad Kaufman 		 * frames until the queue is allocated
138324afba76SLiad Kaufman 		 */
13846862fceeSSara Sharon 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
138524afba76SLiad Kaufman 		skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
1386e705c121SKalle Valo 	}
138724afba76SLiad Kaufman 	mvm_sta->deferred_traffic_tid_map = 0;
1388e705c121SKalle Valo 	mvm_sta->agg_tids = 0;
1389e705c121SKalle Valo 
1390a571f5f6SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) &&
1391a571f5f6SSara Sharon 	    !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1392a571f5f6SSara Sharon 		dup_data = kcalloc(mvm->trans->num_rx_queues,
1393a571f5f6SSara Sharon 				   sizeof(*dup_data),
1394a571f5f6SSara Sharon 				   GFP_KERNEL);
1395a571f5f6SSara Sharon 		if (!dup_data)
1396a571f5f6SSara Sharon 			return -ENOMEM;
1397a571f5f6SSara Sharon 		mvm_sta->dup_data = dup_data;
1398a571f5f6SSara Sharon 	}
1399a571f5f6SSara Sharon 
1400396952eeSSara Sharon 	if (iwl_mvm_is_dqa_supported(mvm) && !iwl_mvm_has_new_tx_api(mvm)) {
1401d5216a28SLiad Kaufman 		ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1402d5216a28SLiad Kaufman 						 ieee80211_vif_type_p2p(vif));
140324afba76SLiad Kaufman 		if (ret)
140424afba76SLiad Kaufman 			goto err;
140524afba76SLiad Kaufman 	}
140624afba76SLiad Kaufman 
14078d98ae6eSLiad Kaufman update_fw:
140824afba76SLiad Kaufman 	ret = iwl_mvm_sta_send_to_fw(mvm, sta, false, 0);
1409e705c121SKalle Valo 	if (ret)
1410e705c121SKalle Valo 		goto err;
1411e705c121SKalle Valo 
1412e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION) {
1413e705c121SKalle Valo 		if (!sta->tdls) {
14140ae98812SSara Sharon 			WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
1415e705c121SKalle Valo 			mvmvif->ap_sta_id = sta_id;
1416e705c121SKalle Valo 		} else {
14170ae98812SSara Sharon 			WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
1418e705c121SKalle Valo 		}
1419e705c121SKalle Valo 	}
1420e705c121SKalle Valo 
1421e705c121SKalle Valo 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1422e705c121SKalle Valo 
1423e705c121SKalle Valo 	return 0;
1424e705c121SKalle Valo 
1425e705c121SKalle Valo err:
1426e3118ad7SLiad Kaufman 	if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
1427e705c121SKalle Valo 		iwl_mvm_tdls_sta_deinit(mvm, sta);
1428e705c121SKalle Valo 	return ret;
1429e705c121SKalle Valo }
1430e705c121SKalle Valo 
1431e705c121SKalle Valo int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1432e705c121SKalle Valo 		      bool drain)
1433e705c121SKalle Valo {
1434e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
1435e705c121SKalle Valo 	int ret;
1436e705c121SKalle Valo 	u32 status;
1437e705c121SKalle Valo 
1438e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1439e705c121SKalle Valo 
1440e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1441e705c121SKalle Valo 	cmd.sta_id = mvmsta->sta_id;
1442e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
1443e705c121SKalle Valo 	cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1444e705c121SKalle Valo 	cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1445e705c121SKalle Valo 
1446e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
1447854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1448854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
1449e705c121SKalle Valo 					  &cmd, &status);
1450e705c121SKalle Valo 	if (ret)
1451e705c121SKalle Valo 		return ret;
1452e705c121SKalle Valo 
1453837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1454e705c121SKalle Valo 	case ADD_STA_SUCCESS:
1455e705c121SKalle Valo 		IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1456e705c121SKalle Valo 			       mvmsta->sta_id);
1457e705c121SKalle Valo 		break;
1458e705c121SKalle Valo 	default:
1459e705c121SKalle Valo 		ret = -EIO;
1460e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1461e705c121SKalle Valo 			mvmsta->sta_id);
1462e705c121SKalle Valo 		break;
1463e705c121SKalle Valo 	}
1464e705c121SKalle Valo 
1465e705c121SKalle Valo 	return ret;
1466e705c121SKalle Valo }
1467e705c121SKalle Valo 
1468e705c121SKalle Valo /*
1469e705c121SKalle Valo  * Remove a station from the FW table. Before sending the command to remove
1470e705c121SKalle Valo  * the station validate that the station is indeed known to the driver (sanity
1471e705c121SKalle Valo  * only).
1472e705c121SKalle Valo  */
1473e705c121SKalle Valo static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1474e705c121SKalle Valo {
1475e705c121SKalle Valo 	struct ieee80211_sta *sta;
1476e705c121SKalle Valo 	struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1477e705c121SKalle Valo 		.sta_id = sta_id,
1478e705c121SKalle Valo 	};
1479e705c121SKalle Valo 	int ret;
1480e705c121SKalle Valo 
1481e705c121SKalle Valo 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1482e705c121SKalle Valo 					lockdep_is_held(&mvm->mutex));
1483e705c121SKalle Valo 
1484e705c121SKalle Valo 	/* Note: internal stations are marked as error values */
1485e705c121SKalle Valo 	if (!sta) {
1486e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid station id\n");
1487e705c121SKalle Valo 		return -EINVAL;
1488e705c121SKalle Valo 	}
1489e705c121SKalle Valo 
1490e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1491e705c121SKalle Valo 				   sizeof(rm_sta_cmd), &rm_sta_cmd);
1492e705c121SKalle Valo 	if (ret) {
1493e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1494e705c121SKalle Valo 		return ret;
1495e705c121SKalle Valo 	}
1496e705c121SKalle Valo 
1497e705c121SKalle Valo 	return 0;
1498e705c121SKalle Valo }
1499e705c121SKalle Valo 
1500e705c121SKalle Valo void iwl_mvm_sta_drained_wk(struct work_struct *wk)
1501e705c121SKalle Valo {
1502e705c121SKalle Valo 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
1503e705c121SKalle Valo 	u8 sta_id;
1504e705c121SKalle Valo 
1505e705c121SKalle Valo 	/*
1506e705c121SKalle Valo 	 * The mutex is needed because of the SYNC cmd, but not only: if the
1507e705c121SKalle Valo 	 * work would run concurrently with iwl_mvm_rm_sta, it would run before
1508e705c121SKalle Valo 	 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
1509e705c121SKalle Valo 	 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
1510e705c121SKalle Valo 	 * that later.
1511e705c121SKalle Valo 	 */
1512e705c121SKalle Valo 	mutex_lock(&mvm->mutex);
1513e705c121SKalle Valo 
1514e705c121SKalle Valo 	for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
1515e705c121SKalle Valo 		int ret;
1516e705c121SKalle Valo 		struct ieee80211_sta *sta =
1517e705c121SKalle Valo 			rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1518e705c121SKalle Valo 						  lockdep_is_held(&mvm->mutex));
1519e705c121SKalle Valo 
1520e705c121SKalle Valo 		/*
1521e705c121SKalle Valo 		 * This station is in use or RCU-removed; the latter happens in
1522e705c121SKalle Valo 		 * managed mode, where mac80211 removes the station before we
1523e705c121SKalle Valo 		 * can remove it from firmware (we can only do that after the
1524e705c121SKalle Valo 		 * MAC is marked unassociated), and possibly while the deauth
1525e705c121SKalle Valo 		 * frame to disconnect from the AP is still queued. Then, the
1526e705c121SKalle Valo 		 * station pointer is -ENOENT when the last skb is reclaimed.
1527e705c121SKalle Valo 		 */
1528e705c121SKalle Valo 		if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
1529e705c121SKalle Valo 			continue;
1530e705c121SKalle Valo 
1531e705c121SKalle Valo 		if (PTR_ERR(sta) == -EINVAL) {
1532e705c121SKalle Valo 			IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
1533e705c121SKalle Valo 				sta_id);
1534e705c121SKalle Valo 			continue;
1535e705c121SKalle Valo 		}
1536e705c121SKalle Valo 
1537e705c121SKalle Valo 		if (!sta) {
1538e705c121SKalle Valo 			IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
1539e705c121SKalle Valo 				sta_id);
1540e705c121SKalle Valo 			continue;
1541e705c121SKalle Valo 		}
1542e705c121SKalle Valo 
1543e705c121SKalle Valo 		WARN_ON(PTR_ERR(sta) != -EBUSY);
1544e705c121SKalle Valo 		/* This station was removed and we waited until it got drained,
1545e705c121SKalle Valo 		 * we can now proceed and remove it.
1546e705c121SKalle Valo 		 */
1547e705c121SKalle Valo 		ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1548e705c121SKalle Valo 		if (ret) {
1549e705c121SKalle Valo 			IWL_ERR(mvm,
1550e705c121SKalle Valo 				"Couldn't remove sta %d after it was drained\n",
1551e705c121SKalle Valo 				sta_id);
1552e705c121SKalle Valo 			continue;
1553e705c121SKalle Valo 		}
1554e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1555e705c121SKalle Valo 		clear_bit(sta_id, mvm->sta_drained);
1556e705c121SKalle Valo 
1557e705c121SKalle Valo 		if (mvm->tfd_drained[sta_id]) {
1558e705c121SKalle Valo 			unsigned long i, msk = mvm->tfd_drained[sta_id];
1559e705c121SKalle Valo 
1560e705c121SKalle Valo 			for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
1561e705c121SKalle Valo 				iwl_mvm_disable_txq(mvm, i, i,
1562e705c121SKalle Valo 						    IWL_MAX_TID_COUNT, 0);
1563e705c121SKalle Valo 
1564e705c121SKalle Valo 			mvm->tfd_drained[sta_id] = 0;
1565e705c121SKalle Valo 			IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
1566e705c121SKalle Valo 				       sta_id, msk);
1567e705c121SKalle Valo 		}
1568e705c121SKalle Valo 	}
1569e705c121SKalle Valo 
1570e705c121SKalle Valo 	mutex_unlock(&mvm->mutex);
1571e705c121SKalle Valo }
1572e705c121SKalle Valo 
157324afba76SLiad Kaufman static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
157424afba76SLiad Kaufman 				       struct ieee80211_vif *vif,
157524afba76SLiad Kaufman 				       struct iwl_mvm_sta *mvm_sta)
157624afba76SLiad Kaufman {
157724afba76SLiad Kaufman 	int ac;
157824afba76SLiad Kaufman 	int i;
157924afba76SLiad Kaufman 
158024afba76SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
158124afba76SLiad Kaufman 
158224afba76SLiad Kaufman 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
15836862fceeSSara Sharon 		if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
158424afba76SLiad Kaufman 			continue;
158524afba76SLiad Kaufman 
158624afba76SLiad Kaufman 		ac = iwl_mvm_tid_to_ac_queue(i);
158724afba76SLiad Kaufman 		iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
158824afba76SLiad Kaufman 				    vif->hw_queue[ac], i, 0);
15896862fceeSSara Sharon 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
159024afba76SLiad Kaufman 	}
159124afba76SLiad Kaufman }
159224afba76SLiad Kaufman 
1593d6d517b7SSara Sharon int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1594d6d517b7SSara Sharon 				  struct iwl_mvm_sta *mvm_sta)
1595d6d517b7SSara Sharon {
1596d6d517b7SSara Sharon 	int i, ret;
1597d6d517b7SSara Sharon 
1598d6d517b7SSara Sharon 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1599d6d517b7SSara Sharon 		u16 txq_id;
1600d6d517b7SSara Sharon 
1601d6d517b7SSara Sharon 		spin_lock_bh(&mvm_sta->lock);
1602d6d517b7SSara Sharon 		txq_id = mvm_sta->tid_data[i].txq_id;
1603d6d517b7SSara Sharon 		spin_unlock_bh(&mvm_sta->lock);
1604d6d517b7SSara Sharon 
1605d6d517b7SSara Sharon 		if (txq_id == IWL_MVM_INVALID_QUEUE)
1606d6d517b7SSara Sharon 			continue;
1607d6d517b7SSara Sharon 
1608d6d517b7SSara Sharon 		ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1609d6d517b7SSara Sharon 		if (ret)
1610d6d517b7SSara Sharon 			break;
1611d6d517b7SSara Sharon 	}
1612d6d517b7SSara Sharon 
1613d6d517b7SSara Sharon 	return ret;
1614d6d517b7SSara Sharon }
1615d6d517b7SSara Sharon 
1616e705c121SKalle Valo int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1617e705c121SKalle Valo 		   struct ieee80211_vif *vif,
1618e705c121SKalle Valo 		   struct ieee80211_sta *sta)
1619e705c121SKalle Valo {
1620e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1621e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
162294c3e614SSara Sharon 	u8 sta_id = mvm_sta->sta_id;
1623e705c121SKalle Valo 	int ret;
1624e705c121SKalle Valo 
1625e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1626e705c121SKalle Valo 
1627a571f5f6SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
1628a571f5f6SSara Sharon 		kfree(mvm_sta->dup_data);
1629a571f5f6SSara Sharon 
1630a6f035a0SLiad Kaufman 	if ((vif->type == NL80211_IFTYPE_STATION &&
163194c3e614SSara Sharon 	     mvmvif->ap_sta_id == sta_id) ||
1632a6f035a0SLiad Kaufman 	    iwl_mvm_is_dqa_supported(mvm)){
1633e705c121SKalle Valo 		ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1634e705c121SKalle Valo 		if (ret)
1635e705c121SKalle Valo 			return ret;
1636e705c121SKalle Valo 		/* flush its queues here since we are freeing mvm_sta */
1637d49394a1SSara Sharon 		ret = iwl_mvm_flush_sta(mvm, mvm_sta, false, 0);
1638e705c121SKalle Valo 		if (ret)
1639e705c121SKalle Valo 			return ret;
1640d6d517b7SSara Sharon 		if (iwl_mvm_has_new_tx_api(mvm)) {
1641d6d517b7SSara Sharon 			ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1642d6d517b7SSara Sharon 		} else {
1643d6d517b7SSara Sharon 			u32 q_mask = mvm_sta->tfd_queue_msk;
1644d6d517b7SSara Sharon 
1645a1a57877SSara Sharon 			ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1646d6d517b7SSara Sharon 							     q_mask);
1647d6d517b7SSara Sharon 		}
1648e705c121SKalle Valo 		if (ret)
1649e705c121SKalle Valo 			return ret;
1650e705c121SKalle Valo 		ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1651e705c121SKalle Valo 
165224afba76SLiad Kaufman 		/* If DQA is supported - the queues can be disabled now */
165394c3e614SSara Sharon 		if (iwl_mvm_is_dqa_supported(mvm)) {
165456214749SLiad Kaufman 			iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
165594c3e614SSara Sharon 			/*
165694c3e614SSara Sharon 			 * If pending_frames is set at this point - it must be
165794c3e614SSara Sharon 			 * driver internal logic error, since queues are empty
165894c3e614SSara Sharon 			 * and removed successuly.
165994c3e614SSara Sharon 			 * warn on it but set it to 0 anyway to avoid station
166094c3e614SSara Sharon 			 * not being removed later in the function
166194c3e614SSara Sharon 			 */
166294c3e614SSara Sharon 			WARN_ON(atomic_xchg(&mvm->pending_frames[sta_id], 0));
166394c3e614SSara Sharon 		}
166456214749SLiad Kaufman 
166556214749SLiad Kaufman 		/* If there is a TXQ still marked as reserved - free it */
166656214749SLiad Kaufman 		if (iwl_mvm_is_dqa_supported(mvm) &&
166756214749SLiad Kaufman 		    mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1668a0315deaSLiad Kaufman 			u8 reserved_txq = mvm_sta->reserved_queue;
1669a0315deaSLiad Kaufman 			enum iwl_mvm_queue_status *status;
1670a0315deaSLiad Kaufman 
1671a0315deaSLiad Kaufman 			/*
1672a0315deaSLiad Kaufman 			 * If no traffic has gone through the reserved TXQ - it
1673a0315deaSLiad Kaufman 			 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1674a0315deaSLiad Kaufman 			 * should be manually marked as free again
1675a0315deaSLiad Kaufman 			 */
1676a0315deaSLiad Kaufman 			spin_lock_bh(&mvm->queue_info_lock);
1677a0315deaSLiad Kaufman 			status = &mvm->queue_info[reserved_txq].status;
1678a0315deaSLiad Kaufman 			if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1679a0315deaSLiad Kaufman 				 (*status != IWL_MVM_QUEUE_FREE),
1680a0315deaSLiad Kaufman 				 "sta_id %d reserved txq %d status %d",
168194c3e614SSara Sharon 				 sta_id, reserved_txq, *status)) {
1682a0315deaSLiad Kaufman 				spin_unlock_bh(&mvm->queue_info_lock);
1683a0315deaSLiad Kaufman 				return -EINVAL;
1684a0315deaSLiad Kaufman 			}
1685a0315deaSLiad Kaufman 
1686a0315deaSLiad Kaufman 			*status = IWL_MVM_QUEUE_FREE;
1687a0315deaSLiad Kaufman 			spin_unlock_bh(&mvm->queue_info_lock);
1688a0315deaSLiad Kaufman 		}
1689a0315deaSLiad Kaufman 
1690e3118ad7SLiad Kaufman 		if (vif->type == NL80211_IFTYPE_STATION &&
169194c3e614SSara Sharon 		    mvmvif->ap_sta_id == sta_id) {
1692e3118ad7SLiad Kaufman 			/* if associated - we can't remove the AP STA now */
1693e705c121SKalle Valo 			if (vif->bss_conf.assoc)
1694e705c121SKalle Valo 				return ret;
1695e705c121SKalle Valo 
1696e705c121SKalle Valo 			/* unassoc - go ahead - remove the AP STA now */
16970ae98812SSara Sharon 			mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1698e705c121SKalle Valo 
1699e705c121SKalle Valo 			/* clear d0i3_ap_sta_id if no longer relevant */
170094c3e614SSara Sharon 			if (mvm->d0i3_ap_sta_id == sta_id)
17010ae98812SSara Sharon 				mvm->d0i3_ap_sta_id = IWL_MVM_INVALID_STA;
1702e705c121SKalle Valo 		}
1703e3118ad7SLiad Kaufman 	}
1704e705c121SKalle Valo 
1705e705c121SKalle Valo 	/*
1706e705c121SKalle Valo 	 * This shouldn't happen - the TDLS channel switch should be canceled
1707e705c121SKalle Valo 	 * before the STA is removed.
1708e705c121SKalle Valo 	 */
170994c3e614SSara Sharon 	if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
17100ae98812SSara Sharon 		mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1711e705c121SKalle Valo 		cancel_delayed_work(&mvm->tdls_cs.dwork);
1712e705c121SKalle Valo 	}
1713e705c121SKalle Valo 
1714e705c121SKalle Valo 	/*
1715e705c121SKalle Valo 	 * Make sure that the tx response code sees the station as -EBUSY and
1716e705c121SKalle Valo 	 * calls the drain worker.
1717e705c121SKalle Valo 	 */
1718e705c121SKalle Valo 	spin_lock_bh(&mvm_sta->lock);
171994c3e614SSara Sharon 
1720e705c121SKalle Valo 	/*
1721e705c121SKalle Valo 	 * There are frames pending on the AC queues for this station.
1722e705c121SKalle Valo 	 * We need to wait until all the frames are drained...
1723e705c121SKalle Valo 	 */
172494c3e614SSara Sharon 	if (atomic_read(&mvm->pending_frames[sta_id])) {
172594c3e614SSara Sharon 		rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id],
1726e705c121SKalle Valo 				   ERR_PTR(-EBUSY));
1727e705c121SKalle Valo 		spin_unlock_bh(&mvm_sta->lock);
1728e705c121SKalle Valo 
1729e705c121SKalle Valo 		/* disable TDLS sta queues on drain complete */
1730e705c121SKalle Valo 		if (sta->tdls) {
173194c3e614SSara Sharon 			mvm->tfd_drained[sta_id] = mvm_sta->tfd_queue_msk;
173294c3e614SSara Sharon 			IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n", sta_id);
1733e705c121SKalle Valo 		}
1734e705c121SKalle Valo 
1735e705c121SKalle Valo 		ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1736e705c121SKalle Valo 	} else {
1737e705c121SKalle Valo 		spin_unlock_bh(&mvm_sta->lock);
1738e705c121SKalle Valo 
1739e3118ad7SLiad Kaufman 		if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
1740e705c121SKalle Valo 			iwl_mvm_tdls_sta_deinit(mvm, sta);
1741e705c121SKalle Valo 
1742e705c121SKalle Valo 		ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1743e705c121SKalle Valo 		RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1744e705c121SKalle Valo 	}
1745e705c121SKalle Valo 
1746e705c121SKalle Valo 	return ret;
1747e705c121SKalle Valo }
1748e705c121SKalle Valo 
1749e705c121SKalle Valo int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1750e705c121SKalle Valo 		      struct ieee80211_vif *vif,
1751e705c121SKalle Valo 		      u8 sta_id)
1752e705c121SKalle Valo {
1753e705c121SKalle Valo 	int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1754e705c121SKalle Valo 
1755e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1756e705c121SKalle Valo 
1757e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1758e705c121SKalle Valo 	return ret;
1759e705c121SKalle Valo }
1760e705c121SKalle Valo 
17610e39eb03SChaya Rachel Ivgi int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1762e705c121SKalle Valo 			     struct iwl_mvm_int_sta *sta,
1763ced19f26SSara Sharon 			     u32 qmask, enum nl80211_iftype iftype,
1764ced19f26SSara Sharon 			     enum iwl_sta_type type)
1765e705c121SKalle Valo {
1766e705c121SKalle Valo 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1767e705c121SKalle Valo 		sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
17680ae98812SSara Sharon 		if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
1769e705c121SKalle Valo 			return -ENOSPC;
1770e705c121SKalle Valo 	}
1771e705c121SKalle Valo 
1772e705c121SKalle Valo 	sta->tfd_queue_msk = qmask;
1773ced19f26SSara Sharon 	sta->type = type;
1774e705c121SKalle Valo 
1775e705c121SKalle Valo 	/* put a non-NULL value so iterating over the stations won't stop */
1776e705c121SKalle Valo 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1777e705c121SKalle Valo 	return 0;
1778e705c121SKalle Valo }
1779e705c121SKalle Valo 
178026d6c16bSSara Sharon void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
1781e705c121SKalle Valo {
1782e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
1783e705c121SKalle Valo 	memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
17840ae98812SSara Sharon 	sta->sta_id = IWL_MVM_INVALID_STA;
1785e705c121SKalle Valo }
1786e705c121SKalle Valo 
1787e705c121SKalle Valo static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1788e705c121SKalle Valo 				      struct iwl_mvm_int_sta *sta,
1789e705c121SKalle Valo 				      const u8 *addr,
1790e705c121SKalle Valo 				      u16 mac_id, u16 color)
1791e705c121SKalle Valo {
1792e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd;
1793e705c121SKalle Valo 	int ret;
1794e705c121SKalle Valo 	u32 status;
1795e705c121SKalle Valo 
1796e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1797e705c121SKalle Valo 
1798e705c121SKalle Valo 	memset(&cmd, 0, sizeof(cmd));
1799e705c121SKalle Valo 	cmd.sta_id = sta->sta_id;
1800e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1801e705c121SKalle Valo 							     color));
1802ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1803ced19f26SSara Sharon 		cmd.station_type = sta->type;
1804e705c121SKalle Valo 
1805bb49701bSSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm))
1806e705c121SKalle Valo 		cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1807cf0cda19SLiad Kaufman 	cmd.tid_disable_tx = cpu_to_le16(0xffff);
1808e705c121SKalle Valo 
1809e705c121SKalle Valo 	if (addr)
1810e705c121SKalle Valo 		memcpy(cmd.addr, addr, ETH_ALEN);
1811e705c121SKalle Valo 
1812854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1813854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
1814e705c121SKalle Valo 					  &cmd, &status);
1815e705c121SKalle Valo 	if (ret)
1816e705c121SKalle Valo 		return ret;
1817e705c121SKalle Valo 
1818837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1819e705c121SKalle Valo 	case ADD_STA_SUCCESS:
1820e705c121SKalle Valo 		IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1821e705c121SKalle Valo 		return 0;
1822e705c121SKalle Valo 	default:
1823e705c121SKalle Valo 		ret = -EIO;
1824e705c121SKalle Valo 		IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1825e705c121SKalle Valo 			status);
1826e705c121SKalle Valo 		break;
1827e705c121SKalle Valo 	}
1828e705c121SKalle Valo 	return ret;
1829e705c121SKalle Valo }
1830e705c121SKalle Valo 
1831c5a719eeSSara Sharon static void iwl_mvm_enable_aux_queue(struct iwl_mvm *mvm)
1832e705c121SKalle Valo {
1833e705c121SKalle Valo 	unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1834e705c121SKalle Valo 					mvm->cfg->base_params->wd_timeout :
1835e705c121SKalle Valo 					IWL_WATCHDOG_DISABLED;
1836e705c121SKalle Valo 
1837310181ecSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
1838310181ecSSara Sharon 		int queue = iwl_mvm_tvqm_enable_txq(mvm, mvm->aux_queue,
1839310181ecSSara Sharon 						    mvm->aux_sta.sta_id,
1840310181ecSSara Sharon 						    IWL_MAX_TID_COUNT,
1841310181ecSSara Sharon 						    wdg_timeout);
1842310181ecSSara Sharon 		mvm->aux_queue = queue;
1843310181ecSSara Sharon 	} else if (iwl_mvm_is_dqa_supported(mvm)) {
184428d0793eSLiad Kaufman 		struct iwl_trans_txq_scd_cfg cfg = {
184528d0793eSLiad Kaufman 			.fifo = IWL_MVM_TX_FIFO_MCAST,
184628d0793eSLiad Kaufman 			.sta_id = mvm->aux_sta.sta_id,
184728d0793eSLiad Kaufman 			.tid = IWL_MAX_TID_COUNT,
184828d0793eSLiad Kaufman 			.aggregate = false,
184928d0793eSLiad Kaufman 			.frame_limit = IWL_FRAME_LIMIT,
185028d0793eSLiad Kaufman 		};
185128d0793eSLiad Kaufman 
185228d0793eSLiad Kaufman 		iwl_mvm_enable_txq(mvm, mvm->aux_queue, mvm->aux_queue, 0, &cfg,
185328d0793eSLiad Kaufman 				   wdg_timeout);
1854c5a719eeSSara Sharon 	} else {
1855c5a719eeSSara Sharon 		iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
1856c5a719eeSSara Sharon 				      IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
185728d0793eSLiad Kaufman 	}
1858c5a719eeSSara Sharon }
1859c5a719eeSSara Sharon 
1860c5a719eeSSara Sharon int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1861c5a719eeSSara Sharon {
1862c5a719eeSSara Sharon 	int ret;
1863c5a719eeSSara Sharon 
1864c5a719eeSSara Sharon 	lockdep_assert_held(&mvm->mutex);
1865c5a719eeSSara Sharon 
1866c5a719eeSSara Sharon 	/* Allocate aux station and assign to it the aux queue */
1867c5a719eeSSara Sharon 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
1868ced19f26SSara Sharon 				       NL80211_IFTYPE_UNSPECIFIED,
1869ced19f26SSara Sharon 				       IWL_STA_AUX_ACTIVITY);
1870c5a719eeSSara Sharon 	if (ret)
1871c5a719eeSSara Sharon 		return ret;
1872c5a719eeSSara Sharon 
1873c5a719eeSSara Sharon 	/* Map Aux queue to fifo - needs to happen before adding Aux station */
1874c5a719eeSSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm))
1875c5a719eeSSara Sharon 		iwl_mvm_enable_aux_queue(mvm);
187628d0793eSLiad Kaufman 
1877e705c121SKalle Valo 	ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
1878e705c121SKalle Valo 					 MAC_INDEX_AUX, 0);
1879c5a719eeSSara Sharon 	if (ret) {
1880e705c121SKalle Valo 		iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1881e705c121SKalle Valo 		return ret;
1882e705c121SKalle Valo 	}
1883e705c121SKalle Valo 
1884c5a719eeSSara Sharon 	/*
1885c5a719eeSSara Sharon 	 * For a000 firmware and on we cannot add queue to a station unknown
1886c5a719eeSSara Sharon 	 * to firmware so enable queue here - after the station was added
1887c5a719eeSSara Sharon 	 */
1888c5a719eeSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm))
1889c5a719eeSSara Sharon 		iwl_mvm_enable_aux_queue(mvm);
1890c5a719eeSSara Sharon 
1891c5a719eeSSara Sharon 	return 0;
1892c5a719eeSSara Sharon }
1893c5a719eeSSara Sharon 
18940e39eb03SChaya Rachel Ivgi int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
18950e39eb03SChaya Rachel Ivgi {
18960e39eb03SChaya Rachel Ivgi 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
18970e39eb03SChaya Rachel Ivgi 
18980e39eb03SChaya Rachel Ivgi 	lockdep_assert_held(&mvm->mutex);
18990e39eb03SChaya Rachel Ivgi 	return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
19000e39eb03SChaya Rachel Ivgi 					 mvmvif->id, 0);
19010e39eb03SChaya Rachel Ivgi }
19020e39eb03SChaya Rachel Ivgi 
19030e39eb03SChaya Rachel Ivgi int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
19040e39eb03SChaya Rachel Ivgi {
19050e39eb03SChaya Rachel Ivgi 	int ret;
19060e39eb03SChaya Rachel Ivgi 
19070e39eb03SChaya Rachel Ivgi 	lockdep_assert_held(&mvm->mutex);
19080e39eb03SChaya Rachel Ivgi 
19090e39eb03SChaya Rachel Ivgi 	ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
19100e39eb03SChaya Rachel Ivgi 	if (ret)
19110e39eb03SChaya Rachel Ivgi 		IWL_WARN(mvm, "Failed sending remove station\n");
19120e39eb03SChaya Rachel Ivgi 
19130e39eb03SChaya Rachel Ivgi 	return ret;
19140e39eb03SChaya Rachel Ivgi }
19150e39eb03SChaya Rachel Ivgi 
19160e39eb03SChaya Rachel Ivgi void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
19170e39eb03SChaya Rachel Ivgi {
19180e39eb03SChaya Rachel Ivgi 	iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
19190e39eb03SChaya Rachel Ivgi }
19200e39eb03SChaya Rachel Ivgi 
1921e705c121SKalle Valo void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
1922e705c121SKalle Valo {
1923e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1924e705c121SKalle Valo 
1925e705c121SKalle Valo 	iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1926e705c121SKalle Valo }
1927e705c121SKalle Valo 
1928e705c121SKalle Valo /*
1929e705c121SKalle Valo  * Send the add station command for the vif's broadcast station.
1930e705c121SKalle Valo  * Assumes that the station was already allocated.
1931e705c121SKalle Valo  *
1932e705c121SKalle Valo  * @mvm: the mvm component
1933e705c121SKalle Valo  * @vif: the interface to which the broadcast station is added
1934e705c121SKalle Valo  * @bsta: the broadcast station to add.
1935e705c121SKalle Valo  */
1936e705c121SKalle Valo int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1937e705c121SKalle Valo {
1938e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1939e705c121SKalle Valo 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
1940e705c121SKalle Valo 	static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1941e705c121SKalle Valo 	const u8 *baddr = _baddr;
19427daa7624SJohannes Berg 	int queue;
1943df88c08dSLiad Kaufman 	int ret;
1944c5a719eeSSara Sharon 	unsigned int wdg_timeout =
1945c5a719eeSSara Sharon 		iwl_mvm_get_wd_timeout(mvm, vif, false, false);
1946de24f638SLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
1947de24f638SLiad Kaufman 		.fifo = IWL_MVM_TX_FIFO_VO,
1948de24f638SLiad Kaufman 		.sta_id = mvmvif->bcast_sta.sta_id,
1949de24f638SLiad Kaufman 		.tid = IWL_MAX_TID_COUNT,
1950de24f638SLiad Kaufman 		.aggregate = false,
1951de24f638SLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
1952de24f638SLiad Kaufman 	};
1953de24f638SLiad Kaufman 
1954c5a719eeSSara Sharon 	lockdep_assert_held(&mvm->mutex);
1955c5a719eeSSara Sharon 
1956310181ecSSara Sharon 	if (iwl_mvm_is_dqa_supported(mvm) && !iwl_mvm_has_new_tx_api(mvm)) {
19574d339989SLiad Kaufman 		if (vif->type == NL80211_IFTYPE_AP ||
19584d339989SLiad Kaufman 		    vif->type == NL80211_IFTYPE_ADHOC)
195949f71713SSara Sharon 			queue = mvm->probe_queue;
1960df88c08dSLiad Kaufman 		else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
196149f71713SSara Sharon 			queue = mvm->p2p_dev_queue;
1962df88c08dSLiad Kaufman 		else if (WARN(1, "Missing required TXQ for adding bcast STA\n"))
1963de24f638SLiad Kaufman 			return -EINVAL;
1964de24f638SLiad Kaufman 
1965df88c08dSLiad Kaufman 		bsta->tfd_queue_msk |= BIT(queue);
1966c5a719eeSSara Sharon 
1967c5a719eeSSara Sharon 		iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0,
1968c5a719eeSSara Sharon 				   &cfg, wdg_timeout);
1969de24f638SLiad Kaufman 	}
1970de24f638SLiad Kaufman 
1971e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_ADHOC)
1972e705c121SKalle Valo 		baddr = vif->bss_conf.bssid;
1973e705c121SKalle Valo 
19740ae98812SSara Sharon 	if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
1975e705c121SKalle Valo 		return -ENOSPC;
1976e705c121SKalle Valo 
1977df88c08dSLiad Kaufman 	ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
1978e705c121SKalle Valo 					 mvmvif->id, mvmvif->color);
1979df88c08dSLiad Kaufman 	if (ret)
1980df88c08dSLiad Kaufman 		return ret;
1981df88c08dSLiad Kaufman 
1982df88c08dSLiad Kaufman 	/*
1983c5a719eeSSara Sharon 	 * For a000 firmware and on we cannot add queue to a station unknown
1984c5a719eeSSara Sharon 	 * to firmware so enable queue here - after the station was added
1985df88c08dSLiad Kaufman 	 */
1986310181ecSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
19877daa7624SJohannes Berg 		queue = iwl_mvm_tvqm_enable_txq(mvm, vif->hw_queue[0],
1988310181ecSSara Sharon 						bsta->sta_id,
1989310181ecSSara Sharon 						IWL_MAX_TID_COUNT,
1990c5a719eeSSara Sharon 						wdg_timeout);
19917daa7624SJohannes Berg 
1992310181ecSSara Sharon 		if (vif->type == NL80211_IFTYPE_AP)
1993310181ecSSara Sharon 			mvm->probe_queue = queue;
1994310181ecSSara Sharon 		else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
1995310181ecSSara Sharon 			mvm->p2p_dev_queue = queue;
1996310181ecSSara Sharon 
1997310181ecSSara Sharon 		bsta->tfd_queue_msk |= BIT(queue);
1998310181ecSSara Sharon 	}
1999df88c08dSLiad Kaufman 
2000df88c08dSLiad Kaufman 	return 0;
2001df88c08dSLiad Kaufman }
2002df88c08dSLiad Kaufman 
2003df88c08dSLiad Kaufman static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2004df88c08dSLiad Kaufman 					  struct ieee80211_vif *vif)
2005df88c08dSLiad Kaufman {
2006df88c08dSLiad Kaufman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2007df88c08dSLiad Kaufman 
2008df88c08dSLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
2009df88c08dSLiad Kaufman 
2010d49394a1SSara Sharon 	iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true, 0);
2011d49394a1SSara Sharon 
20124d339989SLiad Kaufman 	if (vif->type == NL80211_IFTYPE_AP ||
20134d339989SLiad Kaufman 	    vif->type == NL80211_IFTYPE_ADHOC)
2014df88c08dSLiad Kaufman 		iwl_mvm_disable_txq(mvm, vif->cab_queue, vif->cab_queue,
2015df88c08dSLiad Kaufman 				    IWL_MAX_TID_COUNT, 0);
2016df88c08dSLiad Kaufman 
201749f71713SSara Sharon 	if (mvmvif->bcast_sta.tfd_queue_msk & BIT(mvm->probe_queue)) {
201849f71713SSara Sharon 		iwl_mvm_disable_txq(mvm, mvm->probe_queue,
2019df88c08dSLiad Kaufman 				    vif->hw_queue[0], IWL_MAX_TID_COUNT,
2020df88c08dSLiad Kaufman 				    0);
202149f71713SSara Sharon 		mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(mvm->probe_queue);
2022df88c08dSLiad Kaufman 	}
2023df88c08dSLiad Kaufman 
202449f71713SSara Sharon 	if (mvmvif->bcast_sta.tfd_queue_msk & BIT(mvm->p2p_dev_queue)) {
202549f71713SSara Sharon 		iwl_mvm_disable_txq(mvm, mvm->p2p_dev_queue,
2026df88c08dSLiad Kaufman 				    vif->hw_queue[0], IWL_MAX_TID_COUNT,
2027df88c08dSLiad Kaufman 				    0);
202849f71713SSara Sharon 		mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(mvm->p2p_dev_queue);
2029df88c08dSLiad Kaufman 	}
2030e705c121SKalle Valo }
2031e705c121SKalle Valo 
2032e705c121SKalle Valo /* Send the FW a request to remove the station from it's internal data
2033e705c121SKalle Valo  * structures, but DO NOT remove the entry from the local data structures. */
2034e705c121SKalle Valo int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2035e705c121SKalle Valo {
2036e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2037e705c121SKalle Valo 	int ret;
2038e705c121SKalle Valo 
2039e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2040e705c121SKalle Valo 
2041df88c08dSLiad Kaufman 	if (iwl_mvm_is_dqa_supported(mvm))
2042df88c08dSLiad Kaufman 		iwl_mvm_free_bcast_sta_queues(mvm, vif);
2043df88c08dSLiad Kaufman 
2044e705c121SKalle Valo 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
2045e705c121SKalle Valo 	if (ret)
2046e705c121SKalle Valo 		IWL_WARN(mvm, "Failed sending remove station\n");
2047e705c121SKalle Valo 	return ret;
2048e705c121SKalle Valo }
2049e705c121SKalle Valo 
2050e705c121SKalle Valo int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2051e705c121SKalle Valo {
2052e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2053de24f638SLiad Kaufman 	u32 qmask = 0;
2054e705c121SKalle Valo 
2055e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2056e705c121SKalle Valo 
2057df88c08dSLiad Kaufman 	if (!iwl_mvm_is_dqa_supported(mvm)) {
2058e705c121SKalle Valo 		qmask = iwl_mvm_mac_get_queues_mask(vif);
2059e705c121SKalle Valo 
2060e705c121SKalle Valo 		/*
2061e705c121SKalle Valo 		 * The firmware defines the TFD queue mask to only be relevant
2062e705c121SKalle Valo 		 * for *unicast* queues, so the multicast (CAB) queue shouldn't
2063df88c08dSLiad Kaufman 		 * be included. This only happens in NL80211_IFTYPE_AP vif type,
2064df88c08dSLiad Kaufman 		 * so the next line will only have an effect there.
2065e705c121SKalle Valo 		 */
2066e705c121SKalle Valo 		qmask &= ~BIT(vif->cab_queue);
2067de24f638SLiad Kaufman 	}
2068de24f638SLiad Kaufman 
2069e705c121SKalle Valo 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
2070ced19f26SSara Sharon 					ieee80211_vif_type_p2p(vif),
2071ced19f26SSara Sharon 					IWL_STA_GENERAL_PURPOSE);
2072e705c121SKalle Valo }
2073e705c121SKalle Valo 
2074e705c121SKalle Valo /* Allocate a new station entry for the broadcast station to the given vif,
2075e705c121SKalle Valo  * and send it to the FW.
2076e705c121SKalle Valo  * Note that each P2P mac should have its own broadcast station.
2077e705c121SKalle Valo  *
2078e705c121SKalle Valo  * @mvm: the mvm component
2079e705c121SKalle Valo  * @vif: the interface to which the broadcast station is added
2080e705c121SKalle Valo  * @bsta: the broadcast station to add. */
2081e705c121SKalle Valo int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2082e705c121SKalle Valo {
2083e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2084e705c121SKalle Valo 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2085e705c121SKalle Valo 	int ret;
2086e705c121SKalle Valo 
2087e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2088e705c121SKalle Valo 
2089e705c121SKalle Valo 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2090e705c121SKalle Valo 	if (ret)
2091e705c121SKalle Valo 		return ret;
2092e705c121SKalle Valo 
2093e705c121SKalle Valo 	ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2094e705c121SKalle Valo 
2095e705c121SKalle Valo 	if (ret)
2096e705c121SKalle Valo 		iwl_mvm_dealloc_int_sta(mvm, bsta);
2097e705c121SKalle Valo 
2098e705c121SKalle Valo 	return ret;
2099e705c121SKalle Valo }
2100e705c121SKalle Valo 
2101e705c121SKalle Valo void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2102e705c121SKalle Valo {
2103e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2104e705c121SKalle Valo 
2105e705c121SKalle Valo 	iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2106e705c121SKalle Valo }
2107e705c121SKalle Valo 
2108e705c121SKalle Valo /*
2109e705c121SKalle Valo  * Send the FW a request to remove the station from it's internal data
2110e705c121SKalle Valo  * structures, and in addition remove it from the local data structure.
2111e705c121SKalle Valo  */
2112e705c121SKalle Valo int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2113e705c121SKalle Valo {
2114e705c121SKalle Valo 	int ret;
2115e705c121SKalle Valo 
2116e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2117e705c121SKalle Valo 
2118e705c121SKalle Valo 	ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2119e705c121SKalle Valo 
2120e705c121SKalle Valo 	iwl_mvm_dealloc_bcast_sta(mvm, vif);
2121e705c121SKalle Valo 
2122e705c121SKalle Valo 	return ret;
2123e705c121SKalle Valo }
2124e705c121SKalle Valo 
212526d6c16bSSara Sharon /*
212626d6c16bSSara Sharon  * Allocate a new station entry for the multicast station to the given vif,
212726d6c16bSSara Sharon  * and send it to the FW.
212826d6c16bSSara Sharon  * Note that each AP/GO mac should have its own multicast station.
212926d6c16bSSara Sharon  *
213026d6c16bSSara Sharon  * @mvm: the mvm component
213126d6c16bSSara Sharon  * @vif: the interface to which the multicast station is added
213226d6c16bSSara Sharon  */
213326d6c16bSSara Sharon int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
213426d6c16bSSara Sharon {
213526d6c16bSSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
213626d6c16bSSara Sharon 	struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
213726d6c16bSSara Sharon 	static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
213826d6c16bSSara Sharon 	const u8 *maddr = _maddr;
213926d6c16bSSara Sharon 	struct iwl_trans_txq_scd_cfg cfg = {
214026d6c16bSSara Sharon 		.fifo = IWL_MVM_TX_FIFO_MCAST,
214126d6c16bSSara Sharon 		.sta_id = msta->sta_id,
214226d6c16bSSara Sharon 		.tid = IWL_MAX_TID_COUNT,
214326d6c16bSSara Sharon 		.aggregate = false,
214426d6c16bSSara Sharon 		.frame_limit = IWL_FRAME_LIMIT,
214526d6c16bSSara Sharon 	};
214626d6c16bSSara Sharon 	unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
214726d6c16bSSara Sharon 	int ret;
214826d6c16bSSara Sharon 
214926d6c16bSSara Sharon 	lockdep_assert_held(&mvm->mutex);
215026d6c16bSSara Sharon 
215126d6c16bSSara Sharon 	if (!iwl_mvm_is_dqa_supported(mvm))
215226d6c16bSSara Sharon 		return 0;
215326d6c16bSSara Sharon 
2154ee48b722SLiad Kaufman 	if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2155ee48b722SLiad Kaufman 		    vif->type != NL80211_IFTYPE_ADHOC))
215626d6c16bSSara Sharon 		return -ENOTSUPP;
215726d6c16bSSara Sharon 
2158ced19f26SSara Sharon 	/*
2159ced19f26SSara Sharon 	 * While in previous FWs we had to exclude cab queue from TFD queue
2160ced19f26SSara Sharon 	 * mask, now it is needed as any other queue.
2161ced19f26SSara Sharon 	 */
2162ced19f26SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm) &&
2163ced19f26SSara Sharon 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2164ced19f26SSara Sharon 		iwl_mvm_enable_txq(mvm, vif->cab_queue, vif->cab_queue, 0,
2165ced19f26SSara Sharon 				   &cfg, timeout);
2166ced19f26SSara Sharon 		msta->tfd_queue_msk |= BIT(vif->cab_queue);
2167ced19f26SSara Sharon 	}
216826d6c16bSSara Sharon 	ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
216926d6c16bSSara Sharon 					 mvmvif->id, mvmvif->color);
217026d6c16bSSara Sharon 	if (ret) {
217126d6c16bSSara Sharon 		iwl_mvm_dealloc_int_sta(mvm, msta);
217226d6c16bSSara Sharon 		return ret;
217326d6c16bSSara Sharon 	}
217426d6c16bSSara Sharon 
217526d6c16bSSara Sharon 	/*
217626d6c16bSSara Sharon 	 * Enable cab queue after the ADD_STA command is sent.
217726d6c16bSSara Sharon 	 * This is needed for a000 firmware which won't accept SCD_QUEUE_CFG
2178ced19f26SSara Sharon 	 * command with unknown station id, and for FW that doesn't support
2179ced19f26SSara Sharon 	 * station API since the cab queue is not included in the
2180ced19f26SSara Sharon 	 * tfd_queue_mask.
218126d6c16bSSara Sharon 	 */
2182310181ecSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
2183310181ecSSara Sharon 		int queue = iwl_mvm_tvqm_enable_txq(mvm, vif->cab_queue,
2184310181ecSSara Sharon 						    msta->sta_id,
2185310181ecSSara Sharon 						    IWL_MAX_TID_COUNT,
218626d6c16bSSara Sharon 						    timeout);
2187e2af3fabSSara Sharon 		mvmvif->cab_queue = queue;
2188ced19f26SSara Sharon 	} else if (!fw_has_api(&mvm->fw->ucode_capa,
2189ced19f26SSara Sharon 			       IWL_UCODE_TLV_API_STA_TYPE)) {
2190ee48b722SLiad Kaufman 		/*
2191ee48b722SLiad Kaufman 		 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2192ee48b722SLiad Kaufman 		 * invalid, so make sure we use the queue we want.
2193ee48b722SLiad Kaufman 		 * Note that this is done here as we want to avoid making DQA
2194ee48b722SLiad Kaufman 		 * changes in mac80211 layer.
2195ee48b722SLiad Kaufman 		 */
2196ee48b722SLiad Kaufman 		if (vif->type == NL80211_IFTYPE_ADHOC) {
2197ee48b722SLiad Kaufman 			vif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2198ee48b722SLiad Kaufman 			mvmvif->cab_queue = vif->cab_queue;
2199ee48b722SLiad Kaufman 		}
2200310181ecSSara Sharon 		iwl_mvm_enable_txq(mvm, vif->cab_queue, vif->cab_queue, 0,
2201310181ecSSara Sharon 				   &cfg, timeout);
2202310181ecSSara Sharon 	}
220326d6c16bSSara Sharon 
220426d6c16bSSara Sharon 	return 0;
220526d6c16bSSara Sharon }
220626d6c16bSSara Sharon 
220726d6c16bSSara Sharon /*
220826d6c16bSSara Sharon  * Send the FW a request to remove the station from it's internal data
220926d6c16bSSara Sharon  * structures, and in addition remove it from the local data structure.
221026d6c16bSSara Sharon  */
221126d6c16bSSara Sharon int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
221226d6c16bSSara Sharon {
221326d6c16bSSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
221426d6c16bSSara Sharon 	int ret;
221526d6c16bSSara Sharon 
221626d6c16bSSara Sharon 	lockdep_assert_held(&mvm->mutex);
221726d6c16bSSara Sharon 
221826d6c16bSSara Sharon 	if (!iwl_mvm_is_dqa_supported(mvm))
221926d6c16bSSara Sharon 		return 0;
222026d6c16bSSara Sharon 
2221d49394a1SSara Sharon 	iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true, 0);
2222d49394a1SSara Sharon 
2223e2af3fabSSara Sharon 	iwl_mvm_disable_txq(mvm, mvmvif->cab_queue, vif->cab_queue,
222426d6c16bSSara Sharon 			    IWL_MAX_TID_COUNT, 0);
222526d6c16bSSara Sharon 
222626d6c16bSSara Sharon 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
222726d6c16bSSara Sharon 	if (ret)
222826d6c16bSSara Sharon 		IWL_WARN(mvm, "Failed sending remove station\n");
222926d6c16bSSara Sharon 
223026d6c16bSSara Sharon 	return ret;
223126d6c16bSSara Sharon }
223226d6c16bSSara Sharon 
2233e705c121SKalle Valo #define IWL_MAX_RX_BA_SESSIONS 16
2234e705c121SKalle Valo 
2235b915c101SSara Sharon static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
223610b2b201SSara Sharon {
2237b915c101SSara Sharon 	struct iwl_mvm_delba_notif notif = {
2238b915c101SSara Sharon 		.metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
2239b915c101SSara Sharon 		.metadata.sync = 1,
2240b915c101SSara Sharon 		.delba.baid = baid,
2241b915c101SSara Sharon 	};
2242b915c101SSara Sharon 	iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
224310b2b201SSara Sharon };
224410b2b201SSara Sharon 
2245b915c101SSara Sharon static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2246b915c101SSara Sharon 				 struct iwl_mvm_baid_data *data)
2247b915c101SSara Sharon {
2248b915c101SSara Sharon 	int i;
2249b915c101SSara Sharon 
2250b915c101SSara Sharon 	iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2251b915c101SSara Sharon 
2252b915c101SSara Sharon 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2253b915c101SSara Sharon 		int j;
2254b915c101SSara Sharon 		struct iwl_mvm_reorder_buffer *reorder_buf =
2255b915c101SSara Sharon 			&data->reorder_buf[i];
2256b915c101SSara Sharon 
22570690405fSSara Sharon 		spin_lock_bh(&reorder_buf->lock);
22580690405fSSara Sharon 		if (likely(!reorder_buf->num_stored)) {
22590690405fSSara Sharon 			spin_unlock_bh(&reorder_buf->lock);
2260b915c101SSara Sharon 			continue;
22610690405fSSara Sharon 		}
2262b915c101SSara Sharon 
2263b915c101SSara Sharon 		/*
2264b915c101SSara Sharon 		 * This shouldn't happen in regular DELBA since the internal
2265b915c101SSara Sharon 		 * delBA notification should trigger a release of all frames in
2266b915c101SSara Sharon 		 * the reorder buffer.
2267b915c101SSara Sharon 		 */
2268b915c101SSara Sharon 		WARN_ON(1);
2269b915c101SSara Sharon 
2270b915c101SSara Sharon 		for (j = 0; j < reorder_buf->buf_size; j++)
2271b915c101SSara Sharon 			__skb_queue_purge(&reorder_buf->entries[j]);
22720690405fSSara Sharon 		/*
22730690405fSSara Sharon 		 * Prevent timer re-arm. This prevents a very far fetched case
22740690405fSSara Sharon 		 * where we timed out on the notification. There may be prior
22750690405fSSara Sharon 		 * RX frames pending in the RX queue before the notification
22760690405fSSara Sharon 		 * that might get processed between now and the actual deletion
22770690405fSSara Sharon 		 * and we would re-arm the timer although we are deleting the
22780690405fSSara Sharon 		 * reorder buffer.
22790690405fSSara Sharon 		 */
22800690405fSSara Sharon 		reorder_buf->removed = true;
22810690405fSSara Sharon 		spin_unlock_bh(&reorder_buf->lock);
22820690405fSSara Sharon 		del_timer_sync(&reorder_buf->reorder_timer);
2283b915c101SSara Sharon 	}
2284b915c101SSara Sharon }
2285b915c101SSara Sharon 
2286b915c101SSara Sharon static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2287b915c101SSara Sharon 					u32 sta_id,
2288b915c101SSara Sharon 					struct iwl_mvm_baid_data *data,
2289b915c101SSara Sharon 					u16 ssn, u8 buf_size)
2290b915c101SSara Sharon {
2291b915c101SSara Sharon 	int i;
2292b915c101SSara Sharon 
2293b915c101SSara Sharon 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2294b915c101SSara Sharon 		struct iwl_mvm_reorder_buffer *reorder_buf =
2295b915c101SSara Sharon 			&data->reorder_buf[i];
2296b915c101SSara Sharon 		int j;
2297b915c101SSara Sharon 
2298b915c101SSara Sharon 		reorder_buf->num_stored = 0;
2299b915c101SSara Sharon 		reorder_buf->head_sn = ssn;
2300b915c101SSara Sharon 		reorder_buf->buf_size = buf_size;
23010690405fSSara Sharon 		/* rx reorder timer */
23020690405fSSara Sharon 		reorder_buf->reorder_timer.function =
23030690405fSSara Sharon 			iwl_mvm_reorder_timer_expired;
23040690405fSSara Sharon 		reorder_buf->reorder_timer.data = (unsigned long)reorder_buf;
23050690405fSSara Sharon 		init_timer(&reorder_buf->reorder_timer);
23060690405fSSara Sharon 		spin_lock_init(&reorder_buf->lock);
23070690405fSSara Sharon 		reorder_buf->mvm = mvm;
2308b915c101SSara Sharon 		reorder_buf->queue = i;
2309b915c101SSara Sharon 		reorder_buf->sta_id = sta_id;
23105d43eab6SSara Sharon 		reorder_buf->valid = false;
2311b915c101SSara Sharon 		for (j = 0; j < reorder_buf->buf_size; j++)
2312b915c101SSara Sharon 			__skb_queue_head_init(&reorder_buf->entries[j]);
2313b915c101SSara Sharon 	}
231410b2b201SSara Sharon }
231510b2b201SSara Sharon 
2316e705c121SKalle Valo int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
231710b2b201SSara Sharon 		       int tid, u16 ssn, bool start, u8 buf_size, u16 timeout)
2318e705c121SKalle Valo {
2319e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2320e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
232110b2b201SSara Sharon 	struct iwl_mvm_baid_data *baid_data = NULL;
2322e705c121SKalle Valo 	int ret;
2323e705c121SKalle Valo 	u32 status;
2324e705c121SKalle Valo 
2325e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2326e705c121SKalle Valo 
2327e705c121SKalle Valo 	if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
2328e705c121SKalle Valo 		IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2329e705c121SKalle Valo 		return -ENOSPC;
2330e705c121SKalle Valo 	}
2331e705c121SKalle Valo 
233210b2b201SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) && start) {
233310b2b201SSara Sharon 		/*
233410b2b201SSara Sharon 		 * Allocate here so if allocation fails we can bail out early
233510b2b201SSara Sharon 		 * before starting the BA session in the firmware
233610b2b201SSara Sharon 		 */
2337b915c101SSara Sharon 		baid_data = kzalloc(sizeof(*baid_data) +
2338b915c101SSara Sharon 				    mvm->trans->num_rx_queues *
2339b915c101SSara Sharon 				    sizeof(baid_data->reorder_buf[0]),
2340b915c101SSara Sharon 				    GFP_KERNEL);
234110b2b201SSara Sharon 		if (!baid_data)
234210b2b201SSara Sharon 			return -ENOMEM;
234310b2b201SSara Sharon 	}
234410b2b201SSara Sharon 
2345e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2346e705c121SKalle Valo 	cmd.sta_id = mvm_sta->sta_id;
2347e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
2348e705c121SKalle Valo 	if (start) {
2349e705c121SKalle Valo 		cmd.add_immediate_ba_tid = (u8) tid;
2350e705c121SKalle Valo 		cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
2351854c5705SSara Sharon 		cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
2352e705c121SKalle Valo 	} else {
2353e705c121SKalle Valo 		cmd.remove_immediate_ba_tid = (u8) tid;
2354e705c121SKalle Valo 	}
2355e705c121SKalle Valo 	cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2356e705c121SKalle Valo 				  STA_MODIFY_REMOVE_BA_TID;
2357e705c121SKalle Valo 
2358e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
2359854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2360854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
2361e705c121SKalle Valo 					  &cmd, &status);
2362e705c121SKalle Valo 	if (ret)
236310b2b201SSara Sharon 		goto out_free;
2364e705c121SKalle Valo 
2365837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2366e705c121SKalle Valo 	case ADD_STA_SUCCESS:
236735263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2368e705c121SKalle Valo 			     start ? "start" : "stopp");
2369e705c121SKalle Valo 		break;
2370e705c121SKalle Valo 	case ADD_STA_IMMEDIATE_BA_FAILURE:
2371e705c121SKalle Valo 		IWL_WARN(mvm, "RX BA Session refused by fw\n");
2372e705c121SKalle Valo 		ret = -ENOSPC;
2373e705c121SKalle Valo 		break;
2374e705c121SKalle Valo 	default:
2375e705c121SKalle Valo 		ret = -EIO;
2376e705c121SKalle Valo 		IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2377e705c121SKalle Valo 			start ? "start" : "stopp", status);
2378e705c121SKalle Valo 		break;
2379e705c121SKalle Valo 	}
2380e705c121SKalle Valo 
238110b2b201SSara Sharon 	if (ret)
238210b2b201SSara Sharon 		goto out_free;
238310b2b201SSara Sharon 
238410b2b201SSara Sharon 	if (start) {
238510b2b201SSara Sharon 		u8 baid;
238610b2b201SSara Sharon 
2387e705c121SKalle Valo 		mvm->rx_ba_sessions++;
238810b2b201SSara Sharon 
238910b2b201SSara Sharon 		if (!iwl_mvm_has_new_rx_api(mvm))
239010b2b201SSara Sharon 			return 0;
239110b2b201SSara Sharon 
239210b2b201SSara Sharon 		if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
239310b2b201SSara Sharon 			ret = -EINVAL;
239410b2b201SSara Sharon 			goto out_free;
239510b2b201SSara Sharon 		}
239610b2b201SSara Sharon 		baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
239710b2b201SSara Sharon 			    IWL_ADD_STA_BAID_SHIFT);
239810b2b201SSara Sharon 		baid_data->baid = baid;
239910b2b201SSara Sharon 		baid_data->timeout = timeout;
240010b2b201SSara Sharon 		baid_data->last_rx = jiffies;
240172c240feSWei Yongjun 		setup_timer(&baid_data->session_timer,
240272c240feSWei Yongjun 			    iwl_mvm_rx_agg_session_expired,
240372c240feSWei Yongjun 			    (unsigned long)&mvm->baid_map[baid]);
240410b2b201SSara Sharon 		baid_data->mvm = mvm;
240510b2b201SSara Sharon 		baid_data->tid = tid;
240610b2b201SSara Sharon 		baid_data->sta_id = mvm_sta->sta_id;
240710b2b201SSara Sharon 
240810b2b201SSara Sharon 		mvm_sta->tid_to_baid[tid] = baid;
240910b2b201SSara Sharon 		if (timeout)
241010b2b201SSara Sharon 			mod_timer(&baid_data->session_timer,
241110b2b201SSara Sharon 				  TU_TO_EXP_TIME(timeout * 2));
241210b2b201SSara Sharon 
2413b915c101SSara Sharon 		iwl_mvm_init_reorder_buffer(mvm, mvm_sta->sta_id,
2414b915c101SSara Sharon 					    baid_data, ssn, buf_size);
241510b2b201SSara Sharon 		/*
241610b2b201SSara Sharon 		 * protect the BA data with RCU to cover a case where our
241710b2b201SSara Sharon 		 * internal RX sync mechanism will timeout (not that it's
241810b2b201SSara Sharon 		 * supposed to happen) and we will free the session data while
241910b2b201SSara Sharon 		 * RX is being processed in parallel
242010b2b201SSara Sharon 		 */
242135263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
242235263a03SSara Sharon 			     mvm_sta->sta_id, tid, baid);
242310b2b201SSara Sharon 		WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
242410b2b201SSara Sharon 		rcu_assign_pointer(mvm->baid_map[baid], baid_data);
242560dec523SSara Sharon 	} else  {
242610b2b201SSara Sharon 		u8 baid = mvm_sta->tid_to_baid[tid];
242710b2b201SSara Sharon 
242860dec523SSara Sharon 		if (mvm->rx_ba_sessions > 0)
2429e705c121SKalle Valo 			/* check that restart flow didn't zero the counter */
2430e705c121SKalle Valo 			mvm->rx_ba_sessions--;
243110b2b201SSara Sharon 		if (!iwl_mvm_has_new_rx_api(mvm))
243210b2b201SSara Sharon 			return 0;
2433e705c121SKalle Valo 
243410b2b201SSara Sharon 		if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
243510b2b201SSara Sharon 			return -EINVAL;
243610b2b201SSara Sharon 
243710b2b201SSara Sharon 		baid_data = rcu_access_pointer(mvm->baid_map[baid]);
243810b2b201SSara Sharon 		if (WARN_ON(!baid_data))
243910b2b201SSara Sharon 			return -EINVAL;
244010b2b201SSara Sharon 
244110b2b201SSara Sharon 		/* synchronize all rx queues so we can safely delete */
2442b915c101SSara Sharon 		iwl_mvm_free_reorder(mvm, baid_data);
244310b2b201SSara Sharon 		del_timer_sync(&baid_data->session_timer);
244410b2b201SSara Sharon 		RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
244510b2b201SSara Sharon 		kfree_rcu(baid_data, rcu_head);
244635263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
244710b2b201SSara Sharon 	}
244810b2b201SSara Sharon 	return 0;
244910b2b201SSara Sharon 
245010b2b201SSara Sharon out_free:
245110b2b201SSara Sharon 	kfree(baid_data);
2452e705c121SKalle Valo 	return ret;
2453e705c121SKalle Valo }
2454e705c121SKalle Valo 
24559794c64fSLiad Kaufman int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2456e705c121SKalle Valo 		       int tid, u8 queue, bool start)
2457e705c121SKalle Valo {
2458e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2459e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
2460e705c121SKalle Valo 	int ret;
2461e705c121SKalle Valo 	u32 status;
2462e705c121SKalle Valo 
2463e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2464e705c121SKalle Valo 
2465e705c121SKalle Valo 	if (start) {
2466e705c121SKalle Valo 		mvm_sta->tfd_queue_msk |= BIT(queue);
2467e705c121SKalle Valo 		mvm_sta->tid_disable_agg &= ~BIT(tid);
2468e705c121SKalle Valo 	} else {
2469cf961e16SLiad Kaufman 		/* In DQA-mode the queue isn't removed on agg termination */
2470cf961e16SLiad Kaufman 		if (!iwl_mvm_is_dqa_supported(mvm))
2471e705c121SKalle Valo 			mvm_sta->tfd_queue_msk &= ~BIT(queue);
2472e705c121SKalle Valo 		mvm_sta->tid_disable_agg |= BIT(tid);
2473e705c121SKalle Valo 	}
2474e705c121SKalle Valo 
2475e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2476e705c121SKalle Valo 	cmd.sta_id = mvm_sta->sta_id;
2477e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
2478bb49701bSSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm))
2479bb49701bSSara Sharon 		cmd.modify_mask = STA_MODIFY_QUEUES;
2480bb49701bSSara Sharon 	cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
2481e705c121SKalle Valo 	cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2482e705c121SKalle Valo 	cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2483e705c121SKalle Valo 
2484e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
2485854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2486854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
2487e705c121SKalle Valo 					  &cmd, &status);
2488e705c121SKalle Valo 	if (ret)
2489e705c121SKalle Valo 		return ret;
2490e705c121SKalle Valo 
2491837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2492e705c121SKalle Valo 	case ADD_STA_SUCCESS:
2493e705c121SKalle Valo 		break;
2494e705c121SKalle Valo 	default:
2495e705c121SKalle Valo 		ret = -EIO;
2496e705c121SKalle Valo 		IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2497e705c121SKalle Valo 			start ? "start" : "stopp", status);
2498e705c121SKalle Valo 		break;
2499e705c121SKalle Valo 	}
2500e705c121SKalle Valo 
2501e705c121SKalle Valo 	return ret;
2502e705c121SKalle Valo }
2503e705c121SKalle Valo 
2504e705c121SKalle Valo const u8 tid_to_mac80211_ac[] = {
2505e705c121SKalle Valo 	IEEE80211_AC_BE,
2506e705c121SKalle Valo 	IEEE80211_AC_BK,
2507e705c121SKalle Valo 	IEEE80211_AC_BK,
2508e705c121SKalle Valo 	IEEE80211_AC_BE,
2509e705c121SKalle Valo 	IEEE80211_AC_VI,
2510e705c121SKalle Valo 	IEEE80211_AC_VI,
2511e705c121SKalle Valo 	IEEE80211_AC_VO,
2512e705c121SKalle Valo 	IEEE80211_AC_VO,
25139794c64fSLiad Kaufman 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2514e705c121SKalle Valo };
2515e705c121SKalle Valo 
2516e705c121SKalle Valo static const u8 tid_to_ucode_ac[] = {
2517e705c121SKalle Valo 	AC_BE,
2518e705c121SKalle Valo 	AC_BK,
2519e705c121SKalle Valo 	AC_BK,
2520e705c121SKalle Valo 	AC_BE,
2521e705c121SKalle Valo 	AC_VI,
2522e705c121SKalle Valo 	AC_VI,
2523e705c121SKalle Valo 	AC_VO,
2524e705c121SKalle Valo 	AC_VO,
2525e705c121SKalle Valo };
2526e705c121SKalle Valo 
2527e705c121SKalle Valo int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2528e705c121SKalle Valo 			     struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2529e705c121SKalle Valo {
2530e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2531e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data;
2532e705c121SKalle Valo 	int txq_id;
2533e705c121SKalle Valo 	int ret;
2534e705c121SKalle Valo 
2535e705c121SKalle Valo 	if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2536e705c121SKalle Valo 		return -EINVAL;
2537e705c121SKalle Valo 
2538e705c121SKalle Valo 	if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2539e705c121SKalle Valo 		IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
2540e705c121SKalle Valo 			mvmsta->tid_data[tid].state);
2541e705c121SKalle Valo 		return -ENXIO;
2542e705c121SKalle Valo 	}
2543e705c121SKalle Valo 
2544e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2545e705c121SKalle Valo 
2546e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2547e705c121SKalle Valo 
2548e705c121SKalle Valo 	/* possible race condition - we entered D0i3 while starting agg */
2549e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
2550e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
2551e705c121SKalle Valo 		IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
2552e705c121SKalle Valo 		return -EIO;
2553e705c121SKalle Valo 	}
2554e705c121SKalle Valo 
25559f9af3d7SLiad Kaufman 	spin_lock(&mvm->queue_info_lock);
2556e705c121SKalle Valo 
2557cf961e16SLiad Kaufman 	/*
2558cf961e16SLiad Kaufman 	 * Note the possible cases:
2559cf961e16SLiad Kaufman 	 *  1. In DQA mode with an enabled TXQ - TXQ needs to become agg'ed
2560cf961e16SLiad Kaufman 	 *  2. Non-DQA mode: the TXQ hasn't yet been enabled, so find a free
2561cf961e16SLiad Kaufman 	 *	one and mark it as reserved
2562cf961e16SLiad Kaufman 	 *  3. In DQA mode, but no traffic yet on this TID: same treatment as in
2563cf961e16SLiad Kaufman 	 *	non-DQA mode, since the TXQ hasn't yet been allocated
256434e10860SSara Sharon 	 * Don't support case 3 for new TX path as it is not expected to happen
256534e10860SSara Sharon 	 * and aggregation will be offloaded soon anyway
2566cf961e16SLiad Kaufman 	 */
2567cf961e16SLiad Kaufman 	txq_id = mvmsta->tid_data[tid].txq_id;
256834e10860SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
256934e10860SSara Sharon 		if (txq_id == IWL_MVM_INVALID_QUEUE) {
257034e10860SSara Sharon 			ret = -ENXIO;
257134e10860SSara Sharon 			goto release_locks;
257234e10860SSara Sharon 		}
257334e10860SSara Sharon 	} else if (iwl_mvm_is_dqa_supported(mvm) &&
257434e10860SSara Sharon 		   unlikely(mvm->queue_info[txq_id].status ==
257534e10860SSara Sharon 			    IWL_MVM_QUEUE_SHARED)) {
25769f9af3d7SLiad Kaufman 		ret = -ENXIO;
25779f9af3d7SLiad Kaufman 		IWL_DEBUG_TX_QUEUES(mvm,
25789f9af3d7SLiad Kaufman 				    "Can't start tid %d agg on shared queue!\n",
25799f9af3d7SLiad Kaufman 				    tid);
25809f9af3d7SLiad Kaufman 		goto release_locks;
25819f9af3d7SLiad Kaufman 	} else if (!iwl_mvm_is_dqa_supported(mvm) ||
2582cf961e16SLiad Kaufman 	    mvm->queue_info[txq_id].status != IWL_MVM_QUEUE_READY) {
25839794c64fSLiad Kaufman 		txq_id = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
25849794c64fSLiad Kaufman 						 mvm->first_agg_queue,
2585e705c121SKalle Valo 						 mvm->last_agg_queue);
2586e705c121SKalle Valo 		if (txq_id < 0) {
2587e705c121SKalle Valo 			ret = txq_id;
2588e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to allocate agg queue\n");
2589e705c121SKalle Valo 			goto release_locks;
2590e705c121SKalle Valo 		}
259101796ff2SSara Sharon 		/*
259201796ff2SSara Sharon 		 * TXQ shouldn't be in inactive mode for non-DQA, so getting
259301796ff2SSara Sharon 		 * an inactive queue from iwl_mvm_find_free_queue() is
259401796ff2SSara Sharon 		 * certainly a bug
259501796ff2SSara Sharon 		 */
259601796ff2SSara Sharon 		WARN_ON(mvm->queue_info[txq_id].status ==
259701796ff2SSara Sharon 			IWL_MVM_QUEUE_INACTIVE);
2598cf961e16SLiad Kaufman 
2599cf961e16SLiad Kaufman 		/* TXQ hasn't yet been enabled, so mark it only as reserved */
2600cf961e16SLiad Kaufman 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2601cf961e16SLiad Kaufman 	}
26029f9af3d7SLiad Kaufman 
26039f9af3d7SLiad Kaufman 	spin_unlock(&mvm->queue_info_lock);
2604e705c121SKalle Valo 
2605cf961e16SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm,
2606cf961e16SLiad Kaufman 			    "AGG for tid %d will be on queue #%d\n",
2607cf961e16SLiad Kaufman 			    tid, txq_id);
2608cf961e16SLiad Kaufman 
2609e705c121SKalle Valo 	tid_data = &mvmsta->tid_data[tid];
2610e705c121SKalle Valo 	tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2611e705c121SKalle Valo 	tid_data->txq_id = txq_id;
2612e705c121SKalle Valo 	*ssn = tid_data->ssn;
2613e705c121SKalle Valo 
2614e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm,
2615e705c121SKalle Valo 			    "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2616e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2617e705c121SKalle Valo 			    tid_data->next_reclaimed);
2618e705c121SKalle Valo 
2619e705c121SKalle Valo 	if (tid_data->ssn == tid_data->next_reclaimed) {
2620e705c121SKalle Valo 		tid_data->state = IWL_AGG_STARTING;
2621e705c121SKalle Valo 		ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2622e705c121SKalle Valo 	} else {
2623e705c121SKalle Valo 		tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2624e705c121SKalle Valo 	}
2625e705c121SKalle Valo 
2626e705c121SKalle Valo 	ret = 0;
26279f9af3d7SLiad Kaufman 	goto out;
2628e705c121SKalle Valo 
2629e705c121SKalle Valo release_locks:
26309f9af3d7SLiad Kaufman 	spin_unlock(&mvm->queue_info_lock);
26319f9af3d7SLiad Kaufman out:
2632e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2633e705c121SKalle Valo 
2634e705c121SKalle Valo 	return ret;
2635e705c121SKalle Valo }
2636e705c121SKalle Valo 
2637e705c121SKalle Valo int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2638bb81bb68SEmmanuel Grumbach 			    struct ieee80211_sta *sta, u16 tid, u8 buf_size,
2639bb81bb68SEmmanuel Grumbach 			    bool amsdu)
2640e705c121SKalle Valo {
2641e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2642e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2643e705c121SKalle Valo 	unsigned int wdg_timeout =
2644e705c121SKalle Valo 		iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
2645eea76c36SEmmanuel Grumbach 	int queue, ret;
2646cf961e16SLiad Kaufman 	bool alloc_queue = true;
26479f9af3d7SLiad Kaufman 	enum iwl_mvm_queue_status queue_status;
2648e705c121SKalle Valo 	u16 ssn;
2649e705c121SKalle Valo 
2650eea76c36SEmmanuel Grumbach 	struct iwl_trans_txq_scd_cfg cfg = {
2651eea76c36SEmmanuel Grumbach 		.sta_id = mvmsta->sta_id,
2652eea76c36SEmmanuel Grumbach 		.tid = tid,
2653eea76c36SEmmanuel Grumbach 		.frame_limit = buf_size,
2654eea76c36SEmmanuel Grumbach 		.aggregate = true,
2655eea76c36SEmmanuel Grumbach 	};
2656eea76c36SEmmanuel Grumbach 
2657e705c121SKalle Valo 	BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2658e705c121SKalle Valo 		     != IWL_MAX_TID_COUNT);
2659e705c121SKalle Valo 
2660e705c121SKalle Valo 	buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
2661e705c121SKalle Valo 
2662e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2663e705c121SKalle Valo 	ssn = tid_data->ssn;
2664e705c121SKalle Valo 	queue = tid_data->txq_id;
2665e705c121SKalle Valo 	tid_data->state = IWL_AGG_ON;
2666e705c121SKalle Valo 	mvmsta->agg_tids |= BIT(tid);
2667e705c121SKalle Valo 	tid_data->ssn = 0xffff;
2668bb81bb68SEmmanuel Grumbach 	tid_data->amsdu_in_ampdu_allowed = amsdu;
2669e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2670e705c121SKalle Valo 
267134e10860SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
267234e10860SSara Sharon 		/*
267334e10860SSara Sharon 		 * If no queue iwl_mvm_sta_tx_agg_start() would have failed so
267434e10860SSara Sharon 		 * no need to check queue's status
267534e10860SSara Sharon 		 */
267634e10860SSara Sharon 		if (buf_size < mvmsta->max_agg_bufsize)
267734e10860SSara Sharon 			return -ENOTSUPP;
267834e10860SSara Sharon 
267934e10860SSara Sharon 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
268034e10860SSara Sharon 		if (ret)
268134e10860SSara Sharon 			return -EIO;
268234e10860SSara Sharon 		goto out;
268334e10860SSara Sharon 	}
268434e10860SSara Sharon 
2685eea76c36SEmmanuel Grumbach 	cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
2686e705c121SKalle Valo 
26879f9af3d7SLiad Kaufman 	spin_lock_bh(&mvm->queue_info_lock);
26889f9af3d7SLiad Kaufman 	queue_status = mvm->queue_info[queue].status;
26899f9af3d7SLiad Kaufman 	spin_unlock_bh(&mvm->queue_info_lock);
26909f9af3d7SLiad Kaufman 
2691cf961e16SLiad Kaufman 	/* In DQA mode, the existing queue might need to be reconfigured */
2692cf961e16SLiad Kaufman 	if (iwl_mvm_is_dqa_supported(mvm)) {
2693cf961e16SLiad Kaufman 		/* Maybe there is no need to even alloc a queue... */
2694cf961e16SLiad Kaufman 		if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2695cf961e16SLiad Kaufman 			alloc_queue = false;
2696cf961e16SLiad Kaufman 
2697cf961e16SLiad Kaufman 		/*
2698cf961e16SLiad Kaufman 		 * Only reconfig the SCD for the queue if the window size has
2699cf961e16SLiad Kaufman 		 * changed from current (become smaller)
2700cf961e16SLiad Kaufman 		 */
2701cf961e16SLiad Kaufman 		if (!alloc_queue && buf_size < mvmsta->max_agg_bufsize) {
2702cf961e16SLiad Kaufman 			/*
2703cf961e16SLiad Kaufman 			 * If reconfiguring an existing queue, it first must be
2704cf961e16SLiad Kaufman 			 * drained
2705cf961e16SLiad Kaufman 			 */
2706a1a57877SSara Sharon 			ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2707cf961e16SLiad Kaufman 							     BIT(queue));
2708cf961e16SLiad Kaufman 			if (ret) {
2709cf961e16SLiad Kaufman 				IWL_ERR(mvm,
2710cf961e16SLiad Kaufman 					"Error draining queue before reconfig\n");
2711cf961e16SLiad Kaufman 				return ret;
2712cf961e16SLiad Kaufman 			}
2713cf961e16SLiad Kaufman 
2714cf961e16SLiad Kaufman 			ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2715cf961e16SLiad Kaufman 						   mvmsta->sta_id, tid,
2716cf961e16SLiad Kaufman 						   buf_size, ssn);
2717cf961e16SLiad Kaufman 			if (ret) {
2718cf961e16SLiad Kaufman 				IWL_ERR(mvm,
2719cf961e16SLiad Kaufman 					"Error reconfiguring TXQ #%d\n", queue);
2720cf961e16SLiad Kaufman 				return ret;
2721cf961e16SLiad Kaufman 			}
2722cf961e16SLiad Kaufman 		}
2723cf961e16SLiad Kaufman 	}
2724cf961e16SLiad Kaufman 
2725cf961e16SLiad Kaufman 	if (alloc_queue)
2726cf961e16SLiad Kaufman 		iwl_mvm_enable_txq(mvm, queue,
2727cf961e16SLiad Kaufman 				   vif->hw_queue[tid_to_mac80211_ac[tid]], ssn,
2728cf961e16SLiad Kaufman 				   &cfg, wdg_timeout);
2729e705c121SKalle Valo 
27309f9af3d7SLiad Kaufman 	/* Send ADD_STA command to enable aggs only if the queue isn't shared */
27319f9af3d7SLiad Kaufman 	if (queue_status != IWL_MVM_QUEUE_SHARED) {
2732e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2733e705c121SKalle Valo 		if (ret)
2734e705c121SKalle Valo 			return -EIO;
27359f9af3d7SLiad Kaufman 	}
2736e705c121SKalle Valo 
2737e705c121SKalle Valo 	/* No need to mark as reserved */
2738e705c121SKalle Valo 	spin_lock_bh(&mvm->queue_info_lock);
2739cf961e16SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
2740e705c121SKalle Valo 	spin_unlock_bh(&mvm->queue_info_lock);
2741e705c121SKalle Valo 
274234e10860SSara Sharon out:
2743e705c121SKalle Valo 	/*
2744e705c121SKalle Valo 	 * Even though in theory the peer could have different
2745e705c121SKalle Valo 	 * aggregation reorder buffer sizes for different sessions,
2746e705c121SKalle Valo 	 * our ucode doesn't allow for that and has a global limit
2747e705c121SKalle Valo 	 * for each station. Therefore, use the minimum of all the
2748e705c121SKalle Valo 	 * aggregation sessions and our default value.
2749e705c121SKalle Valo 	 */
2750e705c121SKalle Valo 	mvmsta->max_agg_bufsize =
2751e705c121SKalle Valo 		min(mvmsta->max_agg_bufsize, buf_size);
2752e705c121SKalle Valo 	mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2753e705c121SKalle Valo 
2754e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2755e705c121SKalle Valo 		     sta->addr, tid);
2756e705c121SKalle Valo 
2757e705c121SKalle Valo 	return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
2758e705c121SKalle Valo }
2759e705c121SKalle Valo 
276034e10860SSara Sharon static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
276134e10860SSara Sharon 					struct iwl_mvm_sta *mvmsta,
276234e10860SSara Sharon 					u16 txq_id)
276334e10860SSara Sharon {
276434e10860SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm))
276534e10860SSara Sharon 		return;
276634e10860SSara Sharon 
276734e10860SSara Sharon 	spin_lock_bh(&mvm->queue_info_lock);
276834e10860SSara Sharon 	/*
276934e10860SSara Sharon 	 * The TXQ is marked as reserved only if no traffic came through yet
277034e10860SSara Sharon 	 * This means no traffic has been sent on this TID (agg'd or not), so
277134e10860SSara Sharon 	 * we no longer have use for the queue. Since it hasn't even been
277234e10860SSara Sharon 	 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
277334e10860SSara Sharon 	 * free.
277434e10860SSara Sharon 	 */
277534e10860SSara Sharon 	if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
277634e10860SSara Sharon 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
277734e10860SSara Sharon 
277834e10860SSara Sharon 	spin_unlock_bh(&mvm->queue_info_lock);
277934e10860SSara Sharon }
278034e10860SSara Sharon 
2781e705c121SKalle Valo int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2782e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid)
2783e705c121SKalle Valo {
2784e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2785e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2786e705c121SKalle Valo 	u16 txq_id;
2787e705c121SKalle Valo 	int err;
2788e705c121SKalle Valo 
2789e705c121SKalle Valo 	/*
2790e705c121SKalle Valo 	 * If mac80211 is cleaning its state, then say that we finished since
2791e705c121SKalle Valo 	 * our state has been cleared anyway.
2792e705c121SKalle Valo 	 */
2793e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2794e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2795e705c121SKalle Valo 		return 0;
2796e705c121SKalle Valo 	}
2797e705c121SKalle Valo 
2798e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2799e705c121SKalle Valo 
2800e705c121SKalle Valo 	txq_id = tid_data->txq_id;
2801e705c121SKalle Valo 
2802e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
2803e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
2804e705c121SKalle Valo 
2805e705c121SKalle Valo 	mvmsta->agg_tids &= ~BIT(tid);
2806e705c121SKalle Valo 
280734e10860SSara Sharon 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, txq_id);
2808e705c121SKalle Valo 
2809e705c121SKalle Valo 	switch (tid_data->state) {
2810e705c121SKalle Valo 	case IWL_AGG_ON:
2811e705c121SKalle Valo 		tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2812e705c121SKalle Valo 
2813e705c121SKalle Valo 		IWL_DEBUG_TX_QUEUES(mvm,
2814e705c121SKalle Valo 				    "ssn = %d, next_recl = %d\n",
2815e705c121SKalle Valo 				    tid_data->ssn, tid_data->next_reclaimed);
2816e705c121SKalle Valo 
2817664e968bSLiad Kaufman 		/*
2818664e968bSLiad Kaufman 		 * There are still packets for this RA / TID in the HW.
2819664e968bSLiad Kaufman 		 * Not relevant for DQA mode, since there is no need to disable
2820664e968bSLiad Kaufman 		 * the queue.
2821664e968bSLiad Kaufman 		 */
2822664e968bSLiad Kaufman 		if (!iwl_mvm_is_dqa_supported(mvm) &&
2823664e968bSLiad Kaufman 		    tid_data->ssn != tid_data->next_reclaimed) {
2824e705c121SKalle Valo 			tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
2825e705c121SKalle Valo 			err = 0;
2826e705c121SKalle Valo 			break;
2827e705c121SKalle Valo 		}
2828e705c121SKalle Valo 
2829e705c121SKalle Valo 		tid_data->ssn = 0xffff;
2830e705c121SKalle Valo 		tid_data->state = IWL_AGG_OFF;
2831e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
2832e705c121SKalle Valo 
2833e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2834e705c121SKalle Valo 
2835e705c121SKalle Valo 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2836e705c121SKalle Valo 
2837cf961e16SLiad Kaufman 		if (!iwl_mvm_is_dqa_supported(mvm)) {
2838cf961e16SLiad Kaufman 			int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2839cf961e16SLiad Kaufman 
2840cf961e16SLiad Kaufman 			iwl_mvm_disable_txq(mvm, txq_id, mac_queue, tid, 0);
2841cf961e16SLiad Kaufman 		}
2842e705c121SKalle Valo 		return 0;
2843e705c121SKalle Valo 	case IWL_AGG_STARTING:
2844e705c121SKalle Valo 	case IWL_EMPTYING_HW_QUEUE_ADDBA:
2845e705c121SKalle Valo 		/*
2846e705c121SKalle Valo 		 * The agg session has been stopped before it was set up. This
2847e705c121SKalle Valo 		 * can happen when the AddBA timer times out for example.
2848e705c121SKalle Valo 		 */
2849e705c121SKalle Valo 
2850e705c121SKalle Valo 		/* No barriers since we are under mutex */
2851e705c121SKalle Valo 		lockdep_assert_held(&mvm->mutex);
2852e705c121SKalle Valo 
2853e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2854e705c121SKalle Valo 		tid_data->state = IWL_AGG_OFF;
2855e705c121SKalle Valo 		err = 0;
2856e705c121SKalle Valo 		break;
2857e705c121SKalle Valo 	default:
2858e705c121SKalle Valo 		IWL_ERR(mvm,
2859e705c121SKalle Valo 			"Stopping AGG while state not ON or starting for %d on %d (%d)\n",
2860e705c121SKalle Valo 			mvmsta->sta_id, tid, tid_data->state);
2861e705c121SKalle Valo 		IWL_ERR(mvm,
2862e705c121SKalle Valo 			"\ttid_data->txq_id = %d\n", tid_data->txq_id);
2863e705c121SKalle Valo 		err = -EINVAL;
2864e705c121SKalle Valo 	}
2865e705c121SKalle Valo 
2866e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2867e705c121SKalle Valo 
2868e705c121SKalle Valo 	return err;
2869e705c121SKalle Valo }
2870e705c121SKalle Valo 
2871e705c121SKalle Valo int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2872e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid)
2873e705c121SKalle Valo {
2874e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2875e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2876e705c121SKalle Valo 	u16 txq_id;
2877e705c121SKalle Valo 	enum iwl_mvm_agg_state old_state;
2878e705c121SKalle Valo 
2879e705c121SKalle Valo 	/*
2880e705c121SKalle Valo 	 * First set the agg state to OFF to avoid calling
2881e705c121SKalle Valo 	 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
2882e705c121SKalle Valo 	 */
2883e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2884e705c121SKalle Valo 	txq_id = tid_data->txq_id;
2885e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
2886e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
2887e705c121SKalle Valo 	old_state = tid_data->state;
2888e705c121SKalle Valo 	tid_data->state = IWL_AGG_OFF;
2889e705c121SKalle Valo 	mvmsta->agg_tids &= ~BIT(tid);
2890e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2891e705c121SKalle Valo 
289234e10860SSara Sharon 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, txq_id);
2893e705c121SKalle Valo 
2894e705c121SKalle Valo 	if (old_state >= IWL_AGG_ON) {
2895e705c121SKalle Valo 		iwl_mvm_drain_sta(mvm, mvmsta, true);
2896e705c121SKalle Valo 		if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
2897e705c121SKalle Valo 			IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
2898d6d517b7SSara Sharon 
2899d6d517b7SSara Sharon 		if (iwl_mvm_has_new_tx_api(mvm))
2900d6d517b7SSara Sharon 			iwl_trans_wait_txq_empty(mvm->trans, txq_id);
2901d6d517b7SSara Sharon 
2902d6d517b7SSara Sharon 		else
29030b90964aSSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
2904d6d517b7SSara Sharon 
2905e705c121SKalle Valo 		iwl_mvm_drain_sta(mvm, mvmsta, false);
2906e705c121SKalle Valo 
2907e705c121SKalle Valo 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2908e705c121SKalle Valo 
2909cf961e16SLiad Kaufman 		if (!iwl_mvm_is_dqa_supported(mvm)) {
2910cf961e16SLiad Kaufman 			int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2911cf961e16SLiad Kaufman 
2912cf961e16SLiad Kaufman 			iwl_mvm_disable_txq(mvm, tid_data->txq_id, mac_queue,
2913cf961e16SLiad Kaufman 					    tid, 0);
2914cf961e16SLiad Kaufman 		}
2915e705c121SKalle Valo 	}
2916e705c121SKalle Valo 
2917e705c121SKalle Valo 	return 0;
2918e705c121SKalle Valo }
2919e705c121SKalle Valo 
2920e705c121SKalle Valo static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
2921e705c121SKalle Valo {
2922e705c121SKalle Valo 	int i, max = -1, max_offs = -1;
2923e705c121SKalle Valo 
2924e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2925e705c121SKalle Valo 
2926e705c121SKalle Valo 	/* Pick the unused key offset with the highest 'deleted'
2927e705c121SKalle Valo 	 * counter. Every time a key is deleted, all the counters
2928e705c121SKalle Valo 	 * are incremented and the one that was just deleted is
2929e705c121SKalle Valo 	 * reset to zero. Thus, the highest counter is the one
2930e705c121SKalle Valo 	 * that was deleted longest ago. Pick that one.
2931e705c121SKalle Valo 	 */
2932e705c121SKalle Valo 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2933e705c121SKalle Valo 		if (test_bit(i, mvm->fw_key_table))
2934e705c121SKalle Valo 			continue;
2935e705c121SKalle Valo 		if (mvm->fw_key_deleted[i] > max) {
2936e705c121SKalle Valo 			max = mvm->fw_key_deleted[i];
2937e705c121SKalle Valo 			max_offs = i;
2938e705c121SKalle Valo 		}
2939e705c121SKalle Valo 	}
2940e705c121SKalle Valo 
2941e705c121SKalle Valo 	if (max_offs < 0)
2942e705c121SKalle Valo 		return STA_KEY_IDX_INVALID;
2943e705c121SKalle Valo 
2944e705c121SKalle Valo 	return max_offs;
2945e705c121SKalle Valo }
2946e705c121SKalle Valo 
29475f7a1847SJohannes Berg static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
29484615fd15SEmmanuel Grumbach 					       struct ieee80211_vif *vif,
2949e705c121SKalle Valo 					       struct ieee80211_sta *sta)
2950e705c121SKalle Valo {
2951e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2952e705c121SKalle Valo 
29535f7a1847SJohannes Berg 	if (sta)
29545f7a1847SJohannes Berg 		return iwl_mvm_sta_from_mac80211(sta);
2955e705c121SKalle Valo 
2956e705c121SKalle Valo 	/*
2957e705c121SKalle Valo 	 * The device expects GTKs for station interfaces to be
2958e705c121SKalle Valo 	 * installed as GTKs for the AP station. If we have no
2959e705c121SKalle Valo 	 * station ID, then use AP's station ID.
2960e705c121SKalle Valo 	 */
2961e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
29620ae98812SSara Sharon 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
29634615fd15SEmmanuel Grumbach 		u8 sta_id = mvmvif->ap_sta_id;
29644615fd15SEmmanuel Grumbach 
29657d6a1ab6SEmmanuel Grumbach 		sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
29667d6a1ab6SEmmanuel Grumbach 					    lockdep_is_held(&mvm->mutex));
29677d6a1ab6SEmmanuel Grumbach 
29684615fd15SEmmanuel Grumbach 		/*
29694615fd15SEmmanuel Grumbach 		 * It is possible that the 'sta' parameter is NULL,
29704615fd15SEmmanuel Grumbach 		 * for example when a GTK is removed - the sta_id will then
29714615fd15SEmmanuel Grumbach 		 * be the AP ID, and no station was passed by mac80211.
29724615fd15SEmmanuel Grumbach 		 */
29737d6a1ab6SEmmanuel Grumbach 		if (IS_ERR_OR_NULL(sta))
29747d6a1ab6SEmmanuel Grumbach 			return NULL;
29757d6a1ab6SEmmanuel Grumbach 
29767d6a1ab6SEmmanuel Grumbach 		return iwl_mvm_sta_from_mac80211(sta);
29774615fd15SEmmanuel Grumbach 	}
2978e705c121SKalle Valo 
29795f7a1847SJohannes Berg 	return NULL;
2980e705c121SKalle Valo }
2981e705c121SKalle Valo 
2982e705c121SKalle Valo static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
298385aeb58cSDavid Spinadel 				u32 sta_id,
298445c458b4SSara Sharon 				struct ieee80211_key_conf *key, bool mcast,
29854615fd15SEmmanuel Grumbach 				u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
29864615fd15SEmmanuel Grumbach 				u8 key_offset)
2987e705c121SKalle Valo {
298845c458b4SSara Sharon 	union {
298945c458b4SSara Sharon 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
299045c458b4SSara Sharon 		struct iwl_mvm_add_sta_key_cmd cmd;
299145c458b4SSara Sharon 	} u = {};
2992e705c121SKalle Valo 	__le16 key_flags;
2993e705c121SKalle Valo 	int ret;
2994e705c121SKalle Valo 	u32 status;
2995e705c121SKalle Valo 	u16 keyidx;
299645c458b4SSara Sharon 	u64 pn = 0;
299745c458b4SSara Sharon 	int i, size;
299845c458b4SSara Sharon 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
299945c458b4SSara Sharon 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3000e705c121SKalle Valo 
300185aeb58cSDavid Spinadel 	if (sta_id == IWL_MVM_INVALID_STA)
300285aeb58cSDavid Spinadel 		return -EINVAL;
300385aeb58cSDavid Spinadel 
300445c458b4SSara Sharon 	keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
3005e705c121SKalle Valo 		 STA_KEY_FLG_KEYID_MSK;
3006e705c121SKalle Valo 	key_flags = cpu_to_le16(keyidx);
3007e705c121SKalle Valo 	key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3008e705c121SKalle Valo 
300945c458b4SSara Sharon 	switch (key->cipher) {
3010e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
3011e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
301245c458b4SSara Sharon 		if (new_api) {
301345c458b4SSara Sharon 			memcpy((void *)&u.cmd.tx_mic_key,
301445c458b4SSara Sharon 			       &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
301545c458b4SSara Sharon 			       IWL_MIC_KEY_SIZE);
301645c458b4SSara Sharon 
301745c458b4SSara Sharon 			memcpy((void *)&u.cmd.rx_mic_key,
301845c458b4SSara Sharon 			       &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
301945c458b4SSara Sharon 			       IWL_MIC_KEY_SIZE);
302045c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
302145c458b4SSara Sharon 
302245c458b4SSara Sharon 		} else {
302345c458b4SSara Sharon 			u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3024e705c121SKalle Valo 			for (i = 0; i < 5; i++)
302545c458b4SSara Sharon 				u.cmd_v1.tkip_rx_ttak[i] =
302645c458b4SSara Sharon 					cpu_to_le16(tkip_p1k[i]);
302745c458b4SSara Sharon 		}
302845c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3029e705c121SKalle Valo 		break;
3030e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
3031e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
303245c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
303345c458b4SSara Sharon 		if (new_api)
303445c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
3035e705c121SKalle Valo 		break;
3036e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
3037e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
3038e705c121SKalle Valo 		/* fall through */
3039e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
3040e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
304145c458b4SSara Sharon 		memcpy(u.cmd.common.key + 3, key->key, key->keylen);
3042e705c121SKalle Valo 		break;
30432a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
30442a53d166SAyala Beker 		key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
30452a53d166SAyala Beker 		/* fall through */
30462a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
30472a53d166SAyala Beker 		key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
304845c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
304945c458b4SSara Sharon 		if (new_api)
305045c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
30512a53d166SAyala Beker 		break;
3052e705c121SKalle Valo 	default:
3053e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
305445c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3055e705c121SKalle Valo 	}
3056e705c121SKalle Valo 
3057e705c121SKalle Valo 	if (mcast)
3058e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3059e705c121SKalle Valo 
306045c458b4SSara Sharon 	u.cmd.common.key_offset = key_offset;
306145c458b4SSara Sharon 	u.cmd.common.key_flags = key_flags;
306285aeb58cSDavid Spinadel 	u.cmd.common.sta_id = sta_id;
306345c458b4SSara Sharon 
306445c458b4SSara Sharon 	if (new_api) {
306545c458b4SSara Sharon 		u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
306645c458b4SSara Sharon 		size = sizeof(u.cmd);
306745c458b4SSara Sharon 	} else {
306845c458b4SSara Sharon 		size = sizeof(u.cmd_v1);
306945c458b4SSara Sharon 	}
3070e705c121SKalle Valo 
3071e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
3072e705c121SKalle Valo 	if (cmd_flags & CMD_ASYNC)
307345c458b4SSara Sharon 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
307445c458b4SSara Sharon 					   &u.cmd);
3075e705c121SKalle Valo 	else
307645c458b4SSara Sharon 		ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
307745c458b4SSara Sharon 						  &u.cmd, &status);
3078e705c121SKalle Valo 
3079e705c121SKalle Valo 	switch (status) {
3080e705c121SKalle Valo 	case ADD_STA_SUCCESS:
3081e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3082e705c121SKalle Valo 		break;
3083e705c121SKalle Valo 	default:
3084e705c121SKalle Valo 		ret = -EIO;
3085e705c121SKalle Valo 		IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3086e705c121SKalle Valo 		break;
3087e705c121SKalle Valo 	}
3088e705c121SKalle Valo 
3089e705c121SKalle Valo 	return ret;
3090e705c121SKalle Valo }
3091e705c121SKalle Valo 
3092e705c121SKalle Valo static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3093e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
3094e705c121SKalle Valo 				 u8 sta_id, bool remove_key)
3095e705c121SKalle Valo {
3096e705c121SKalle Valo 	struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3097e705c121SKalle Valo 
3098e705c121SKalle Valo 	/* verify the key details match the required command's expectations */
30998e160ab8SAyala Beker 	if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
31008e160ab8SAyala Beker 		    (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
31018e160ab8SAyala Beker 		    (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
31028e160ab8SAyala Beker 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
31038e160ab8SAyala Beker 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
31048e160ab8SAyala Beker 		return -EINVAL;
31058e160ab8SAyala Beker 
31068e160ab8SAyala Beker 	if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
31078e160ab8SAyala Beker 		    keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3108e705c121SKalle Valo 		return -EINVAL;
3109e705c121SKalle Valo 
3110e705c121SKalle Valo 	igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3111e705c121SKalle Valo 	igtk_cmd.sta_id = cpu_to_le32(sta_id);
3112e705c121SKalle Valo 
3113e705c121SKalle Valo 	if (remove_key) {
3114e705c121SKalle Valo 		igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3115e705c121SKalle Valo 	} else {
3116e705c121SKalle Valo 		struct ieee80211_key_seq seq;
3117e705c121SKalle Valo 		const u8 *pn;
3118e705c121SKalle Valo 
3119aa950524SAyala Beker 		switch (keyconf->cipher) {
3120aa950524SAyala Beker 		case WLAN_CIPHER_SUITE_AES_CMAC:
3121aa950524SAyala Beker 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3122aa950524SAyala Beker 			break;
31238e160ab8SAyala Beker 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
31248e160ab8SAyala Beker 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
31258e160ab8SAyala Beker 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
31268e160ab8SAyala Beker 			break;
3127aa950524SAyala Beker 		default:
3128aa950524SAyala Beker 			return -EINVAL;
3129aa950524SAyala Beker 		}
3130aa950524SAyala Beker 
31318e160ab8SAyala Beker 		memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
31328e160ab8SAyala Beker 		if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
31338e160ab8SAyala Beker 			igtk_cmd.ctrl_flags |=
31348e160ab8SAyala Beker 				cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3135e705c121SKalle Valo 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3136e705c121SKalle Valo 		pn = seq.aes_cmac.pn;
3137e705c121SKalle Valo 		igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3138e705c121SKalle Valo 						       ((u64) pn[4] << 8) |
3139e705c121SKalle Valo 						       ((u64) pn[3] << 16) |
3140e705c121SKalle Valo 						       ((u64) pn[2] << 24) |
3141e705c121SKalle Valo 						       ((u64) pn[1] << 32) |
3142e705c121SKalle Valo 						       ((u64) pn[0] << 40));
3143e705c121SKalle Valo 	}
3144e705c121SKalle Valo 
3145e705c121SKalle Valo 	IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
3146e705c121SKalle Valo 		       remove_key ? "removing" : "installing",
3147e705c121SKalle Valo 		       igtk_cmd.sta_id);
3148e705c121SKalle Valo 
31498e160ab8SAyala Beker 	if (!iwl_mvm_has_new_rx_api(mvm)) {
31508e160ab8SAyala Beker 		struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
31518e160ab8SAyala Beker 			.ctrl_flags = igtk_cmd.ctrl_flags,
31528e160ab8SAyala Beker 			.key_id = igtk_cmd.key_id,
31538e160ab8SAyala Beker 			.sta_id = igtk_cmd.sta_id,
31548e160ab8SAyala Beker 			.receive_seq_cnt = igtk_cmd.receive_seq_cnt
31558e160ab8SAyala Beker 		};
31568e160ab8SAyala Beker 
31578e160ab8SAyala Beker 		memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
31588e160ab8SAyala Beker 		       ARRAY_SIZE(igtk_cmd_v1.igtk));
31598e160ab8SAyala Beker 		return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
31608e160ab8SAyala Beker 					    sizeof(igtk_cmd_v1), &igtk_cmd_v1);
31618e160ab8SAyala Beker 	}
3162e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3163e705c121SKalle Valo 				    sizeof(igtk_cmd), &igtk_cmd);
3164e705c121SKalle Valo }
3165e705c121SKalle Valo 
3166e705c121SKalle Valo 
3167e705c121SKalle Valo static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3168e705c121SKalle Valo 				       struct ieee80211_vif *vif,
3169e705c121SKalle Valo 				       struct ieee80211_sta *sta)
3170e705c121SKalle Valo {
3171e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3172e705c121SKalle Valo 
3173e705c121SKalle Valo 	if (sta)
3174e705c121SKalle Valo 		return sta->addr;
3175e705c121SKalle Valo 
3176e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
31770ae98812SSara Sharon 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3178e705c121SKalle Valo 		u8 sta_id = mvmvif->ap_sta_id;
3179e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3180e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
3181e705c121SKalle Valo 		return sta->addr;
3182e705c121SKalle Valo 	}
3183e705c121SKalle Valo 
3184e705c121SKalle Valo 
3185e705c121SKalle Valo 	return NULL;
3186e705c121SKalle Valo }
3187e705c121SKalle Valo 
3188e705c121SKalle Valo static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3189e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3190e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3191e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
31924615fd15SEmmanuel Grumbach 				 u8 key_offset,
3193e705c121SKalle Valo 				 bool mcast)
3194e705c121SKalle Valo {
3195e705c121SKalle Valo 	int ret;
3196e705c121SKalle Valo 	const u8 *addr;
3197e705c121SKalle Valo 	struct ieee80211_key_seq seq;
3198e705c121SKalle Valo 	u16 p1k[5];
319985aeb58cSDavid Spinadel 	u32 sta_id;
320085aeb58cSDavid Spinadel 
320185aeb58cSDavid Spinadel 	if (sta) {
320285aeb58cSDavid Spinadel 		struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
320385aeb58cSDavid Spinadel 
320485aeb58cSDavid Spinadel 		sta_id = mvm_sta->sta_id;
320585aeb58cSDavid Spinadel 	} else if (vif->type == NL80211_IFTYPE_AP &&
320685aeb58cSDavid Spinadel 		   !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
320785aeb58cSDavid Spinadel 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
320885aeb58cSDavid Spinadel 
320985aeb58cSDavid Spinadel 		sta_id = mvmvif->mcast_sta.sta_id;
321085aeb58cSDavid Spinadel 	} else {
321185aeb58cSDavid Spinadel 		IWL_ERR(mvm, "Failed to find station id\n");
321285aeb58cSDavid Spinadel 		return -EINVAL;
321385aeb58cSDavid Spinadel 	}
3214e705c121SKalle Valo 
3215e705c121SKalle Valo 	switch (keyconf->cipher) {
3216e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
321785aeb58cSDavid Spinadel 		if (vif->type == NL80211_IFTYPE_AP) {
321885aeb58cSDavid Spinadel 			ret = -EINVAL;
321985aeb58cSDavid Spinadel 			break;
322085aeb58cSDavid Spinadel 		}
3221e705c121SKalle Valo 		addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3222e705c121SKalle Valo 		/* get phase 1 key from mac80211 */
3223e705c121SKalle Valo 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3224e705c121SKalle Valo 		ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
322585aeb58cSDavid Spinadel 		ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
32264615fd15SEmmanuel Grumbach 					   seq.tkip.iv32, p1k, 0, key_offset);
3227e705c121SKalle Valo 		break;
3228e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
3229e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
3230e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
32312a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
32322a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
323385aeb58cSDavid Spinadel 		ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
32344615fd15SEmmanuel Grumbach 					   0, NULL, 0, key_offset);
3235e705c121SKalle Valo 		break;
3236e705c121SKalle Valo 	default:
323785aeb58cSDavid Spinadel 		ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
32384615fd15SEmmanuel Grumbach 					   0, NULL, 0, key_offset);
3239e705c121SKalle Valo 	}
3240e705c121SKalle Valo 
3241e705c121SKalle Valo 	return ret;
3242e705c121SKalle Valo }
3243e705c121SKalle Valo 
3244e705c121SKalle Valo static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
3245e705c121SKalle Valo 				    struct ieee80211_key_conf *keyconf,
3246e705c121SKalle Valo 				    bool mcast)
3247e705c121SKalle Valo {
324845c458b4SSara Sharon 	union {
324945c458b4SSara Sharon 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
325045c458b4SSara Sharon 		struct iwl_mvm_add_sta_key_cmd cmd;
325145c458b4SSara Sharon 	} u = {};
325245c458b4SSara Sharon 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
325345c458b4SSara Sharon 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3254e705c121SKalle Valo 	__le16 key_flags;
325545c458b4SSara Sharon 	int ret, size;
3256e705c121SKalle Valo 	u32 status;
3257e705c121SKalle Valo 
325885aeb58cSDavid Spinadel 	if (sta_id == IWL_MVM_INVALID_STA)
325985aeb58cSDavid Spinadel 		return -EINVAL;
326085aeb58cSDavid Spinadel 
3261e705c121SKalle Valo 	key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
3262e705c121SKalle Valo 				 STA_KEY_FLG_KEYID_MSK);
3263e705c121SKalle Valo 	key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
3264e705c121SKalle Valo 	key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
3265e705c121SKalle Valo 
3266e705c121SKalle Valo 	if (mcast)
3267e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3268e705c121SKalle Valo 
326945c458b4SSara Sharon 	/*
327045c458b4SSara Sharon 	 * The fields assigned here are in the same location at the start
327145c458b4SSara Sharon 	 * of the command, so we can do this union trick.
327245c458b4SSara Sharon 	 */
327345c458b4SSara Sharon 	u.cmd.common.key_flags = key_flags;
327445c458b4SSara Sharon 	u.cmd.common.key_offset = keyconf->hw_key_idx;
327545c458b4SSara Sharon 	u.cmd.common.sta_id = sta_id;
327645c458b4SSara Sharon 
327745c458b4SSara Sharon 	size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
3278e705c121SKalle Valo 
3279e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
328045c458b4SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
328145c458b4SSara Sharon 					  &status);
3282e705c121SKalle Valo 
3283e705c121SKalle Valo 	switch (status) {
3284e705c121SKalle Valo 	case ADD_STA_SUCCESS:
3285e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
3286e705c121SKalle Valo 		break;
3287e705c121SKalle Valo 	default:
3288e705c121SKalle Valo 		ret = -EIO;
3289e705c121SKalle Valo 		IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
3290e705c121SKalle Valo 		break;
3291e705c121SKalle Valo 	}
3292e705c121SKalle Valo 
3293e705c121SKalle Valo 	return ret;
3294e705c121SKalle Valo }
3295e705c121SKalle Valo 
3296e705c121SKalle Valo int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3297e705c121SKalle Valo 			struct ieee80211_vif *vif,
3298e705c121SKalle Valo 			struct ieee80211_sta *sta,
3299e705c121SKalle Valo 			struct ieee80211_key_conf *keyconf,
33004615fd15SEmmanuel Grumbach 			u8 key_offset)
3301e705c121SKalle Valo {
3302e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
33035f7a1847SJohannes Berg 	struct iwl_mvm_sta *mvm_sta;
330485aeb58cSDavid Spinadel 	u8 sta_id = IWL_MVM_INVALID_STA;
3305e705c121SKalle Valo 	int ret;
3306e705c121SKalle Valo 	static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3307e705c121SKalle Valo 
3308e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3309e705c121SKalle Valo 
331085aeb58cSDavid Spinadel 	if (vif->type != NL80211_IFTYPE_AP ||
331185aeb58cSDavid Spinadel 	    keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3312e705c121SKalle Valo 		/* Get the station id from the mvm local station table */
33135f7a1847SJohannes Berg 		mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
33145f7a1847SJohannes Berg 		if (!mvm_sta) {
33155f7a1847SJohannes Berg 			IWL_ERR(mvm, "Failed to find station\n");
3316e705c121SKalle Valo 			return -EINVAL;
3317e705c121SKalle Valo 		}
33185f7a1847SJohannes Berg 		sta_id = mvm_sta->sta_id;
3319e705c121SKalle Valo 
33208e160ab8SAyala Beker 		if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
33218e160ab8SAyala Beker 		    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
33228e160ab8SAyala Beker 		    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
332385aeb58cSDavid Spinadel 			ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id,
332485aeb58cSDavid Spinadel 						    false);
3325e705c121SKalle Valo 			goto end;
3326e705c121SKalle Valo 		}
3327e705c121SKalle Valo 
3328e705c121SKalle Valo 		/*
3329e705c121SKalle Valo 		 * It is possible that the 'sta' parameter is NULL, and thus
333085aeb58cSDavid Spinadel 		 * there is a need to retrieve  the sta from the local station
333185aeb58cSDavid Spinadel 		 * table.
3332e705c121SKalle Valo 		 */
3333e705c121SKalle Valo 		if (!sta) {
333485aeb58cSDavid Spinadel 			sta = rcu_dereference_protected(
333585aeb58cSDavid Spinadel 				mvm->fw_id_to_mac_id[sta_id],
3336e705c121SKalle Valo 				lockdep_is_held(&mvm->mutex));
3337e705c121SKalle Valo 			if (IS_ERR_OR_NULL(sta)) {
3338e705c121SKalle Valo 				IWL_ERR(mvm, "Invalid station id\n");
3339e705c121SKalle Valo 				return -EINVAL;
3340e705c121SKalle Valo 			}
3341e705c121SKalle Valo 		}
3342e705c121SKalle Valo 
3343e705c121SKalle Valo 		if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3344e705c121SKalle Valo 			return -EINVAL;
334585aeb58cSDavid Spinadel 	}
3346e705c121SKalle Valo 
33474615fd15SEmmanuel Grumbach 	/* If the key_offset is not pre-assigned, we need to find a
33484615fd15SEmmanuel Grumbach 	 * new offset to use.  In normal cases, the offset is not
33494615fd15SEmmanuel Grumbach 	 * pre-assigned, but during HW_RESTART we want to reuse the
33504615fd15SEmmanuel Grumbach 	 * same indices, so we pass them when this function is called.
33514615fd15SEmmanuel Grumbach 	 *
33524615fd15SEmmanuel Grumbach 	 * In D3 entry, we need to hardcoded the indices (because the
33534615fd15SEmmanuel Grumbach 	 * firmware hardcodes the PTK offset to 0).  In this case, we
33544615fd15SEmmanuel Grumbach 	 * need to make sure we don't overwrite the hw_key_idx in the
33554615fd15SEmmanuel Grumbach 	 * keyconf structure, because otherwise we cannot configure
33564615fd15SEmmanuel Grumbach 	 * the original ones back when resuming.
3357e705c121SKalle Valo 	 */
33584615fd15SEmmanuel Grumbach 	if (key_offset == STA_KEY_IDX_INVALID) {
33594615fd15SEmmanuel Grumbach 		key_offset  = iwl_mvm_set_fw_key_idx(mvm);
33604615fd15SEmmanuel Grumbach 		if (key_offset == STA_KEY_IDX_INVALID)
3361e705c121SKalle Valo 			return -ENOSPC;
33624615fd15SEmmanuel Grumbach 		keyconf->hw_key_idx = key_offset;
3363e705c121SKalle Valo 	}
3364e705c121SKalle Valo 
33654615fd15SEmmanuel Grumbach 	ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
33669c3deeb5SLuca Coelho 	if (ret)
3367e705c121SKalle Valo 		goto end;
3368e705c121SKalle Valo 
3369e705c121SKalle Valo 	/*
3370e705c121SKalle Valo 	 * For WEP, the same key is used for multicast and unicast. Upload it
3371e705c121SKalle Valo 	 * again, using the same key offset, and now pointing the other one
3372e705c121SKalle Valo 	 * to the same key slot (offset).
3373e705c121SKalle Valo 	 * If this fails, remove the original as well.
3374e705c121SKalle Valo 	 */
337585aeb58cSDavid Spinadel 	if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
337685aeb58cSDavid Spinadel 	     keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
337785aeb58cSDavid Spinadel 	    sta) {
33784615fd15SEmmanuel Grumbach 		ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
33794615fd15SEmmanuel Grumbach 					    key_offset, !mcast);
3380e705c121SKalle Valo 		if (ret) {
3381e705c121SKalle Valo 			__iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
33829c3deeb5SLuca Coelho 			goto end;
3383e705c121SKalle Valo 		}
3384e705c121SKalle Valo 	}
3385e705c121SKalle Valo 
33869c3deeb5SLuca Coelho 	__set_bit(key_offset, mvm->fw_key_table);
33879c3deeb5SLuca Coelho 
3388e705c121SKalle Valo end:
3389e705c121SKalle Valo 	IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3390e705c121SKalle Valo 		      keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3391e705c121SKalle Valo 		      sta ? sta->addr : zero_addr, ret);
3392e705c121SKalle Valo 	return ret;
3393e705c121SKalle Valo }
3394e705c121SKalle Valo 
3395e705c121SKalle Valo int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3396e705c121SKalle Valo 			   struct ieee80211_vif *vif,
3397e705c121SKalle Valo 			   struct ieee80211_sta *sta,
3398e705c121SKalle Valo 			   struct ieee80211_key_conf *keyconf)
3399e705c121SKalle Valo {
3400e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
34015f7a1847SJohannes Berg 	struct iwl_mvm_sta *mvm_sta;
34020ae98812SSara Sharon 	u8 sta_id = IWL_MVM_INVALID_STA;
3403e705c121SKalle Valo 	int ret, i;
3404e705c121SKalle Valo 
3405e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3406e705c121SKalle Valo 
34075f7a1847SJohannes Berg 	/* Get the station from the mvm local station table */
34085f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
340971793b7dSLuca Coelho 	if (mvm_sta)
3410cd4d23c1SIlan Peer 		sta_id = mvm_sta->sta_id;
341185aeb58cSDavid Spinadel 	else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
341285aeb58cSDavid Spinadel 		sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
341385aeb58cSDavid Spinadel 
3414e705c121SKalle Valo 
3415e705c121SKalle Valo 	IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3416e705c121SKalle Valo 		      keyconf->keyidx, sta_id);
3417e705c121SKalle Valo 
341871793b7dSLuca Coelho 	if (mvm_sta && (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
34198e160ab8SAyala Beker 			keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
342071793b7dSLuca Coelho 			keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256))
3421e705c121SKalle Valo 		return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3422e705c121SKalle Valo 
3423e705c121SKalle Valo 	if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3424e705c121SKalle Valo 		IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3425e705c121SKalle Valo 			keyconf->hw_key_idx);
3426e705c121SKalle Valo 		return -ENOENT;
3427e705c121SKalle Valo 	}
3428e705c121SKalle Valo 
3429e705c121SKalle Valo 	/* track which key was deleted last */
3430e705c121SKalle Valo 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3431e705c121SKalle Valo 		if (mvm->fw_key_deleted[i] < U8_MAX)
3432e705c121SKalle Valo 			mvm->fw_key_deleted[i]++;
3433e705c121SKalle Valo 	}
3434e705c121SKalle Valo 	mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3435e705c121SKalle Valo 
343685aeb58cSDavid Spinadel 	if (sta && !mvm_sta) {
3437e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3438e705c121SKalle Valo 		return 0;
3439e705c121SKalle Valo 	}
3440e705c121SKalle Valo 
3441e705c121SKalle Valo 	ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3442e705c121SKalle Valo 	if (ret)
3443e705c121SKalle Valo 		return ret;
3444e705c121SKalle Valo 
3445e705c121SKalle Valo 	/* delete WEP key twice to get rid of (now useless) offset */
3446e705c121SKalle Valo 	if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3447e705c121SKalle Valo 	    keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3448e705c121SKalle Valo 		ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3449e705c121SKalle Valo 
3450e705c121SKalle Valo 	return ret;
3451e705c121SKalle Valo }
3452e705c121SKalle Valo 
3453e705c121SKalle Valo void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3454e705c121SKalle Valo 			     struct ieee80211_vif *vif,
3455e705c121SKalle Valo 			     struct ieee80211_key_conf *keyconf,
3456e705c121SKalle Valo 			     struct ieee80211_sta *sta, u32 iv32,
3457e705c121SKalle Valo 			     u16 *phase1key)
3458e705c121SKalle Valo {
3459e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta;
3460e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3461e705c121SKalle Valo 
3462e705c121SKalle Valo 	rcu_read_lock();
3463e705c121SKalle Valo 
34645f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
34655f7a1847SJohannes Berg 	if (WARN_ON_ONCE(!mvm_sta))
346612f17211SEmmanuel Grumbach 		goto unlock;
346785aeb58cSDavid Spinadel 	iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
34684615fd15SEmmanuel Grumbach 			     iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
346912f17211SEmmanuel Grumbach 
347012f17211SEmmanuel Grumbach  unlock:
3471e705c121SKalle Valo 	rcu_read_unlock();
3472e705c121SKalle Valo }
3473e705c121SKalle Valo 
3474e705c121SKalle Valo void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3475e705c121SKalle Valo 				struct ieee80211_sta *sta)
3476e705c121SKalle Valo {
3477e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3478e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3479e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3480e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3481e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_PS),
3482e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3483e705c121SKalle Valo 	};
3484e705c121SKalle Valo 	int ret;
3485e705c121SKalle Valo 
3486854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3487854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3488e705c121SKalle Valo 	if (ret)
3489e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3490e705c121SKalle Valo }
3491e705c121SKalle Valo 
3492e705c121SKalle Valo void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3493e705c121SKalle Valo 				       struct ieee80211_sta *sta,
3494e705c121SKalle Valo 				       enum ieee80211_frame_release_type reason,
3495e705c121SKalle Valo 				       u16 cnt, u16 tids, bool more_data,
34969a3fcf91SSara Sharon 				       bool single_sta_queue)
3497e705c121SKalle Valo {
3498e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3499e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3500e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3501e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3502e705c121SKalle Valo 		.modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3503e705c121SKalle Valo 		.sleep_tx_count = cpu_to_le16(cnt),
3504e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3505e705c121SKalle Valo 	};
3506e705c121SKalle Valo 	int tid, ret;
3507e705c121SKalle Valo 	unsigned long _tids = tids;
3508e705c121SKalle Valo 
3509e705c121SKalle Valo 	/* convert TIDs to ACs - we don't support TSPEC so that's OK
3510e705c121SKalle Valo 	 * Note that this field is reserved and unused by firmware not
3511e705c121SKalle Valo 	 * supporting GO uAPSD, so it's safe to always do this.
3512e705c121SKalle Valo 	 */
3513e705c121SKalle Valo 	for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3514e705c121SKalle Valo 		cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3515e705c121SKalle Valo 
35169a3fcf91SSara Sharon 	/* If we're releasing frames from aggregation or dqa queues then check
35179a3fcf91SSara Sharon 	 * if all the queues that we're releasing frames from, combined, have:
3518e705c121SKalle Valo 	 *  - more frames than the service period, in which case more_data
3519e705c121SKalle Valo 	 *    needs to be set
3520e705c121SKalle Valo 	 *  - fewer than 'cnt' frames, in which case we need to adjust the
3521e705c121SKalle Valo 	 *    firmware command (but do that unconditionally)
3522e705c121SKalle Valo 	 */
35239a3fcf91SSara Sharon 	if (single_sta_queue) {
3524e705c121SKalle Valo 		int remaining = cnt;
352536be0eb6SEmmanuel Grumbach 		int sleep_tx_count;
3526e705c121SKalle Valo 
3527e705c121SKalle Valo 		spin_lock_bh(&mvmsta->lock);
3528e705c121SKalle Valo 		for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3529e705c121SKalle Valo 			struct iwl_mvm_tid_data *tid_data;
3530e705c121SKalle Valo 			u16 n_queued;
3531e705c121SKalle Valo 
3532e705c121SKalle Valo 			tid_data = &mvmsta->tid_data[tid];
35339a3fcf91SSara Sharon 			if (WARN(!iwl_mvm_is_dqa_supported(mvm) &&
35349a3fcf91SSara Sharon 				 tid_data->state != IWL_AGG_ON &&
3535e705c121SKalle Valo 				 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
3536e705c121SKalle Valo 				 "TID %d state is %d\n",
3537e705c121SKalle Valo 				 tid, tid_data->state)) {
3538e705c121SKalle Valo 				spin_unlock_bh(&mvmsta->lock);
3539e705c121SKalle Valo 				ieee80211_sta_eosp(sta);
3540e705c121SKalle Valo 				return;
3541e705c121SKalle Valo 			}
3542e705c121SKalle Valo 
3543e705c121SKalle Valo 			n_queued = iwl_mvm_tid_queued(tid_data);
3544e705c121SKalle Valo 			if (n_queued > remaining) {
3545e705c121SKalle Valo 				more_data = true;
3546e705c121SKalle Valo 				remaining = 0;
3547e705c121SKalle Valo 				break;
3548e705c121SKalle Valo 			}
3549e705c121SKalle Valo 			remaining -= n_queued;
3550e705c121SKalle Valo 		}
355136be0eb6SEmmanuel Grumbach 		sleep_tx_count = cnt - remaining;
355236be0eb6SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
355336be0eb6SEmmanuel Grumbach 			mvmsta->sleep_tx_count = sleep_tx_count;
3554e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
3555e705c121SKalle Valo 
355636be0eb6SEmmanuel Grumbach 		cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3557e705c121SKalle Valo 		if (WARN_ON(cnt - remaining == 0)) {
3558e705c121SKalle Valo 			ieee80211_sta_eosp(sta);
3559e705c121SKalle Valo 			return;
3560e705c121SKalle Valo 		}
3561e705c121SKalle Valo 	}
3562e705c121SKalle Valo 
3563e705c121SKalle Valo 	/* Note: this is ignored by firmware not supporting GO uAPSD */
3564e705c121SKalle Valo 	if (more_data)
3565ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
3566e705c121SKalle Valo 
3567e705c121SKalle Valo 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3568e705c121SKalle Valo 		mvmsta->next_status_eosp = true;
3569ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
3570e705c121SKalle Valo 	} else {
3571ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
3572e705c121SKalle Valo 	}
3573e705c121SKalle Valo 
3574156f92f2SEmmanuel Grumbach 	/* block the Tx queues until the FW updated the sleep Tx count */
3575156f92f2SEmmanuel Grumbach 	iwl_trans_block_txq_ptrs(mvm->trans, true);
3576156f92f2SEmmanuel Grumbach 
3577156f92f2SEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3578156f92f2SEmmanuel Grumbach 				   CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3579854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3580e705c121SKalle Valo 	if (ret)
3581e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3582e705c121SKalle Valo }
3583e705c121SKalle Valo 
3584e705c121SKalle Valo void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3585e705c121SKalle Valo 			   struct iwl_rx_cmd_buffer *rxb)
3586e705c121SKalle Valo {
3587e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3588e705c121SKalle Valo 	struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3589e705c121SKalle Valo 	struct ieee80211_sta *sta;
3590e705c121SKalle Valo 	u32 sta_id = le32_to_cpu(notif->sta_id);
3591e705c121SKalle Valo 
3592e705c121SKalle Valo 	if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
3593e705c121SKalle Valo 		return;
3594e705c121SKalle Valo 
3595e705c121SKalle Valo 	rcu_read_lock();
3596e705c121SKalle Valo 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3597e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(sta))
3598e705c121SKalle Valo 		ieee80211_sta_eosp(sta);
3599e705c121SKalle Valo 	rcu_read_unlock();
3600e705c121SKalle Valo }
3601e705c121SKalle Valo 
3602e705c121SKalle Valo void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3603e705c121SKalle Valo 				   struct iwl_mvm_sta *mvmsta, bool disable)
3604e705c121SKalle Valo {
3605e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3606e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3607e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3608e705c121SKalle Valo 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3609e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3610e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3611e705c121SKalle Valo 	};
3612e705c121SKalle Valo 	int ret;
3613e705c121SKalle Valo 
3614854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3615854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3616e705c121SKalle Valo 	if (ret)
3617e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3618e705c121SKalle Valo }
3619e705c121SKalle Valo 
3620e705c121SKalle Valo void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3621e705c121SKalle Valo 				      struct ieee80211_sta *sta,
3622e705c121SKalle Valo 				      bool disable)
3623e705c121SKalle Valo {
3624e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3625e705c121SKalle Valo 
3626e705c121SKalle Valo 	spin_lock_bh(&mvm_sta->lock);
3627e705c121SKalle Valo 
3628e705c121SKalle Valo 	if (mvm_sta->disable_tx == disable) {
3629e705c121SKalle Valo 		spin_unlock_bh(&mvm_sta->lock);
3630e705c121SKalle Valo 		return;
3631e705c121SKalle Valo 	}
3632e705c121SKalle Valo 
3633e705c121SKalle Valo 	mvm_sta->disable_tx = disable;
3634e705c121SKalle Valo 
3635e705c121SKalle Valo 	/*
3636e705c121SKalle Valo 	 * Tell mac80211 to start/stop queuing tx for this station,
3637e705c121SKalle Valo 	 * but don't stop queuing if there are still pending frames
3638e705c121SKalle Valo 	 * for this station.
3639e705c121SKalle Valo 	 */
3640e705c121SKalle Valo 	if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
3641e705c121SKalle Valo 		ieee80211_sta_block_awake(mvm->hw, sta, disable);
3642e705c121SKalle Valo 
3643e705c121SKalle Valo 	iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3644e705c121SKalle Valo 
3645e705c121SKalle Valo 	spin_unlock_bh(&mvm_sta->lock);
3646e705c121SKalle Valo }
3647e705c121SKalle Valo 
3648ced19f26SSara Sharon static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
3649ced19f26SSara Sharon 					      struct iwl_mvm_vif *mvmvif,
3650ced19f26SSara Sharon 					      struct iwl_mvm_int_sta *sta,
3651ced19f26SSara Sharon 					      bool disable)
3652ced19f26SSara Sharon {
3653ced19f26SSara Sharon 	u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
3654ced19f26SSara Sharon 	struct iwl_mvm_add_sta_cmd cmd = {
3655ced19f26SSara Sharon 		.add_modify = STA_MODE_MODIFY,
3656ced19f26SSara Sharon 		.sta_id = sta->sta_id,
3657ced19f26SSara Sharon 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3658ced19f26SSara Sharon 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3659ced19f26SSara Sharon 		.mac_id_n_color = cpu_to_le32(id),
3660ced19f26SSara Sharon 	};
3661ced19f26SSara Sharon 	int ret;
3662ced19f26SSara Sharon 
3663ced19f26SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, 0,
3664ced19f26SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3665ced19f26SSara Sharon 	if (ret)
3666ced19f26SSara Sharon 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3667ced19f26SSara Sharon }
3668ced19f26SSara Sharon 
3669e705c121SKalle Valo void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3670e705c121SKalle Valo 				       struct iwl_mvm_vif *mvmvif,
3671e705c121SKalle Valo 				       bool disable)
3672e705c121SKalle Valo {
3673e705c121SKalle Valo 	struct ieee80211_sta *sta;
3674e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta;
3675e705c121SKalle Valo 	int i;
3676e705c121SKalle Valo 
3677e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3678e705c121SKalle Valo 
3679e705c121SKalle Valo 	/* Block/unblock all the stations of the given mvmvif */
36800ae98812SSara Sharon 	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
3681e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3682e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
3683e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
3684e705c121SKalle Valo 			continue;
3685e705c121SKalle Valo 
3686e705c121SKalle Valo 		mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3687e705c121SKalle Valo 		if (mvm_sta->mac_id_n_color !=
3688e705c121SKalle Valo 		    FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3689e705c121SKalle Valo 			continue;
3690e705c121SKalle Valo 
3691e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3692e705c121SKalle Valo 	}
3693ced19f26SSara Sharon 
3694ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3695ced19f26SSara Sharon 		return;
3696ced19f26SSara Sharon 
3697ced19f26SSara Sharon 	/* Need to block/unblock also multicast station */
3698ced19f26SSara Sharon 	if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
3699ced19f26SSara Sharon 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3700ced19f26SSara Sharon 						  &mvmvif->mcast_sta, disable);
3701ced19f26SSara Sharon 
3702ced19f26SSara Sharon 	/*
3703ced19f26SSara Sharon 	 * Only unblock the broadcast station (FW blocks it for immediate
3704ced19f26SSara Sharon 	 * quiet, not the driver)
3705ced19f26SSara Sharon 	 */
3706ced19f26SSara Sharon 	if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
3707ced19f26SSara Sharon 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3708ced19f26SSara Sharon 						  &mvmvif->bcast_sta, disable);
3709e705c121SKalle Valo }
3710e705c121SKalle Valo 
3711e705c121SKalle Valo void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3712e705c121SKalle Valo {
3713e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3714e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
3715e705c121SKalle Valo 
3716e705c121SKalle Valo 	rcu_read_lock();
3717e705c121SKalle Valo 
3718e705c121SKalle Valo 	mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3719e705c121SKalle Valo 
3720e705c121SKalle Valo 	if (!WARN_ON(!mvmsta))
3721e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3722e705c121SKalle Valo 
3723e705c121SKalle Valo 	rcu_read_unlock();
3724e705c121SKalle Valo }
3725