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
11475c6bdeSJohannes Berg  * Copyright(c) 2018 - 2019 Intel Corporation
12e705c121SKalle Valo  *
13e705c121SKalle Valo  * This program is free software; you can redistribute it and/or modify
14e705c121SKalle Valo  * it under the terms of version 2 of the GNU General Public License as
15e705c121SKalle Valo  * published by the Free Software Foundation.
16e705c121SKalle Valo  *
17e705c121SKalle Valo  * This program is distributed in the hope that it will be useful, but
18e705c121SKalle Valo  * WITHOUT ANY WARRANTY; without even the implied warranty of
19e705c121SKalle Valo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20e705c121SKalle Valo  * General Public License for more details.
21e705c121SKalle Valo  *
22e705c121SKalle Valo  * The full GNU General Public License is included in this distribution
23e705c121SKalle Valo  * in the file called COPYING.
24e705c121SKalle Valo  *
25e705c121SKalle Valo  * Contact Information:
26cb2f8277SEmmanuel Grumbach  *  Intel Linux Wireless <linuxwifi@intel.com>
27e705c121SKalle Valo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28e705c121SKalle Valo  *
29e705c121SKalle Valo  * BSD LICENSE
30e705c121SKalle Valo  *
31e705c121SKalle Valo  * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
32e705c121SKalle Valo  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
3326d6c16bSSara Sharon  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
34475c6bdeSJohannes Berg  * Copyright(c) 2018 - 2019 Intel Corporation
35e705c121SKalle Valo  * All rights reserved.
36e705c121SKalle Valo  *
37e705c121SKalle Valo  * Redistribution and use in source and binary forms, with or without
38e705c121SKalle Valo  * modification, are permitted provided that the following conditions
39e705c121SKalle Valo  * are met:
40e705c121SKalle Valo  *
41e705c121SKalle Valo  *  * Redistributions of source code must retain the above copyright
42e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer.
43e705c121SKalle Valo  *  * Redistributions in binary form must reproduce the above copyright
44e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer in
45e705c121SKalle Valo  *    the documentation and/or other materials provided with the
46e705c121SKalle Valo  *    distribution.
47e705c121SKalle Valo  *  * Neither the name Intel Corporation nor the names of its
48e705c121SKalle Valo  *    contributors may be used to endorse or promote products derived
49e705c121SKalle Valo  *    from this software without specific prior written permission.
50e705c121SKalle Valo  *
51e705c121SKalle Valo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52e705c121SKalle Valo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53e705c121SKalle Valo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54e705c121SKalle Valo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55e705c121SKalle Valo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56e705c121SKalle Valo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57e705c121SKalle Valo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58e705c121SKalle Valo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59e705c121SKalle Valo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60e705c121SKalle Valo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61e705c121SKalle Valo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62e705c121SKalle Valo  *
63e705c121SKalle Valo  *****************************************************************************/
64e705c121SKalle Valo #include <net/mac80211.h>
65e705c121SKalle Valo 
66e705c121SKalle Valo #include "mvm.h"
67e705c121SKalle Valo #include "sta.h"
68e705c121SKalle Valo #include "rs.h"
69e705c121SKalle Valo 
70337bfc98SAvraham Stern static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm);
71337bfc98SAvraham Stern 
72337bfc98SAvraham Stern static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
73337bfc98SAvraham Stern 				u32 sta_id,
74337bfc98SAvraham Stern 				struct ieee80211_key_conf *key, bool mcast,
75337bfc98SAvraham Stern 				u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
76337bfc98SAvraham Stern 				u8 key_offset, bool mfp);
77337bfc98SAvraham Stern 
78854c5705SSara Sharon /*
79854c5705SSara Sharon  * New version of ADD_STA_sta command added new fields at the end of the
80854c5705SSara Sharon  * structure, so sending the size of the relevant API's structure is enough to
81854c5705SSara Sharon  * support both API versions.
82854c5705SSara Sharon  */
83854c5705SSara Sharon static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
84854c5705SSara Sharon {
85ced19f26SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) ||
86ced19f26SSara Sharon 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
87ced19f26SSara Sharon 		return sizeof(struct iwl_mvm_add_sta_cmd);
88ced19f26SSara Sharon 	else
89ced19f26SSara Sharon 		return sizeof(struct iwl_mvm_add_sta_cmd_v7);
90854c5705SSara Sharon }
91854c5705SSara Sharon 
92e705c121SKalle Valo static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
93e705c121SKalle Valo 				    enum nl80211_iftype iftype)
94e705c121SKalle Valo {
95e705c121SKalle Valo 	int sta_id;
96e705c121SKalle Valo 	u32 reserved_ids = 0;
97e705c121SKalle Valo 
98e705c121SKalle Valo 	BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
99e705c121SKalle Valo 	WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
100e705c121SKalle Valo 
101e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
102e705c121SKalle Valo 
103e705c121SKalle Valo 	/* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
104e705c121SKalle Valo 	if (iftype != NL80211_IFTYPE_STATION)
105e705c121SKalle Valo 		reserved_ids = BIT(0);
106e705c121SKalle Valo 
107e705c121SKalle Valo 	/* Don't take rcu_read_lock() since we are protected by mvm->mutex */
1080ae98812SSara Sharon 	for (sta_id = 0; sta_id < ARRAY_SIZE(mvm->fw_id_to_mac_id); sta_id++) {
109e705c121SKalle Valo 		if (BIT(sta_id) & reserved_ids)
110e705c121SKalle Valo 			continue;
111e705c121SKalle Valo 
112e705c121SKalle Valo 		if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
113e705c121SKalle Valo 					       lockdep_is_held(&mvm->mutex)))
114e705c121SKalle Valo 			return sta_id;
115e705c121SKalle Valo 	}
1160ae98812SSara Sharon 	return IWL_MVM_INVALID_STA;
117e705c121SKalle Valo }
118e705c121SKalle Valo 
119e705c121SKalle Valo /* send station add/update command to firmware */
120e705c121SKalle Valo int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
12124afba76SLiad Kaufman 			   bool update, unsigned int flags)
122e705c121SKalle Valo {
123e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
124e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd add_sta_cmd = {
125e705c121SKalle Valo 		.sta_id = mvm_sta->sta_id,
126e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
127e705c121SKalle Valo 		.add_modify = update ? 1 : 0,
128e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
1298addabf8SNaftali Goldstein 						 STA_FLG_MIMO_EN_MSK |
1308addabf8SNaftali Goldstein 						 STA_FLG_RTS_MIMO_PROT),
131cf0cda19SLiad Kaufman 		.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
132e705c121SKalle Valo 	};
133e705c121SKalle Valo 	int ret;
134e705c121SKalle Valo 	u32 status;
135e705c121SKalle Valo 	u32 agg_size = 0, mpdu_dens = 0;
136e705c121SKalle Valo 
137ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
138ced19f26SSara Sharon 		add_sta_cmd.station_type = mvm_sta->sta_type;
139ced19f26SSara Sharon 
14024afba76SLiad Kaufman 	if (!update || (flags & STA_MODIFY_QUEUES)) {
141e705c121SKalle Valo 		memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
14224afba76SLiad Kaufman 
143bb49701bSSara Sharon 		if (!iwl_mvm_has_new_tx_api(mvm)) {
144bb49701bSSara Sharon 			add_sta_cmd.tfd_queue_msk =
145bb49701bSSara Sharon 				cpu_to_le32(mvm_sta->tfd_queue_msk);
146bb49701bSSara Sharon 
14724afba76SLiad Kaufman 			if (flags & STA_MODIFY_QUEUES)
14824afba76SLiad Kaufman 				add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
149bb49701bSSara Sharon 		} else {
150bb49701bSSara Sharon 			WARN_ON(flags & STA_MODIFY_QUEUES);
151bb49701bSSara Sharon 		}
152e705c121SKalle Valo 	}
153e705c121SKalle Valo 
154e705c121SKalle Valo 	switch (sta->bandwidth) {
155e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_160:
156e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
157e705c121SKalle Valo 		/* fall through */
158e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_80:
159e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
160e705c121SKalle Valo 		/* fall through */
161e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_40:
162e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
163e705c121SKalle Valo 		/* fall through */
164e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_20:
165e705c121SKalle Valo 		if (sta->ht_cap.ht_supported)
166e705c121SKalle Valo 			add_sta_cmd.station_flags |=
167e705c121SKalle Valo 				cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
168e705c121SKalle Valo 		break;
169e705c121SKalle Valo 	}
170e705c121SKalle Valo 
171e705c121SKalle Valo 	switch (sta->rx_nss) {
172e705c121SKalle Valo 	case 1:
173e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
174e705c121SKalle Valo 		break;
175e705c121SKalle Valo 	case 2:
176e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
177e705c121SKalle Valo 		break;
178e705c121SKalle Valo 	case 3 ... 8:
179e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
180e705c121SKalle Valo 		break;
181e705c121SKalle Valo 	}
182e705c121SKalle Valo 
183e705c121SKalle Valo 	switch (sta->smps_mode) {
184e705c121SKalle Valo 	case IEEE80211_SMPS_AUTOMATIC:
185e705c121SKalle Valo 	case IEEE80211_SMPS_NUM_MODES:
186e705c121SKalle Valo 		WARN_ON(1);
187e705c121SKalle Valo 		break;
188e705c121SKalle Valo 	case IEEE80211_SMPS_STATIC:
189e705c121SKalle Valo 		/* override NSS */
190e705c121SKalle Valo 		add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
191e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
192e705c121SKalle Valo 		break;
193e705c121SKalle Valo 	case IEEE80211_SMPS_DYNAMIC:
194e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
195e705c121SKalle Valo 		break;
196e705c121SKalle Valo 	case IEEE80211_SMPS_OFF:
197e705c121SKalle Valo 		/* nothing */
198e705c121SKalle Valo 		break;
199e705c121SKalle Valo 	}
200e705c121SKalle Valo 
201e705c121SKalle Valo 	if (sta->ht_cap.ht_supported) {
202e705c121SKalle Valo 		add_sta_cmd.station_flags_msk |=
203e705c121SKalle Valo 			cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
204e705c121SKalle Valo 				    STA_FLG_AGG_MPDU_DENS_MSK);
205e705c121SKalle Valo 
206e705c121SKalle Valo 		mpdu_dens = sta->ht_cap.ampdu_density;
207e705c121SKalle Valo 	}
208e705c121SKalle Valo 
209e705c121SKalle Valo 	if (sta->vht_cap.vht_supported) {
210e705c121SKalle Valo 		agg_size = sta->vht_cap.cap &
211e705c121SKalle Valo 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
212e705c121SKalle Valo 		agg_size >>=
213e705c121SKalle Valo 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
214e705c121SKalle Valo 	} else if (sta->ht_cap.ht_supported) {
215e705c121SKalle Valo 		agg_size = sta->ht_cap.ampdu_factor;
216e705c121SKalle Valo 	}
217e705c121SKalle Valo 
218e705c121SKalle Valo 	add_sta_cmd.station_flags |=
219e705c121SKalle Valo 		cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
220e705c121SKalle Valo 	add_sta_cmd.station_flags |=
221e705c121SKalle Valo 		cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
222d94c5a82SGregory Greenman 	if (mvm_sta->sta_state >= IEEE80211_STA_ASSOC)
22319c52f46SEmmanuel Grumbach 		add_sta_cmd.assoc_id = cpu_to_le16(sta->aid);
224e705c121SKalle Valo 
22565e25482SJohannes Berg 	if (sta->wme) {
22665e25482SJohannes Berg 		add_sta_cmd.modify_mask |= STA_MODIFY_UAPSD_ACS;
22765e25482SJohannes Berg 
22865e25482SJohannes Berg 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
229c80eb570SEmmanuel Grumbach 			add_sta_cmd.uapsd_acs |= BIT(AC_BK);
23065e25482SJohannes Berg 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
231c80eb570SEmmanuel Grumbach 			add_sta_cmd.uapsd_acs |= BIT(AC_BE);
23265e25482SJohannes Berg 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
233c80eb570SEmmanuel Grumbach 			add_sta_cmd.uapsd_acs |= BIT(AC_VI);
23465e25482SJohannes Berg 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
235c80eb570SEmmanuel Grumbach 			add_sta_cmd.uapsd_acs |= BIT(AC_VO);
236c80eb570SEmmanuel Grumbach 		add_sta_cmd.uapsd_acs |= add_sta_cmd.uapsd_acs << 4;
237e71ca5eaSEmmanuel Grumbach 		add_sta_cmd.sp_length = sta->max_sp ? sta->max_sp * 2 : 128;
23865e25482SJohannes Berg 	}
23965e25482SJohannes Berg 
240e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
241854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
242854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
243e705c121SKalle Valo 					  &add_sta_cmd, &status);
244e705c121SKalle Valo 	if (ret)
245e705c121SKalle Valo 		return ret;
246e705c121SKalle Valo 
247837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
248e705c121SKalle Valo 	case ADD_STA_SUCCESS:
249e705c121SKalle Valo 		IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
250e705c121SKalle Valo 		break;
251e705c121SKalle Valo 	default:
252e705c121SKalle Valo 		ret = -EIO;
253e705c121SKalle Valo 		IWL_ERR(mvm, "ADD_STA failed\n");
254e705c121SKalle Valo 		break;
255e705c121SKalle Valo 	}
256e705c121SKalle Valo 
257e705c121SKalle Valo 	return ret;
258e705c121SKalle Valo }
259e705c121SKalle Valo 
2608cef5344SKees Cook static void iwl_mvm_rx_agg_session_expired(struct timer_list *t)
26110b2b201SSara Sharon {
2628cef5344SKees Cook 	struct iwl_mvm_baid_data *data =
2638cef5344SKees Cook 		from_timer(data, t, session_timer);
2648cef5344SKees Cook 	struct iwl_mvm_baid_data __rcu **rcu_ptr = data->rcu_ptr;
26510b2b201SSara Sharon 	struct iwl_mvm_baid_data *ba_data;
26610b2b201SSara Sharon 	struct ieee80211_sta *sta;
26710b2b201SSara Sharon 	struct iwl_mvm_sta *mvm_sta;
26810b2b201SSara Sharon 	unsigned long timeout;
26910b2b201SSara Sharon 
27010b2b201SSara Sharon 	rcu_read_lock();
27110b2b201SSara Sharon 
27210b2b201SSara Sharon 	ba_data = rcu_dereference(*rcu_ptr);
27310b2b201SSara Sharon 
27410b2b201SSara Sharon 	if (WARN_ON(!ba_data))
27510b2b201SSara Sharon 		goto unlock;
27610b2b201SSara Sharon 
27710b2b201SSara Sharon 	if (!ba_data->timeout)
27810b2b201SSara Sharon 		goto unlock;
27910b2b201SSara Sharon 
28010b2b201SSara Sharon 	timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
28110b2b201SSara Sharon 	if (time_is_after_jiffies(timeout)) {
28210b2b201SSara Sharon 		mod_timer(&ba_data->session_timer, timeout);
28310b2b201SSara Sharon 		goto unlock;
28410b2b201SSara Sharon 	}
28510b2b201SSara Sharon 
28610b2b201SSara Sharon 	/* Timer expired */
28710b2b201SSara Sharon 	sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
28861dd8a8aSEmmanuel Grumbach 
28961dd8a8aSEmmanuel Grumbach 	/*
29061dd8a8aSEmmanuel Grumbach 	 * sta should be valid unless the following happens:
29161dd8a8aSEmmanuel Grumbach 	 * The firmware asserts which triggers a reconfig flow, but
29261dd8a8aSEmmanuel Grumbach 	 * the reconfig fails before we set the pointer to sta into
29361dd8a8aSEmmanuel Grumbach 	 * the fw_id_to_mac_id pointer table. Mac80211 can't stop
29461dd8a8aSEmmanuel Grumbach 	 * A-MDPU and hence the timer continues to run. Then, the
29561dd8a8aSEmmanuel Grumbach 	 * timer expires and sta is NULL.
29661dd8a8aSEmmanuel Grumbach 	 */
29761dd8a8aSEmmanuel Grumbach 	if (!sta)
29861dd8a8aSEmmanuel Grumbach 		goto unlock;
29961dd8a8aSEmmanuel Grumbach 
30010b2b201SSara Sharon 	mvm_sta = iwl_mvm_sta_from_mac80211(sta);
30120fc690fSNaftali Goldstein 	ieee80211_rx_ba_timer_expired(mvm_sta->vif,
30210b2b201SSara Sharon 				      sta->addr, ba_data->tid);
30310b2b201SSara Sharon unlock:
30410b2b201SSara Sharon 	rcu_read_unlock();
30510b2b201SSara Sharon }
30610b2b201SSara Sharon 
3079794c64fSLiad Kaufman /* Disable aggregations for a bitmap of TIDs for a given station */
3089794c64fSLiad Kaufman static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
3099794c64fSLiad Kaufman 					unsigned long disable_agg_tids,
3109794c64fSLiad Kaufman 					bool remove_queue)
3119794c64fSLiad Kaufman {
3129794c64fSLiad Kaufman 	struct iwl_mvm_add_sta_cmd cmd = {};
3139794c64fSLiad Kaufman 	struct ieee80211_sta *sta;
3149794c64fSLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
3159794c64fSLiad Kaufman 	u32 status;
3169794c64fSLiad Kaufman 	u8 sta_id;
3179794c64fSLiad Kaufman 
318bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
319bb49701bSSara Sharon 		return -EINVAL;
320bb49701bSSara Sharon 
3219794c64fSLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
3229794c64fSLiad Kaufman 
3239794c64fSLiad Kaufman 	rcu_read_lock();
3249794c64fSLiad Kaufman 
3259794c64fSLiad Kaufman 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3269794c64fSLiad Kaufman 
3279794c64fSLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
3289794c64fSLiad Kaufman 		rcu_read_unlock();
3299794c64fSLiad Kaufman 		return -EINVAL;
3309794c64fSLiad Kaufman 	}
3319794c64fSLiad Kaufman 
3329794c64fSLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
3339794c64fSLiad Kaufman 
3349794c64fSLiad Kaufman 	mvmsta->tid_disable_agg |= disable_agg_tids;
3359794c64fSLiad Kaufman 
3369794c64fSLiad Kaufman 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
3379794c64fSLiad Kaufman 	cmd.sta_id = mvmsta->sta_id;
3389794c64fSLiad Kaufman 	cmd.add_modify = STA_MODE_MODIFY;
3399794c64fSLiad Kaufman 	cmd.modify_mask = STA_MODIFY_QUEUES;
3409794c64fSLiad Kaufman 	if (disable_agg_tids)
3419794c64fSLiad Kaufman 		cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
3429794c64fSLiad Kaufman 	if (remove_queue)
3439794c64fSLiad Kaufman 		cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
3449794c64fSLiad Kaufman 	cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
3459794c64fSLiad Kaufman 	cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
3469794c64fSLiad Kaufman 
3479794c64fSLiad Kaufman 	rcu_read_unlock();
3489794c64fSLiad Kaufman 
3499794c64fSLiad Kaufman 	/* Notify FW of queue removal from the STA queues */
3509794c64fSLiad Kaufman 	status = ADD_STA_SUCCESS;
351b2c1bf59SSara Sharon 	return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
3529794c64fSLiad Kaufman 					   iwl_mvm_add_sta_cmd_size(mvm),
3539794c64fSLiad Kaufman 					   &cmd, &status);
3549794c64fSLiad Kaufman }
3559794c64fSLiad Kaufman 
356cfbc6c4cSSara Sharon static int iwl_mvm_disable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
357cfbc6c4cSSara Sharon 			       int queue, u8 tid, u8 flags)
35899448a8cSJohannes Berg {
35999448a8cSJohannes Berg 	struct iwl_scd_txq_cfg_cmd cmd = {
36099448a8cSJohannes Berg 		.scd_queue = queue,
36199448a8cSJohannes Berg 		.action = SCD_CFG_DISABLE_QUEUE,
36299448a8cSJohannes Berg 	};
36399448a8cSJohannes Berg 	int ret;
36499448a8cSJohannes Berg 
36599448a8cSJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm)) {
36699448a8cSJohannes Berg 		iwl_trans_txq_free(mvm->trans, queue);
36799448a8cSJohannes Berg 
36899448a8cSJohannes Berg 		return 0;
36999448a8cSJohannes Berg 	}
37099448a8cSJohannes Berg 
371f3f240f9SJohannes Berg 	if (WARN_ON(mvm->queue_info[queue].tid_bitmap == 0))
37299448a8cSJohannes Berg 		return 0;
37399448a8cSJohannes Berg 
37499448a8cSJohannes Berg 	mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
37599448a8cSJohannes Berg 
3761c14089eSJohannes Berg 	cmd.action = mvm->queue_info[queue].tid_bitmap ?
37799448a8cSJohannes Berg 		SCD_CFG_ENABLE_QUEUE : SCD_CFG_DISABLE_QUEUE;
37899448a8cSJohannes Berg 	if (cmd.action == SCD_CFG_DISABLE_QUEUE)
37999448a8cSJohannes Berg 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_FREE;
38099448a8cSJohannes Berg 
38199448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
382cfbc6c4cSSara Sharon 			    "Disabling TXQ #%d tids=0x%x\n",
38399448a8cSJohannes Berg 			    queue,
384cfbc6c4cSSara Sharon 			    mvm->queue_info[queue].tid_bitmap);
38599448a8cSJohannes Berg 
38699448a8cSJohannes Berg 	/* If the queue is still enabled - nothing left to do in this func */
387f3f240f9SJohannes Berg 	if (cmd.action == SCD_CFG_ENABLE_QUEUE)
38899448a8cSJohannes Berg 		return 0;
38999448a8cSJohannes Berg 
39099448a8cSJohannes Berg 	cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
39199448a8cSJohannes Berg 	cmd.tid = mvm->queue_info[queue].txq_tid;
39299448a8cSJohannes Berg 
39399448a8cSJohannes Berg 	/* Make sure queue info is correct even though we overwrite it */
394cfbc6c4cSSara Sharon 	WARN(mvm->queue_info[queue].tid_bitmap,
395cfbc6c4cSSara Sharon 	     "TXQ #%d info out-of-sync - tids=0x%x\n",
396cfbc6c4cSSara Sharon 	     queue, mvm->queue_info[queue].tid_bitmap);
39799448a8cSJohannes Berg 
39899448a8cSJohannes Berg 	/* If we are here - the queue is freed and we can zero out these vals */
39999448a8cSJohannes Berg 	mvm->queue_info[queue].tid_bitmap = 0;
400cfbc6c4cSSara Sharon 
401cfbc6c4cSSara Sharon 	if (sta) {
402cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
403cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_tid(sta, tid);
404cfbc6c4cSSara Sharon 
405cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
406cfbc6c4cSSara Sharon 	}
40799448a8cSJohannes Berg 
40899448a8cSJohannes Berg 	/* Regardless if this is a reserved TXQ for a STA - mark it as false */
40999448a8cSJohannes Berg 	mvm->queue_info[queue].reserved = false;
41099448a8cSJohannes Berg 
41199448a8cSJohannes Berg 	iwl_trans_txq_disable(mvm->trans, queue, false);
41299448a8cSJohannes Berg 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, flags,
41399448a8cSJohannes Berg 				   sizeof(struct iwl_scd_txq_cfg_cmd), &cmd);
41499448a8cSJohannes Berg 
41599448a8cSJohannes Berg 	if (ret)
41699448a8cSJohannes Berg 		IWL_ERR(mvm, "Failed to disable queue %d (ret=%d)\n",
41799448a8cSJohannes Berg 			queue, ret);
41899448a8cSJohannes Berg 	return ret;
41999448a8cSJohannes Berg }
42099448a8cSJohannes Berg 
42142db09c1SLiad Kaufman static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
42242db09c1SLiad Kaufman {
42342db09c1SLiad Kaufman 	struct ieee80211_sta *sta;
42442db09c1SLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
42542db09c1SLiad Kaufman 	unsigned long tid_bitmap;
42642db09c1SLiad Kaufman 	unsigned long agg_tids = 0;
427806911daSSharon Dvir 	u8 sta_id;
42842db09c1SLiad Kaufman 	int tid;
42942db09c1SLiad Kaufman 
43042db09c1SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
43142db09c1SLiad Kaufman 
432bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
433bb49701bSSara Sharon 		return -EINVAL;
434bb49701bSSara Sharon 
43542db09c1SLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
43642db09c1SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
43742db09c1SLiad Kaufman 
43842db09c1SLiad Kaufman 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
43942db09c1SLiad Kaufman 					lockdep_is_held(&mvm->mutex));
44042db09c1SLiad Kaufman 
44142db09c1SLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
44242db09c1SLiad Kaufman 		return -EINVAL;
44342db09c1SLiad Kaufman 
44442db09c1SLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
44542db09c1SLiad Kaufman 
44642db09c1SLiad Kaufman 	spin_lock_bh(&mvmsta->lock);
44742db09c1SLiad Kaufman 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
44842db09c1SLiad Kaufman 		if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
44942db09c1SLiad Kaufman 			agg_tids |= BIT(tid);
45042db09c1SLiad Kaufman 	}
45142db09c1SLiad Kaufman 	spin_unlock_bh(&mvmsta->lock);
45242db09c1SLiad Kaufman 
45342db09c1SLiad Kaufman 	return agg_tids;
45442db09c1SLiad Kaufman }
45542db09c1SLiad Kaufman 
4569794c64fSLiad Kaufman /*
4579794c64fSLiad Kaufman  * Remove a queue from a station's resources.
4589794c64fSLiad Kaufman  * Note that this only marks as free. It DOESN'T delete a BA agreement, and
4599794c64fSLiad Kaufman  * doesn't disable the queue
4609794c64fSLiad Kaufman  */
4619794c64fSLiad Kaufman static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
4629794c64fSLiad Kaufman {
4639794c64fSLiad Kaufman 	struct ieee80211_sta *sta;
4649794c64fSLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
4659794c64fSLiad Kaufman 	unsigned long tid_bitmap;
4669794c64fSLiad Kaufman 	unsigned long disable_agg_tids = 0;
4679794c64fSLiad Kaufman 	u8 sta_id;
4689794c64fSLiad Kaufman 	int tid;
4699794c64fSLiad Kaufman 
4709794c64fSLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
4719794c64fSLiad Kaufman 
472bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
473bb49701bSSara Sharon 		return -EINVAL;
474bb49701bSSara Sharon 
4759794c64fSLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
4769794c64fSLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
4779794c64fSLiad Kaufman 
4789794c64fSLiad Kaufman 	rcu_read_lock();
4799794c64fSLiad Kaufman 
4809794c64fSLiad Kaufman 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
4819794c64fSLiad Kaufman 
4829794c64fSLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
4839794c64fSLiad Kaufman 		rcu_read_unlock();
4849794c64fSLiad Kaufman 		return 0;
4859794c64fSLiad Kaufman 	}
4869794c64fSLiad Kaufman 
4879794c64fSLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
4889794c64fSLiad Kaufman 
4899794c64fSLiad Kaufman 	spin_lock_bh(&mvmsta->lock);
49042db09c1SLiad Kaufman 	/* Unmap MAC queues and TIDs from this queue */
4919794c64fSLiad Kaufman 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
492cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
493cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_tid(sta, tid);
494cfbc6c4cSSara Sharon 
4959794c64fSLiad Kaufman 		if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
4969794c64fSLiad Kaufman 			disable_agg_tids |= BIT(tid);
4976862fceeSSara Sharon 		mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
498cfbc6c4cSSara Sharon 
499cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
5009794c64fSLiad Kaufman 	}
5019794c64fSLiad Kaufman 
50242db09c1SLiad Kaufman 	mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
5039794c64fSLiad Kaufman 	spin_unlock_bh(&mvmsta->lock);
5049794c64fSLiad Kaufman 
5059794c64fSLiad Kaufman 	rcu_read_unlock();
5069794c64fSLiad Kaufman 
50706bc6f6eSJohannes Berg 	/*
50806bc6f6eSJohannes Berg 	 * The TX path may have been using this TXQ_ID from the tid_data,
50906bc6f6eSJohannes Berg 	 * so make sure it's no longer running so that we can safely reuse
51006bc6f6eSJohannes Berg 	 * this TXQ later. We've set all the TIDs to IWL_MVM_INVALID_QUEUE
51106bc6f6eSJohannes Berg 	 * above, but nothing guarantees we've stopped using them. Thus,
51206bc6f6eSJohannes Berg 	 * without this, we could get to iwl_mvm_disable_txq() and remove
51306bc6f6eSJohannes Berg 	 * the queue while still sending frames to it.
51406bc6f6eSJohannes Berg 	 */
51506bc6f6eSJohannes Berg 	synchronize_net();
51606bc6f6eSJohannes Berg 
5179794c64fSLiad Kaufman 	return disable_agg_tids;
5189794c64fSLiad Kaufman }
5199794c64fSLiad Kaufman 
52001796ff2SSara Sharon static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
521cfbc6c4cSSara Sharon 				       struct ieee80211_sta *old_sta,
522724fe771SJohannes Berg 				       u8 new_sta_id)
52301796ff2SSara Sharon {
52401796ff2SSara Sharon 	struct iwl_mvm_sta *mvmsta;
525cfbc6c4cSSara Sharon 	u8 sta_id, tid;
52601796ff2SSara Sharon 	unsigned long disable_agg_tids = 0;
527724fe771SJohannes Berg 	bool same_sta;
52801796ff2SSara Sharon 	int ret;
52901796ff2SSara Sharon 
53001796ff2SSara Sharon 	lockdep_assert_held(&mvm->mutex);
53101796ff2SSara Sharon 
532bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
533bb49701bSSara Sharon 		return -EINVAL;
534bb49701bSSara Sharon 
53501796ff2SSara Sharon 	sta_id = mvm->queue_info[queue].ra_sta_id;
53601796ff2SSara Sharon 	tid = mvm->queue_info[queue].txq_tid;
53701796ff2SSara Sharon 
538724fe771SJohannes Berg 	same_sta = sta_id == new_sta_id;
539724fe771SJohannes Berg 
54001796ff2SSara Sharon 	mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
541e3df1e4bSSharon Dvir 	if (WARN_ON(!mvmsta))
542e3df1e4bSSharon Dvir 		return -EINVAL;
54301796ff2SSara Sharon 
54401796ff2SSara Sharon 	disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
54501796ff2SSara Sharon 	/* Disable the queue */
54601796ff2SSara Sharon 	if (disable_agg_tids)
54701796ff2SSara Sharon 		iwl_mvm_invalidate_sta_queue(mvm, queue,
54801796ff2SSara Sharon 					     disable_agg_tids, false);
54901796ff2SSara Sharon 
550cfbc6c4cSSara Sharon 	ret = iwl_mvm_disable_txq(mvm, old_sta, queue, tid, 0);
55101796ff2SSara Sharon 	if (ret) {
55201796ff2SSara Sharon 		IWL_ERR(mvm,
55301796ff2SSara Sharon 			"Failed to free inactive queue %d (ret=%d)\n",
55401796ff2SSara Sharon 			queue, ret);
55501796ff2SSara Sharon 
55601796ff2SSara Sharon 		return ret;
55701796ff2SSara Sharon 	}
55801796ff2SSara Sharon 
55901796ff2SSara Sharon 	/* If TXQ is allocated to another STA, update removal in FW */
56001796ff2SSara Sharon 	if (!same_sta)
56101796ff2SSara Sharon 		iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
56201796ff2SSara Sharon 
56301796ff2SSara Sharon 	return 0;
56401796ff2SSara Sharon }
56501796ff2SSara Sharon 
56642db09c1SLiad Kaufman static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
56742db09c1SLiad Kaufman 				    unsigned long tfd_queue_mask, u8 ac)
56842db09c1SLiad Kaufman {
56942db09c1SLiad Kaufman 	int queue = 0;
57042db09c1SLiad Kaufman 	u8 ac_to_queue[IEEE80211_NUM_ACS];
57142db09c1SLiad Kaufman 	int i;
57242db09c1SLiad Kaufman 
57390d2d94cSJohannes Berg 	/*
57490d2d94cSJohannes Berg 	 * This protects us against grabbing a queue that's being reconfigured
57590d2d94cSJohannes Berg 	 * by the inactivity checker.
57690d2d94cSJohannes Berg 	 */
57790d2d94cSJohannes Berg 	lockdep_assert_held(&mvm->mutex);
57890d2d94cSJohannes Berg 
579bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
580bb49701bSSara Sharon 		return -EINVAL;
58142db09c1SLiad Kaufman 
58242db09c1SLiad Kaufman 	memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
58342db09c1SLiad Kaufman 
58442db09c1SLiad Kaufman 	/* See what ACs the existing queues for this STA have */
58542db09c1SLiad Kaufman 	for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
58642db09c1SLiad Kaufman 		/* Only DATA queues can be shared */
58742db09c1SLiad Kaufman 		if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
58842db09c1SLiad Kaufman 		    i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
58942db09c1SLiad Kaufman 			continue;
59042db09c1SLiad Kaufman 
59142db09c1SLiad Kaufman 		ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
59242db09c1SLiad Kaufman 	}
59342db09c1SLiad Kaufman 
59442db09c1SLiad Kaufman 	/*
59542db09c1SLiad Kaufman 	 * The queue to share is chosen only from DATA queues as follows (in
59642db09c1SLiad Kaufman 	 * descending priority):
59742db09c1SLiad Kaufman 	 * 1. An AC_BE queue
59842db09c1SLiad Kaufman 	 * 2. Same AC queue
59942db09c1SLiad Kaufman 	 * 3. Highest AC queue that is lower than new AC
60042db09c1SLiad Kaufman 	 * 4. Any existing AC (there always is at least 1 DATA queue)
60142db09c1SLiad Kaufman 	 */
60242db09c1SLiad Kaufman 
60342db09c1SLiad Kaufman 	/* Priority 1: An AC_BE queue */
60442db09c1SLiad Kaufman 	if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
60542db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_BE];
60642db09c1SLiad Kaufman 	/* Priority 2: Same AC queue */
60742db09c1SLiad Kaufman 	else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
60842db09c1SLiad Kaufman 		queue = ac_to_queue[ac];
60942db09c1SLiad Kaufman 	/* Priority 3a: If new AC is VO and VI exists - use VI */
61042db09c1SLiad Kaufman 	else if (ac == IEEE80211_AC_VO &&
61142db09c1SLiad Kaufman 		 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
61242db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VI];
61342db09c1SLiad Kaufman 	/* Priority 3b: No BE so only AC less than the new one is BK */
61442db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
61542db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_BK];
61642db09c1SLiad Kaufman 	/* Priority 4a: No BE nor BK - use VI if exists */
61742db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
61842db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VI];
61942db09c1SLiad Kaufman 	/* Priority 4b: No BE, BK nor VI - use VO if exists */
62042db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
62142db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VO];
62242db09c1SLiad Kaufman 
62342db09c1SLiad Kaufman 	/* Make sure queue found (or not) is legal */
6249f9af3d7SLiad Kaufman 	if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
6259f9af3d7SLiad Kaufman 	    !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
6269f9af3d7SLiad Kaufman 	    (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
62742db09c1SLiad Kaufman 		IWL_ERR(mvm, "No DATA queues available to share\n");
6289f9af3d7SLiad Kaufman 		return -ENOSPC;
6299f9af3d7SLiad Kaufman 	}
6309f9af3d7SLiad Kaufman 
63142db09c1SLiad Kaufman 	return queue;
63242db09c1SLiad Kaufman }
63342db09c1SLiad Kaufman 
63458f2cc57SLiad Kaufman /*
6359f9af3d7SLiad Kaufman  * If a given queue has a higher AC than the TID stream that is being compared
6369f9af3d7SLiad Kaufman  * to, the queue needs to be redirected to the lower AC. This function does that
63758f2cc57SLiad Kaufman  * in such a case, otherwise - if no redirection required - it does nothing,
63858f2cc57SLiad Kaufman  * unless the %force param is true.
63958f2cc57SLiad Kaufman  */
640cfbc6c4cSSara Sharon static int iwl_mvm_redirect_queue(struct iwl_mvm *mvm, int queue, int tid,
64158f2cc57SLiad Kaufman 				  int ac, int ssn, unsigned int wdg_timeout,
642cfbc6c4cSSara Sharon 				  bool force, struct iwl_mvm_txq *txq)
64358f2cc57SLiad Kaufman {
64458f2cc57SLiad Kaufman 	struct iwl_scd_txq_cfg_cmd cmd = {
64558f2cc57SLiad Kaufman 		.scd_queue = queue,
646f7c692deSLiad Kaufman 		.action = SCD_CFG_DISABLE_QUEUE,
64758f2cc57SLiad Kaufman 	};
64858f2cc57SLiad Kaufman 	bool shared_queue;
64958f2cc57SLiad Kaufman 	int ret;
65058f2cc57SLiad Kaufman 
651bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
652bb49701bSSara Sharon 		return -EINVAL;
653bb49701bSSara Sharon 
65458f2cc57SLiad Kaufman 	/*
65558f2cc57SLiad Kaufman 	 * If the AC is lower than current one - FIFO needs to be redirected to
65658f2cc57SLiad Kaufman 	 * the lowest one of the streams in the queue. Check if this is needed
65758f2cc57SLiad Kaufman 	 * here.
65858f2cc57SLiad Kaufman 	 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
65958f2cc57SLiad Kaufman 	 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
66058f2cc57SLiad Kaufman 	 * we need to check if the numerical value of X is LARGER than of Y.
66158f2cc57SLiad Kaufman 	 */
66258f2cc57SLiad Kaufman 	if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
66358f2cc57SLiad Kaufman 		IWL_DEBUG_TX_QUEUES(mvm,
66458f2cc57SLiad Kaufman 				    "No redirection needed on TXQ #%d\n",
66558f2cc57SLiad Kaufman 				    queue);
66658f2cc57SLiad Kaufman 		return 0;
66758f2cc57SLiad Kaufman 	}
66858f2cc57SLiad Kaufman 
66958f2cc57SLiad Kaufman 	cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
67058f2cc57SLiad Kaufman 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
671edbe961cSLiad Kaufman 	cmd.tid = mvm->queue_info[queue].txq_tid;
6721c14089eSJohannes Berg 	shared_queue = hweight16(mvm->queue_info[queue].tid_bitmap) > 1;
67358f2cc57SLiad Kaufman 
6749f9af3d7SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
67558f2cc57SLiad Kaufman 			    queue, iwl_mvm_ac_to_tx_fifo[ac]);
67658f2cc57SLiad Kaufman 
677cfbc6c4cSSara Sharon 	/* Stop the queue and wait for it to empty */
678cfbc6c4cSSara Sharon 	txq->stopped = true;
679cfbc6c4cSSara Sharon 
680a1a57877SSara Sharon 	ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue));
68158f2cc57SLiad Kaufman 	if (ret) {
68258f2cc57SLiad Kaufman 		IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
68358f2cc57SLiad Kaufman 			queue);
68458f2cc57SLiad Kaufman 		ret = -EIO;
68558f2cc57SLiad Kaufman 		goto out;
68658f2cc57SLiad Kaufman 	}
68758f2cc57SLiad Kaufman 
68858f2cc57SLiad Kaufman 	/* Before redirecting the queue we need to de-activate it */
68958f2cc57SLiad Kaufman 	iwl_trans_txq_disable(mvm->trans, queue, false);
69058f2cc57SLiad Kaufman 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
69158f2cc57SLiad Kaufman 	if (ret)
69258f2cc57SLiad Kaufman 		IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
69358f2cc57SLiad Kaufman 			ret);
69458f2cc57SLiad Kaufman 
69558f2cc57SLiad Kaufman 	/* Make sure the SCD wrptr is correctly set before reconfiguring */
696ca3b9c6bSSara Sharon 	iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
69758f2cc57SLiad Kaufman 
698edbe961cSLiad Kaufman 	/* Update the TID "owner" of the queue */
699edbe961cSLiad Kaufman 	mvm->queue_info[queue].txq_tid = tid;
700edbe961cSLiad Kaufman 
70158f2cc57SLiad Kaufman 	/* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
70258f2cc57SLiad Kaufman 
70358f2cc57SLiad Kaufman 	/* Redirect to lower AC */
70458f2cc57SLiad Kaufman 	iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
7050ec9257bSSara Sharon 			     cmd.sta_id, tid, IWL_FRAME_LIMIT, ssn);
70658f2cc57SLiad Kaufman 
70758f2cc57SLiad Kaufman 	/* Update AC marking of the queue */
70858f2cc57SLiad Kaufman 	mvm->queue_info[queue].mac80211_ac = ac;
70958f2cc57SLiad Kaufman 
71058f2cc57SLiad Kaufman 	/*
71158f2cc57SLiad Kaufman 	 * Mark queue as shared in transport if shared
71258f2cc57SLiad Kaufman 	 * Note this has to be done after queue enablement because enablement
71358f2cc57SLiad Kaufman 	 * can also set this value, and there is no indication there to shared
71458f2cc57SLiad Kaufman 	 * queues
71558f2cc57SLiad Kaufman 	 */
71658f2cc57SLiad Kaufman 	if (shared_queue)
71758f2cc57SLiad Kaufman 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
71858f2cc57SLiad Kaufman 
71958f2cc57SLiad Kaufman out:
720cfbc6c4cSSara Sharon 	/* Continue using the queue */
721cfbc6c4cSSara Sharon 	txq->stopped = false;
72258f2cc57SLiad Kaufman 
72358f2cc57SLiad Kaufman 	return ret;
72458f2cc57SLiad Kaufman }
72558f2cc57SLiad Kaufman 
72699448a8cSJohannes Berg static int iwl_mvm_find_free_queue(struct iwl_mvm *mvm, u8 sta_id,
72799448a8cSJohannes Berg 				   u8 minq, u8 maxq)
72899448a8cSJohannes Berg {
72999448a8cSJohannes Berg 	int i;
73099448a8cSJohannes Berg 
731f3f240f9SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
73299448a8cSJohannes Berg 
73399448a8cSJohannes Berg 	/* This should not be hit with new TX path */
73499448a8cSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
73599448a8cSJohannes Berg 		return -ENOSPC;
73699448a8cSJohannes Berg 
73799448a8cSJohannes Berg 	/* Start by looking for a free queue */
73899448a8cSJohannes Berg 	for (i = minq; i <= maxq; i++)
7391c14089eSJohannes Berg 		if (mvm->queue_info[i].tid_bitmap == 0 &&
74099448a8cSJohannes Berg 		    mvm->queue_info[i].status == IWL_MVM_QUEUE_FREE)
74199448a8cSJohannes Berg 			return i;
74299448a8cSJohannes Berg 
74399448a8cSJohannes Berg 	return -ENOSPC;
74499448a8cSJohannes Berg }
74599448a8cSJohannes Berg 
746cfbc6c4cSSara Sharon static int iwl_mvm_tvqm_enable_txq(struct iwl_mvm *mvm,
74799448a8cSJohannes Berg 				   u8 sta_id, u8 tid, unsigned int timeout)
74899448a8cSJohannes Berg {
74999448a8cSJohannes Berg 	int queue, size = IWL_DEFAULT_QUEUE_SIZE;
75099448a8cSJohannes Berg 
75199448a8cSJohannes Berg 	if (tid == IWL_MAX_TID_COUNT) {
75299448a8cSJohannes Berg 		tid = IWL_MGMT_TID;
753ff911dcaSShaul Triebitz 		size = max_t(u32, IWL_MGMT_QUEUE_SIZE,
754ff911dcaSShaul Triebitz 			     mvm->trans->cfg->min_txq_size);
75599448a8cSJohannes Berg 	}
75699448a8cSJohannes Berg 	queue = iwl_trans_txq_alloc(mvm->trans,
75799448a8cSJohannes Berg 				    cpu_to_le16(TX_QUEUE_CFG_ENABLE_QUEUE),
75899448a8cSJohannes Berg 				    sta_id, tid, SCD_QUEUE_CFG, size, timeout);
75999448a8cSJohannes Berg 
76099448a8cSJohannes Berg 	if (queue < 0) {
76199448a8cSJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm,
76299448a8cSJohannes Berg 				    "Failed allocating TXQ for sta %d tid %d, ret: %d\n",
76399448a8cSJohannes Berg 				    sta_id, tid, queue);
76499448a8cSJohannes Berg 		return queue;
76599448a8cSJohannes Berg 	}
76699448a8cSJohannes Berg 
76799448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d for sta %d tid %d\n",
76899448a8cSJohannes Berg 			    queue, sta_id, tid);
76999448a8cSJohannes Berg 
770cfbc6c4cSSara Sharon 	IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d\n", queue);
77199448a8cSJohannes Berg 
77299448a8cSJohannes Berg 	return queue;
77399448a8cSJohannes Berg }
77499448a8cSJohannes Berg 
775310181ecSSara Sharon static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
776310181ecSSara Sharon 					struct ieee80211_sta *sta, u8 ac,
777310181ecSSara Sharon 					int tid)
778310181ecSSara Sharon {
779310181ecSSara Sharon 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
780cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq =
781cfbc6c4cSSara Sharon 		iwl_mvm_txq_from_tid(sta, tid);
782310181ecSSara Sharon 	unsigned int wdg_timeout =
783310181ecSSara Sharon 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
784310181ecSSara Sharon 	int queue = -1;
785310181ecSSara Sharon 
786310181ecSSara Sharon 	lockdep_assert_held(&mvm->mutex);
787310181ecSSara Sharon 
788310181ecSSara Sharon 	IWL_DEBUG_TX_QUEUES(mvm,
789310181ecSSara Sharon 			    "Allocating queue for sta %d on tid %d\n",
790310181ecSSara Sharon 			    mvmsta->sta_id, tid);
791cfbc6c4cSSara Sharon 	queue = iwl_mvm_tvqm_enable_txq(mvm, mvmsta->sta_id, tid, wdg_timeout);
792310181ecSSara Sharon 	if (queue < 0)
793310181ecSSara Sharon 		return queue;
794310181ecSSara Sharon 
795cfbc6c4cSSara Sharon 	mvmtxq->txq_id = queue;
796cfbc6c4cSSara Sharon 	mvm->tvqm_info[queue].txq_tid = tid;
797cfbc6c4cSSara Sharon 	mvm->tvqm_info[queue].sta_id = mvmsta->sta_id;
798cfbc6c4cSSara Sharon 
799310181ecSSara Sharon 	IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
800310181ecSSara Sharon 
801310181ecSSara Sharon 	spin_lock_bh(&mvmsta->lock);
802310181ecSSara Sharon 	mvmsta->tid_data[tid].txq_id = queue;
803310181ecSSara Sharon 	spin_unlock_bh(&mvmsta->lock);
804310181ecSSara Sharon 
805310181ecSSara Sharon 	return 0;
806310181ecSSara Sharon }
807310181ecSSara Sharon 
808cfbc6c4cSSara Sharon static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm,
809cfbc6c4cSSara Sharon 				       struct ieee80211_sta *sta,
810cfbc6c4cSSara Sharon 				       int queue, u8 sta_id, u8 tid)
81199448a8cSJohannes Berg {
81299448a8cSJohannes Berg 	bool enable_queue = true;
81399448a8cSJohannes Berg 
81499448a8cSJohannes Berg 	/* Make sure this TID isn't already enabled */
81599448a8cSJohannes Berg 	if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) {
81699448a8cSJohannes Berg 		IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n",
81799448a8cSJohannes Berg 			queue, tid);
81899448a8cSJohannes Berg 		return false;
81999448a8cSJohannes Berg 	}
82099448a8cSJohannes Berg 
82199448a8cSJohannes Berg 	/* Update mappings and refcounts */
8221c14089eSJohannes Berg 	if (mvm->queue_info[queue].tid_bitmap)
82399448a8cSJohannes Berg 		enable_queue = false;
82499448a8cSJohannes Berg 
82599448a8cSJohannes Berg 	mvm->queue_info[queue].tid_bitmap |= BIT(tid);
82699448a8cSJohannes Berg 	mvm->queue_info[queue].ra_sta_id = sta_id;
82799448a8cSJohannes Berg 
82899448a8cSJohannes Berg 	if (enable_queue) {
82999448a8cSJohannes Berg 		if (tid != IWL_MAX_TID_COUNT)
83099448a8cSJohannes Berg 			mvm->queue_info[queue].mac80211_ac =
83199448a8cSJohannes Berg 				tid_to_mac80211_ac[tid];
83299448a8cSJohannes Berg 		else
83399448a8cSJohannes Berg 			mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO;
83499448a8cSJohannes Berg 
83599448a8cSJohannes Berg 		mvm->queue_info[queue].txq_tid = tid;
83699448a8cSJohannes Berg 	}
83799448a8cSJohannes Berg 
838cfbc6c4cSSara Sharon 	if (sta) {
839cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
840cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_tid(sta, tid);
841cfbc6c4cSSara Sharon 
842cfbc6c4cSSara Sharon 		mvmtxq->txq_id = queue;
843cfbc6c4cSSara Sharon 	}
844cfbc6c4cSSara Sharon 
84599448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
846cfbc6c4cSSara Sharon 			    "Enabling TXQ #%d tids=0x%x\n",
847cfbc6c4cSSara Sharon 			    queue, mvm->queue_info[queue].tid_bitmap);
84899448a8cSJohannes Berg 
84999448a8cSJohannes Berg 	return enable_queue;
85099448a8cSJohannes Berg }
85199448a8cSJohannes Berg 
852cfbc6c4cSSara Sharon static bool iwl_mvm_enable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
853cfbc6c4cSSara Sharon 			       int queue, u16 ssn,
85499448a8cSJohannes Berg 			       const struct iwl_trans_txq_scd_cfg *cfg,
85599448a8cSJohannes Berg 			       unsigned int wdg_timeout)
85699448a8cSJohannes Berg {
85799448a8cSJohannes Berg 	struct iwl_scd_txq_cfg_cmd cmd = {
85899448a8cSJohannes Berg 		.scd_queue = queue,
85999448a8cSJohannes Berg 		.action = SCD_CFG_ENABLE_QUEUE,
86099448a8cSJohannes Berg 		.window = cfg->frame_limit,
86199448a8cSJohannes Berg 		.sta_id = cfg->sta_id,
86299448a8cSJohannes Berg 		.ssn = cpu_to_le16(ssn),
86399448a8cSJohannes Berg 		.tx_fifo = cfg->fifo,
86499448a8cSJohannes Berg 		.aggregate = cfg->aggregate,
86599448a8cSJohannes Berg 		.tid = cfg->tid,
86699448a8cSJohannes Berg 	};
86799448a8cSJohannes Berg 	bool inc_ssn;
86899448a8cSJohannes Berg 
86999448a8cSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
87099448a8cSJohannes Berg 		return false;
87199448a8cSJohannes Berg 
87299448a8cSJohannes Berg 	/* Send the enabling command if we need to */
873cfbc6c4cSSara Sharon 	if (!iwl_mvm_update_txq_mapping(mvm, sta, queue, cfg->sta_id, cfg->tid))
87499448a8cSJohannes Berg 		return false;
87599448a8cSJohannes Berg 
87699448a8cSJohannes Berg 	inc_ssn = iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn,
87799448a8cSJohannes Berg 					   NULL, wdg_timeout);
87899448a8cSJohannes Berg 	if (inc_ssn)
87999448a8cSJohannes Berg 		le16_add_cpu(&cmd.ssn, 1);
88099448a8cSJohannes Berg 
88199448a8cSJohannes Berg 	WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd),
88299448a8cSJohannes Berg 	     "Failed to configure queue %d on FIFO %d\n", queue, cfg->fifo);
88399448a8cSJohannes Berg 
88499448a8cSJohannes Berg 	return inc_ssn;
88599448a8cSJohannes Berg }
88699448a8cSJohannes Berg 
887b3a87f11SJohannes Berg static void iwl_mvm_change_queue_tid(struct iwl_mvm *mvm, int queue)
88819aefa45SLiad Kaufman {
88919aefa45SLiad Kaufman 	struct iwl_scd_txq_cfg_cmd cmd = {
89019aefa45SLiad Kaufman 		.scd_queue = queue,
89119aefa45SLiad Kaufman 		.action = SCD_CFG_UPDATE_QUEUE_TID,
89219aefa45SLiad Kaufman 	};
89319aefa45SLiad Kaufman 	int tid;
89419aefa45SLiad Kaufman 	unsigned long tid_bitmap;
89519aefa45SLiad Kaufman 	int ret;
89619aefa45SLiad Kaufman 
89719aefa45SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
89819aefa45SLiad Kaufman 
899bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
900bb49701bSSara Sharon 		return;
901bb49701bSSara Sharon 
90219aefa45SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
90319aefa45SLiad Kaufman 
90419aefa45SLiad Kaufman 	if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
90519aefa45SLiad Kaufman 		return;
90619aefa45SLiad Kaufman 
90719aefa45SLiad Kaufman 	/* Find any TID for queue */
90819aefa45SLiad Kaufman 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
90919aefa45SLiad Kaufman 	cmd.tid = tid;
91019aefa45SLiad Kaufman 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
91119aefa45SLiad Kaufman 
91219aefa45SLiad Kaufman 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
913341ca402SLiad Kaufman 	if (ret) {
91419aefa45SLiad Kaufman 		IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
91519aefa45SLiad Kaufman 			queue, ret);
916341ca402SLiad Kaufman 		return;
917341ca402SLiad Kaufman 	}
918341ca402SLiad Kaufman 
919341ca402SLiad Kaufman 	mvm->queue_info[queue].txq_tid = tid;
92019aefa45SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
92119aefa45SLiad Kaufman 			    queue, tid);
92219aefa45SLiad Kaufman }
92319aefa45SLiad Kaufman 
9249f9af3d7SLiad Kaufman static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
9259f9af3d7SLiad Kaufman {
9269f9af3d7SLiad Kaufman 	struct ieee80211_sta *sta;
9279f9af3d7SLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
928806911daSSharon Dvir 	u8 sta_id;
9299f9af3d7SLiad Kaufman 	int tid = -1;
9309f9af3d7SLiad Kaufman 	unsigned long tid_bitmap;
9319f9af3d7SLiad Kaufman 	unsigned int wdg_timeout;
9329f9af3d7SLiad Kaufman 	int ssn;
9339f9af3d7SLiad Kaufman 	int ret = true;
9349f9af3d7SLiad Kaufman 
935bb49701bSSara Sharon 	/* queue sharing is disabled on new TX path */
936bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
937bb49701bSSara Sharon 		return;
938bb49701bSSara Sharon 
9399f9af3d7SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
9409f9af3d7SLiad Kaufman 
9419f9af3d7SLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
9429f9af3d7SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
9439f9af3d7SLiad Kaufman 
9449f9af3d7SLiad Kaufman 	/* Find TID for queue, and make sure it is the only one on the queue */
9459f9af3d7SLiad Kaufman 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
9469f9af3d7SLiad Kaufman 	if (tid_bitmap != BIT(tid)) {
9479f9af3d7SLiad Kaufman 		IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
9489f9af3d7SLiad Kaufman 			queue, tid_bitmap);
9499f9af3d7SLiad Kaufman 		return;
9509f9af3d7SLiad Kaufman 	}
9519f9af3d7SLiad Kaufman 
9529f9af3d7SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
9539f9af3d7SLiad Kaufman 			    tid);
9549f9af3d7SLiad Kaufman 
9559f9af3d7SLiad Kaufman 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
9569f9af3d7SLiad Kaufman 					lockdep_is_held(&mvm->mutex));
9579f9af3d7SLiad Kaufman 
9589f9af3d7SLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
9599f9af3d7SLiad Kaufman 		return;
9609f9af3d7SLiad Kaufman 
9619f9af3d7SLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
9629f9af3d7SLiad Kaufman 	wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
9639f9af3d7SLiad Kaufman 
9649f9af3d7SLiad Kaufman 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
9659f9af3d7SLiad Kaufman 
966cfbc6c4cSSara Sharon 	ret = iwl_mvm_redirect_queue(mvm, queue, tid,
9679f9af3d7SLiad Kaufman 				     tid_to_mac80211_ac[tid], ssn,
968cfbc6c4cSSara Sharon 				     wdg_timeout, true,
969cfbc6c4cSSara Sharon 				     iwl_mvm_txq_from_tid(sta, tid));
9709f9af3d7SLiad Kaufman 	if (ret) {
9719f9af3d7SLiad Kaufman 		IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
9729f9af3d7SLiad Kaufman 		return;
9739f9af3d7SLiad Kaufman 	}
9749f9af3d7SLiad Kaufman 
9759f9af3d7SLiad Kaufman 	/* If aggs should be turned back on - do it */
9769f9af3d7SLiad Kaufman 	if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
9779cd70e80SEmmanuel Grumbach 		struct iwl_mvm_add_sta_cmd cmd = {0};
9789f9af3d7SLiad Kaufman 
9799f9af3d7SLiad Kaufman 		mvmsta->tid_disable_agg &= ~BIT(tid);
9809f9af3d7SLiad Kaufman 
9819f9af3d7SLiad Kaufman 		cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
9829f9af3d7SLiad Kaufman 		cmd.sta_id = mvmsta->sta_id;
9839f9af3d7SLiad Kaufman 		cmd.add_modify = STA_MODE_MODIFY;
9849f9af3d7SLiad Kaufman 		cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
9859f9af3d7SLiad Kaufman 		cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
9869f9af3d7SLiad Kaufman 		cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
9879f9af3d7SLiad Kaufman 
9889f9af3d7SLiad Kaufman 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
9899f9af3d7SLiad Kaufman 					   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
9909f9af3d7SLiad Kaufman 		if (!ret) {
9919f9af3d7SLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm,
9929f9af3d7SLiad Kaufman 					    "TXQ #%d is now aggregated again\n",
9939f9af3d7SLiad Kaufman 					    queue);
9949f9af3d7SLiad Kaufman 
9959f9af3d7SLiad Kaufman 			/* Mark queue intenally as aggregating again */
9969f9af3d7SLiad Kaufman 			iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
9979f9af3d7SLiad Kaufman 		}
9989f9af3d7SLiad Kaufman 	}
9999f9af3d7SLiad Kaufman 
10009f9af3d7SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
10019f9af3d7SLiad Kaufman }
10029f9af3d7SLiad Kaufman 
100399448a8cSJohannes Berg /*
100499448a8cSJohannes Berg  * Remove inactive TIDs of a given queue.
100599448a8cSJohannes Berg  * If all queue TIDs are inactive - mark the queue as inactive
100699448a8cSJohannes Berg  * If only some the queue TIDs are inactive - unmap them from the queue
1007724fe771SJohannes Berg  *
1008724fe771SJohannes Berg  * Returns %true if all TIDs were removed and the queue could be reused.
100999448a8cSJohannes Berg  */
1010724fe771SJohannes Berg static bool iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
101199448a8cSJohannes Berg 					 struct iwl_mvm_sta *mvmsta, int queue,
101290d2d94cSJohannes Berg 					 unsigned long tid_bitmap,
1013b3a87f11SJohannes Berg 					 unsigned long *unshare_queues,
1014b3a87f11SJohannes Berg 					 unsigned long *changetid_queues)
101599448a8cSJohannes Berg {
101699448a8cSJohannes Berg 	int tid;
101799448a8cSJohannes Berg 
101899448a8cSJohannes Berg 	lockdep_assert_held(&mvmsta->lock);
1019f3f240f9SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
102099448a8cSJohannes Berg 
102199448a8cSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1022724fe771SJohannes Berg 		return false;
102399448a8cSJohannes Berg 
102499448a8cSJohannes Berg 	/* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
102599448a8cSJohannes Berg 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
102699448a8cSJohannes Berg 		/* If some TFDs are still queued - don't mark TID as inactive */
102799448a8cSJohannes Berg 		if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid]))
102899448a8cSJohannes Berg 			tid_bitmap &= ~BIT(tid);
102999448a8cSJohannes Berg 
103099448a8cSJohannes Berg 		/* Don't mark as inactive any TID that has an active BA */
103199448a8cSJohannes Berg 		if (mvmsta->tid_data[tid].state != IWL_AGG_OFF)
103299448a8cSJohannes Berg 			tid_bitmap &= ~BIT(tid);
103399448a8cSJohannes Berg 	}
103499448a8cSJohannes Berg 
1035724fe771SJohannes Berg 	/* If all TIDs in the queue are inactive - return it can be reused */
103699448a8cSJohannes Berg 	if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
1037724fe771SJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Queue %d is inactive\n", queue);
1038724fe771SJohannes Berg 		return true;
103999448a8cSJohannes Berg 	}
104099448a8cSJohannes Berg 
104199448a8cSJohannes Berg 	/*
104299448a8cSJohannes Berg 	 * If we are here, this is a shared queue and not all TIDs timed-out.
104399448a8cSJohannes Berg 	 * Remove the ones that did.
104499448a8cSJohannes Berg 	 */
104599448a8cSJohannes Berg 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1046b3a87f11SJohannes Berg 		u16 tid_bitmap;
104799448a8cSJohannes Berg 
104899448a8cSJohannes Berg 		mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
104999448a8cSJohannes Berg 		mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
105099448a8cSJohannes Berg 
1051b3a87f11SJohannes Berg 		tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1052b3a87f11SJohannes Berg 
1053b3a87f11SJohannes Berg 		/*
1054b3a87f11SJohannes Berg 		 * We need to take into account a situation in which a TXQ was
1055b3a87f11SJohannes Berg 		 * allocated to TID x, and then turned shared by adding TIDs y
1056b3a87f11SJohannes Berg 		 * and z. If TID x becomes inactive and is removed from the TXQ,
1057b3a87f11SJohannes Berg 		 * ownership must be given to one of the remaining TIDs.
1058b3a87f11SJohannes Berg 		 * This is mainly because if TID x continues - a new queue can't
1059b3a87f11SJohannes Berg 		 * be allocated for it as long as it is an owner of another TXQ.
1060b3a87f11SJohannes Berg 		 *
1061b3a87f11SJohannes Berg 		 * Mark this queue in the right bitmap, we'll send the command
1062b3a87f11SJohannes Berg 		 * to the firmware later.
1063b3a87f11SJohannes Berg 		 */
1064b3a87f11SJohannes Berg 		if (!(tid_bitmap & BIT(mvm->queue_info[queue].txq_tid)))
1065b3a87f11SJohannes Berg 			set_bit(queue, changetid_queues);
1066b3a87f11SJohannes Berg 
106799448a8cSJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm,
106899448a8cSJohannes Berg 				    "Removing inactive TID %d from shared Q:%d\n",
106999448a8cSJohannes Berg 				    tid, queue);
107099448a8cSJohannes Berg 	}
107199448a8cSJohannes Berg 
107299448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
107399448a8cSJohannes Berg 			    "TXQ #%d left with tid bitmap 0x%x\n", queue,
107499448a8cSJohannes Berg 			    mvm->queue_info[queue].tid_bitmap);
107599448a8cSJohannes Berg 
107699448a8cSJohannes Berg 	/*
107799448a8cSJohannes Berg 	 * There may be different TIDs with the same mac queues, so make
107899448a8cSJohannes Berg 	 * sure all TIDs have existing corresponding mac queues enabled
107999448a8cSJohannes Berg 	 */
108099448a8cSJohannes Berg 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
108199448a8cSJohannes Berg 
108299448a8cSJohannes Berg 	/* If the queue is marked as shared - "unshare" it */
10831c14089eSJohannes Berg 	if (hweight16(mvm->queue_info[queue].tid_bitmap) == 1 &&
108499448a8cSJohannes Berg 	    mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
108599448a8cSJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
108699448a8cSJohannes Berg 				    queue);
108790d2d94cSJohannes Berg 		set_bit(queue, unshare_queues);
108899448a8cSJohannes Berg 	}
1089724fe771SJohannes Berg 
1090724fe771SJohannes Berg 	return false;
109199448a8cSJohannes Berg }
109299448a8cSJohannes Berg 
1093724fe771SJohannes Berg /*
1094724fe771SJohannes Berg  * Check for inactivity - this includes checking if any queue
1095724fe771SJohannes Berg  * can be unshared and finding one (and only one) that can be
1096724fe771SJohannes Berg  * reused.
1097724fe771SJohannes Berg  * This function is also invoked as a sort of clean-up task,
1098724fe771SJohannes Berg  * in which case @alloc_for_sta is IWL_MVM_INVALID_STA.
1099724fe771SJohannes Berg  *
1100724fe771SJohannes Berg  * Returns the queue number, or -ENOSPC.
1101724fe771SJohannes Berg  */
1102724fe771SJohannes Berg static int iwl_mvm_inactivity_check(struct iwl_mvm *mvm, u8 alloc_for_sta)
110399448a8cSJohannes Berg {
110499448a8cSJohannes Berg 	unsigned long now = jiffies;
110590d2d94cSJohannes Berg 	unsigned long unshare_queues = 0;
1106b3a87f11SJohannes Berg 	unsigned long changetid_queues = 0;
1107724fe771SJohannes Berg 	int i, ret, free_queue = -ENOSPC;
1108cfbc6c4cSSara Sharon 	struct ieee80211_sta *queue_owner  = NULL;
110999448a8cSJohannes Berg 
1110df2a2245SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
1111df2a2245SJohannes Berg 
111299448a8cSJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm))
1113724fe771SJohannes Berg 		return -ENOSPC;
111499448a8cSJohannes Berg 
111599448a8cSJohannes Berg 	rcu_read_lock();
111699448a8cSJohannes Berg 
1117459ab045SJohannes Berg 	/* we skip the CMD queue below by starting at 1 */
1118459ab045SJohannes Berg 	BUILD_BUG_ON(IWL_MVM_DQA_CMD_QUEUE != 0);
1119459ab045SJohannes Berg 
1120459ab045SJohannes Berg 	for (i = 1; i < IWL_MAX_HW_QUEUES; i++) {
112199448a8cSJohannes Berg 		struct ieee80211_sta *sta;
112299448a8cSJohannes Berg 		struct iwl_mvm_sta *mvmsta;
112399448a8cSJohannes Berg 		u8 sta_id;
112499448a8cSJohannes Berg 		int tid;
112599448a8cSJohannes Berg 		unsigned long inactive_tid_bitmap = 0;
112699448a8cSJohannes Berg 		unsigned long queue_tid_bitmap;
112799448a8cSJohannes Berg 
112899448a8cSJohannes Berg 		queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
1129459ab045SJohannes Berg 		if (!queue_tid_bitmap)
1130459ab045SJohannes Berg 			continue;
113199448a8cSJohannes Berg 
113299448a8cSJohannes Berg 		/* If TXQ isn't in active use anyway - nothing to do here... */
113399448a8cSJohannes Berg 		if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
1134459ab045SJohannes Berg 		    mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED)
113599448a8cSJohannes Berg 			continue;
113699448a8cSJohannes Berg 
113799448a8cSJohannes Berg 		/* Check to see if there are inactive TIDs on this queue */
113899448a8cSJohannes Berg 		for_each_set_bit(tid, &queue_tid_bitmap,
113999448a8cSJohannes Berg 				 IWL_MAX_TID_COUNT + 1) {
114099448a8cSJohannes Berg 			if (time_after(mvm->queue_info[i].last_frame_time[tid] +
114199448a8cSJohannes Berg 				       IWL_MVM_DQA_QUEUE_TIMEOUT, now))
114299448a8cSJohannes Berg 				continue;
114399448a8cSJohannes Berg 
114499448a8cSJohannes Berg 			inactive_tid_bitmap |= BIT(tid);
114599448a8cSJohannes Berg 		}
114699448a8cSJohannes Berg 
114799448a8cSJohannes Berg 		/* If all TIDs are active - finish check on this queue */
114899448a8cSJohannes Berg 		if (!inactive_tid_bitmap)
114999448a8cSJohannes Berg 			continue;
115099448a8cSJohannes Berg 
115199448a8cSJohannes Berg 		/*
115299448a8cSJohannes Berg 		 * If we are here - the queue hadn't been served recently and is
115399448a8cSJohannes Berg 		 * in use
115499448a8cSJohannes Berg 		 */
115599448a8cSJohannes Berg 
115699448a8cSJohannes Berg 		sta_id = mvm->queue_info[i].ra_sta_id;
115799448a8cSJohannes Berg 		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
115899448a8cSJohannes Berg 
115999448a8cSJohannes Berg 		/*
116099448a8cSJohannes Berg 		 * If the STA doesn't exist anymore, it isn't an error. It could
116199448a8cSJohannes Berg 		 * be that it was removed since getting the queues, and in this
116299448a8cSJohannes Berg 		 * case it should've inactivated its queues anyway.
116399448a8cSJohannes Berg 		 */
116499448a8cSJohannes Berg 		if (IS_ERR_OR_NULL(sta))
116599448a8cSJohannes Berg 			continue;
116699448a8cSJohannes Berg 
116799448a8cSJohannes Berg 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
116899448a8cSJohannes Berg 
1169f3f240f9SJohannes Berg 		spin_lock_bh(&mvmsta->lock);
1170724fe771SJohannes Berg 		ret = iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
117190d2d94cSJohannes Berg 						   inactive_tid_bitmap,
1172b3a87f11SJohannes Berg 						   &unshare_queues,
1173b3a87f11SJohannes Berg 						   &changetid_queues);
1174cfbc6c4cSSara Sharon 		if (ret >= 0 && free_queue < 0) {
1175cfbc6c4cSSara Sharon 			queue_owner = sta;
1176724fe771SJohannes Berg 			free_queue = ret;
1177cfbc6c4cSSara Sharon 		}
1178459ab045SJohannes Berg 		/* only unlock sta lock - we still need the queue info lock */
1179f3f240f9SJohannes Berg 		spin_unlock_bh(&mvmsta->lock);
118099448a8cSJohannes Berg 	}
118199448a8cSJohannes Berg 
1182df2a2245SJohannes Berg 
1183df2a2245SJohannes Berg 	/* Reconfigure queues requiring reconfiguation */
118490d2d94cSJohannes Berg 	for_each_set_bit(i, &unshare_queues, IWL_MAX_HW_QUEUES)
118590d2d94cSJohannes Berg 		iwl_mvm_unshare_queue(mvm, i);
1186b3a87f11SJohannes Berg 	for_each_set_bit(i, &changetid_queues, IWL_MAX_HW_QUEUES)
1187b3a87f11SJohannes Berg 		iwl_mvm_change_queue_tid(mvm, i);
1188724fe771SJohannes Berg 
1189724fe771SJohannes Berg 	if (free_queue >= 0 && alloc_for_sta != IWL_MVM_INVALID_STA) {
1190cfbc6c4cSSara Sharon 		ret = iwl_mvm_free_inactive_queue(mvm, free_queue, queue_owner,
1191724fe771SJohannes Berg 						  alloc_for_sta);
1192cfbc6c4cSSara Sharon 		if (ret) {
1193cfbc6c4cSSara Sharon 			rcu_read_unlock();
1194724fe771SJohannes Berg 			return ret;
1195724fe771SJohannes Berg 		}
1196cfbc6c4cSSara Sharon 	}
1197cfbc6c4cSSara Sharon 
1198cfbc6c4cSSara Sharon 	rcu_read_unlock();
1199724fe771SJohannes Berg 
1200724fe771SJohannes Berg 	return free_queue;
120199448a8cSJohannes Berg }
120299448a8cSJohannes Berg 
1203c20e08b0SJohannes Berg static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
1204cfbc6c4cSSara Sharon 				   struct ieee80211_sta *sta, u8 ac, int tid)
1205c20e08b0SJohannes Berg {
1206c20e08b0SJohannes Berg 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1207c20e08b0SJohannes Berg 	struct iwl_trans_txq_scd_cfg cfg = {
1208c20e08b0SJohannes Berg 		.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
1209c20e08b0SJohannes Berg 		.sta_id = mvmsta->sta_id,
1210c20e08b0SJohannes Berg 		.tid = tid,
1211c20e08b0SJohannes Berg 		.frame_limit = IWL_FRAME_LIMIT,
1212c20e08b0SJohannes Berg 	};
1213c20e08b0SJohannes Berg 	unsigned int wdg_timeout =
1214c20e08b0SJohannes Berg 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1215c20e08b0SJohannes Berg 	int queue = -1;
1216c20e08b0SJohannes Berg 	unsigned long disable_agg_tids = 0;
1217c20e08b0SJohannes Berg 	enum iwl_mvm_agg_state queue_state;
1218c20e08b0SJohannes Berg 	bool shared_queue = false, inc_ssn;
1219c20e08b0SJohannes Berg 	int ssn;
1220c20e08b0SJohannes Berg 	unsigned long tfd_queue_mask;
1221c20e08b0SJohannes Berg 	int ret;
1222c20e08b0SJohannes Berg 
1223c20e08b0SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
1224c20e08b0SJohannes Berg 
1225c20e08b0SJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm))
1226c20e08b0SJohannes Berg 		return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
1227c20e08b0SJohannes Berg 
1228c20e08b0SJohannes Berg 	spin_lock_bh(&mvmsta->lock);
1229c20e08b0SJohannes Berg 	tfd_queue_mask = mvmsta->tfd_queue_msk;
123035739348SSara Sharon 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1231c20e08b0SJohannes Berg 	spin_unlock_bh(&mvmsta->lock);
1232c20e08b0SJohannes Berg 
1233cfbc6c4cSSara Sharon 	if (tid == IWL_MAX_TID_COUNT) {
1234c20e08b0SJohannes Berg 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1235c20e08b0SJohannes Berg 						IWL_MVM_DQA_MIN_MGMT_QUEUE,
1236c20e08b0SJohannes Berg 						IWL_MVM_DQA_MAX_MGMT_QUEUE);
1237c20e08b0SJohannes Berg 		if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
1238c20e08b0SJohannes Berg 			IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
1239c20e08b0SJohannes Berg 					    queue);
1240c20e08b0SJohannes Berg 
1241c20e08b0SJohannes Berg 		/* If no such queue is found, we'll use a DATA queue instead */
1242c20e08b0SJohannes Berg 	}
1243c20e08b0SJohannes Berg 
1244c20e08b0SJohannes Berg 	if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
1245c20e08b0SJohannes Berg 	    (mvm->queue_info[mvmsta->reserved_queue].status ==
1246724fe771SJohannes Berg 			IWL_MVM_QUEUE_RESERVED)) {
1247c20e08b0SJohannes Berg 		queue = mvmsta->reserved_queue;
1248c20e08b0SJohannes Berg 		mvm->queue_info[queue].reserved = true;
1249c20e08b0SJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
1250c20e08b0SJohannes Berg 	}
1251c20e08b0SJohannes Berg 
1252c20e08b0SJohannes Berg 	if (queue < 0)
1253c20e08b0SJohannes Berg 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1254c20e08b0SJohannes Berg 						IWL_MVM_DQA_MIN_DATA_QUEUE,
1255c20e08b0SJohannes Berg 						IWL_MVM_DQA_MAX_DATA_QUEUE);
1256724fe771SJohannes Berg 	if (queue < 0) {
1257724fe771SJohannes Berg 		/* try harder - perhaps kill an inactive queue */
1258724fe771SJohannes Berg 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1259c20e08b0SJohannes Berg 	}
1260c20e08b0SJohannes Berg 
1261c20e08b0SJohannes Berg 	/* No free queue - we'll have to share */
1262c20e08b0SJohannes Berg 	if (queue <= 0) {
1263c20e08b0SJohannes Berg 		queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
1264c20e08b0SJohannes Berg 		if (queue > 0) {
1265c20e08b0SJohannes Berg 			shared_queue = true;
1266c20e08b0SJohannes Berg 			mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
1267c20e08b0SJohannes Berg 		}
1268c20e08b0SJohannes Berg 	}
1269c20e08b0SJohannes Berg 
1270c20e08b0SJohannes Berg 	/*
1271c20e08b0SJohannes Berg 	 * Mark TXQ as ready, even though it hasn't been fully configured yet,
1272c20e08b0SJohannes Berg 	 * to make sure no one else takes it.
1273c20e08b0SJohannes Berg 	 * This will allow avoiding re-acquiring the lock at the end of the
1274c20e08b0SJohannes Berg 	 * configuration. On error we'll mark it back as free.
1275c20e08b0SJohannes Berg 	 */
1276c20e08b0SJohannes Berg 	if (queue > 0 && !shared_queue)
1277c20e08b0SJohannes Berg 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1278c20e08b0SJohannes Berg 
1279c20e08b0SJohannes Berg 	/* This shouldn't happen - out of queues */
1280c20e08b0SJohannes Berg 	if (WARN_ON(queue <= 0)) {
1281c20e08b0SJohannes Berg 		IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
1282c20e08b0SJohannes Berg 			tid, cfg.sta_id);
1283c20e08b0SJohannes Berg 		return queue;
1284c20e08b0SJohannes Berg 	}
1285c20e08b0SJohannes Berg 
1286c20e08b0SJohannes Berg 	/*
1287c20e08b0SJohannes Berg 	 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
1288c20e08b0SJohannes Berg 	 * but for configuring the SCD to send A-MPDUs we need to mark the queue
1289c20e08b0SJohannes Berg 	 * as aggregatable.
1290c20e08b0SJohannes Berg 	 * Mark all DATA queues as allowing to be aggregated at some point
1291c20e08b0SJohannes Berg 	 */
1292c20e08b0SJohannes Berg 	cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1293c20e08b0SJohannes Berg 			 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1294c20e08b0SJohannes Berg 
1295c20e08b0SJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
1296c20e08b0SJohannes Berg 			    "Allocating %squeue #%d to sta %d on tid %d\n",
1297c20e08b0SJohannes Berg 			    shared_queue ? "shared " : "", queue,
1298c20e08b0SJohannes Berg 			    mvmsta->sta_id, tid);
1299c20e08b0SJohannes Berg 
1300c20e08b0SJohannes Berg 	if (shared_queue) {
1301c20e08b0SJohannes Berg 		/* Disable any open aggs on this queue */
1302c20e08b0SJohannes Berg 		disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
1303c20e08b0SJohannes Berg 
1304c20e08b0SJohannes Berg 		if (disable_agg_tids) {
1305c20e08b0SJohannes Berg 			IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
1306c20e08b0SJohannes Berg 					    queue);
1307c20e08b0SJohannes Berg 			iwl_mvm_invalidate_sta_queue(mvm, queue,
1308c20e08b0SJohannes Berg 						     disable_agg_tids, false);
1309c20e08b0SJohannes Berg 		}
1310c20e08b0SJohannes Berg 	}
1311c20e08b0SJohannes Berg 
1312cfbc6c4cSSara Sharon 	inc_ssn = iwl_mvm_enable_txq(mvm, sta, queue, ssn, &cfg, wdg_timeout);
1313c20e08b0SJohannes Berg 
1314c20e08b0SJohannes Berg 	/*
1315c20e08b0SJohannes Berg 	 * Mark queue as shared in transport if shared
1316c20e08b0SJohannes Berg 	 * Note this has to be done after queue enablement because enablement
1317c20e08b0SJohannes Berg 	 * can also set this value, and there is no indication there to shared
1318c20e08b0SJohannes Berg 	 * queues
1319c20e08b0SJohannes Berg 	 */
1320c20e08b0SJohannes Berg 	if (shared_queue)
1321c20e08b0SJohannes Berg 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
1322c20e08b0SJohannes Berg 
1323c20e08b0SJohannes Berg 	spin_lock_bh(&mvmsta->lock);
1324c20e08b0SJohannes Berg 	/*
1325c20e08b0SJohannes Berg 	 * This looks racy, but it is not. We have only one packet for
1326c20e08b0SJohannes Berg 	 * this ra/tid in our Tx path since we stop the Qdisc when we
1327c20e08b0SJohannes Berg 	 * need to allocate a new TFD queue.
1328c20e08b0SJohannes Berg 	 */
132935739348SSara Sharon 	if (inc_ssn) {
1330c20e08b0SJohannes Berg 		mvmsta->tid_data[tid].seq_number += 0x10;
133135739348SSara Sharon 		ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
133235739348SSara Sharon 	}
1333c20e08b0SJohannes Berg 	mvmsta->tid_data[tid].txq_id = queue;
1334c20e08b0SJohannes Berg 	mvmsta->tfd_queue_msk |= BIT(queue);
1335c20e08b0SJohannes Berg 	queue_state = mvmsta->tid_data[tid].state;
1336c20e08b0SJohannes Berg 
1337c20e08b0SJohannes Berg 	if (mvmsta->reserved_queue == queue)
1338c20e08b0SJohannes Berg 		mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
1339c20e08b0SJohannes Berg 	spin_unlock_bh(&mvmsta->lock);
1340c20e08b0SJohannes Berg 
1341c20e08b0SJohannes Berg 	if (!shared_queue) {
1342c20e08b0SJohannes Berg 		ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
1343c20e08b0SJohannes Berg 		if (ret)
1344c20e08b0SJohannes Berg 			goto out_err;
1345c20e08b0SJohannes Berg 
1346c20e08b0SJohannes Berg 		/* If we need to re-enable aggregations... */
1347c20e08b0SJohannes Berg 		if (queue_state == IWL_AGG_ON) {
1348c20e08b0SJohannes Berg 			ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1349c20e08b0SJohannes Berg 			if (ret)
1350c20e08b0SJohannes Berg 				goto out_err;
1351c20e08b0SJohannes Berg 		}
1352c20e08b0SJohannes Berg 	} else {
1353c20e08b0SJohannes Berg 		/* Redirect queue, if needed */
1354cfbc6c4cSSara Sharon 		ret = iwl_mvm_redirect_queue(mvm, queue, tid, ac, ssn,
1355cfbc6c4cSSara Sharon 					     wdg_timeout, false,
1356cfbc6c4cSSara Sharon 					     iwl_mvm_txq_from_tid(sta, tid));
1357c20e08b0SJohannes Berg 		if (ret)
1358c20e08b0SJohannes Berg 			goto out_err;
1359c20e08b0SJohannes Berg 	}
1360c20e08b0SJohannes Berg 
1361c20e08b0SJohannes Berg 	return 0;
1362c20e08b0SJohannes Berg 
1363c20e08b0SJohannes Berg out_err:
1364cfbc6c4cSSara Sharon 	iwl_mvm_disable_txq(mvm, sta, queue, tid, 0);
1365c20e08b0SJohannes Berg 
1366c20e08b0SJohannes Berg 	return ret;
1367c20e08b0SJohannes Berg }
1368c20e08b0SJohannes Berg 
136924afba76SLiad Kaufman static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
137024afba76SLiad Kaufman {
137124afba76SLiad Kaufman 	if (tid == IWL_MAX_TID_COUNT)
137224afba76SLiad Kaufman 		return IEEE80211_AC_VO; /* MGMT */
137324afba76SLiad Kaufman 
137424afba76SLiad Kaufman 	return tid_to_mac80211_ac[tid];
137524afba76SLiad Kaufman }
137624afba76SLiad Kaufman 
137724afba76SLiad Kaufman void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
137824afba76SLiad Kaufman {
137924afba76SLiad Kaufman 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
138024afba76SLiad Kaufman 					   add_stream_wk);
138124afba76SLiad Kaufman 
138224afba76SLiad Kaufman 	mutex_lock(&mvm->mutex);
138324afba76SLiad Kaufman 
1384724fe771SJohannes Berg 	iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
13859f9af3d7SLiad Kaufman 
1386cfbc6c4cSSara Sharon 	while (!list_empty(&mvm->add_stream_txqs)) {
1387cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq;
1388cfbc6c4cSSara Sharon 		struct ieee80211_txq *txq;
1389cfbc6c4cSSara Sharon 		u8 tid;
139024afba76SLiad Kaufman 
1391cfbc6c4cSSara Sharon 		mvmtxq = list_first_entry(&mvm->add_stream_txqs,
1392cfbc6c4cSSara Sharon 					  struct iwl_mvm_txq, list);
139324afba76SLiad Kaufman 
1394cfbc6c4cSSara Sharon 		txq = container_of((void *)mvmtxq, struct ieee80211_txq,
1395cfbc6c4cSSara Sharon 				   drv_priv);
1396cfbc6c4cSSara Sharon 		tid = txq->tid;
1397cfbc6c4cSSara Sharon 		if (tid == IEEE80211_NUM_TIDS)
1398cfbc6c4cSSara Sharon 			tid = IWL_MAX_TID_COUNT;
1399cfbc6c4cSSara Sharon 
1400cfbc6c4cSSara Sharon 		iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, tid);
1401cfbc6c4cSSara Sharon 		list_del_init(&mvmtxq->list);
1402cfbc6c4cSSara Sharon 		iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
140324afba76SLiad Kaufman 	}
140424afba76SLiad Kaufman 
140524afba76SLiad Kaufman 	mutex_unlock(&mvm->mutex);
140624afba76SLiad Kaufman }
140724afba76SLiad Kaufman 
140824afba76SLiad Kaufman static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1409d5216a28SLiad Kaufman 				      struct ieee80211_sta *sta,
1410d5216a28SLiad Kaufman 				      enum nl80211_iftype vif_type)
141124afba76SLiad Kaufman {
141224afba76SLiad Kaufman 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
141324afba76SLiad Kaufman 	int queue;
141424afba76SLiad Kaufman 
1415396952eeSSara Sharon 	/* queue reserving is disabled on new TX path */
1416396952eeSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1417396952eeSSara Sharon 		return 0;
1418396952eeSSara Sharon 
1419724fe771SJohannes Berg 	/* run the general cleanup/unsharing of queues */
1420724fe771SJohannes Berg 	iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
14219794c64fSLiad Kaufman 
142224afba76SLiad Kaufman 	/* Make sure we have free resources for this STA */
1423d5216a28SLiad Kaufman 	if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
14241c14089eSJohannes Berg 	    !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].tid_bitmap &&
1425cf961e16SLiad Kaufman 	    (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1426cf961e16SLiad Kaufman 	     IWL_MVM_QUEUE_FREE))
1427d5216a28SLiad Kaufman 		queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1428d5216a28SLiad Kaufman 	else
14299794c64fSLiad Kaufman 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
14309794c64fSLiad Kaufman 						IWL_MVM_DQA_MIN_DATA_QUEUE,
143124afba76SLiad Kaufman 						IWL_MVM_DQA_MAX_DATA_QUEUE);
143224afba76SLiad Kaufman 	if (queue < 0) {
1433724fe771SJohannes Berg 		/* try again - this time kick out a queue if needed */
1434724fe771SJohannes Berg 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1435724fe771SJohannes Berg 		if (queue < 0) {
143624afba76SLiad Kaufman 			IWL_ERR(mvm, "No available queues for new station\n");
143724afba76SLiad Kaufman 			return -ENOSPC;
1438724fe771SJohannes Berg 		}
143924afba76SLiad Kaufman 	}
1440cf961e16SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
144124afba76SLiad Kaufman 
144224afba76SLiad Kaufman 	mvmsta->reserved_queue = queue;
144324afba76SLiad Kaufman 
144424afba76SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
144524afba76SLiad Kaufman 			    queue, mvmsta->sta_id);
144624afba76SLiad Kaufman 
144724afba76SLiad Kaufman 	return 0;
144824afba76SLiad Kaufman }
144924afba76SLiad Kaufman 
14508d98ae6eSLiad Kaufman /*
14518d98ae6eSLiad Kaufman  * In DQA mode, after a HW restart the queues should be allocated as before, in
14528d98ae6eSLiad Kaufman  * order to avoid race conditions when there are shared queues. This function
14538d98ae6eSLiad Kaufman  * does the re-mapping and queue allocation.
14548d98ae6eSLiad Kaufman  *
14558d98ae6eSLiad Kaufman  * Note that re-enabling aggregations isn't done in this function.
14568d98ae6eSLiad Kaufman  */
14578d98ae6eSLiad Kaufman static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1458cfbc6c4cSSara Sharon 						 struct ieee80211_sta *sta)
14598d98ae6eSLiad Kaufman {
1460cfbc6c4cSSara Sharon 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1461cfbc6c4cSSara Sharon 	unsigned int wdg =
14628d98ae6eSLiad Kaufman 		iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
14638d98ae6eSLiad Kaufman 	int i;
14648d98ae6eSLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
14658d98ae6eSLiad Kaufman 		.sta_id = mvm_sta->sta_id,
14668d98ae6eSLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
14678d98ae6eSLiad Kaufman 	};
14688d98ae6eSLiad Kaufman 
146903c902bfSJohannes Berg 	/* Make sure reserved queue is still marked as such (if allocated) */
147003c902bfSJohannes Berg 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
14718d98ae6eSLiad Kaufman 		mvm->queue_info[mvm_sta->reserved_queue].status =
14728d98ae6eSLiad Kaufman 			IWL_MVM_QUEUE_RESERVED;
14738d98ae6eSLiad Kaufman 
14748d98ae6eSLiad Kaufman 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
14758d98ae6eSLiad Kaufman 		struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
14768d98ae6eSLiad Kaufman 		int txq_id = tid_data->txq_id;
14778d98ae6eSLiad Kaufman 		int ac;
14788d98ae6eSLiad Kaufman 
14796862fceeSSara Sharon 		if (txq_id == IWL_MVM_INVALID_QUEUE)
14808d98ae6eSLiad Kaufman 			continue;
14818d98ae6eSLiad Kaufman 
14828d98ae6eSLiad Kaufman 		ac = tid_to_mac80211_ac[i];
14838d98ae6eSLiad Kaufman 
1484310181ecSSara Sharon 		if (iwl_mvm_has_new_tx_api(mvm)) {
1485310181ecSSara Sharon 			IWL_DEBUG_TX_QUEUES(mvm,
1486310181ecSSara Sharon 					    "Re-mapping sta %d tid %d\n",
1487310181ecSSara Sharon 					    mvm_sta->sta_id, i);
1488cfbc6c4cSSara Sharon 			txq_id = iwl_mvm_tvqm_enable_txq(mvm, mvm_sta->sta_id,
1489cfbc6c4cSSara Sharon 							 i, wdg);
1490310181ecSSara Sharon 			tid_data->txq_id = txq_id;
14915d39051aSLiad Kaufman 
14925d39051aSLiad Kaufman 			/*
14935d39051aSLiad Kaufman 			 * Since we don't set the seq number after reset, and HW
14945d39051aSLiad Kaufman 			 * sets it now, FW reset will cause the seq num to start
14955d39051aSLiad Kaufman 			 * at 0 again, so driver will need to update it
14965d39051aSLiad Kaufman 			 * internally as well, so it keeps in sync with real val
14975d39051aSLiad Kaufman 			 */
14985d39051aSLiad Kaufman 			tid_data->seq_number = 0;
1499310181ecSSara Sharon 		} else {
1500310181ecSSara Sharon 			u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1501310181ecSSara Sharon 
15028d98ae6eSLiad Kaufman 			cfg.tid = i;
1503cf6c6ea3SEmmanuel Grumbach 			cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
15048d98ae6eSLiad Kaufman 			cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1505310181ecSSara Sharon 					 txq_id ==
1506310181ecSSara Sharon 					 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
15078d98ae6eSLiad Kaufman 
15088d98ae6eSLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm,
15098d98ae6eSLiad Kaufman 					    "Re-mapping sta %d tid %d to queue %d\n",
15108d98ae6eSLiad Kaufman 					    mvm_sta->sta_id, i, txq_id);
15118d98ae6eSLiad Kaufman 
1512cfbc6c4cSSara Sharon 			iwl_mvm_enable_txq(mvm, sta, txq_id, seq, &cfg, wdg);
15138d98ae6eSLiad Kaufman 			mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
15148d98ae6eSLiad Kaufman 		}
151534e10860SSara Sharon 	}
15168d98ae6eSLiad Kaufman }
15178d98ae6eSLiad Kaufman 
1518732d06e9SShaul Triebitz static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1519732d06e9SShaul Triebitz 				      struct iwl_mvm_int_sta *sta,
1520732d06e9SShaul Triebitz 				      const u8 *addr,
1521732d06e9SShaul Triebitz 				      u16 mac_id, u16 color)
1522732d06e9SShaul Triebitz {
1523732d06e9SShaul Triebitz 	struct iwl_mvm_add_sta_cmd cmd;
1524732d06e9SShaul Triebitz 	int ret;
15253f497de9SLuca Coelho 	u32 status = ADD_STA_SUCCESS;
1526732d06e9SShaul Triebitz 
1527732d06e9SShaul Triebitz 	lockdep_assert_held(&mvm->mutex);
1528732d06e9SShaul Triebitz 
1529732d06e9SShaul Triebitz 	memset(&cmd, 0, sizeof(cmd));
1530732d06e9SShaul Triebitz 	cmd.sta_id = sta->sta_id;
1531732d06e9SShaul Triebitz 	cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1532732d06e9SShaul Triebitz 							     color));
1533732d06e9SShaul Triebitz 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1534732d06e9SShaul Triebitz 		cmd.station_type = sta->type;
1535732d06e9SShaul Triebitz 
1536732d06e9SShaul Triebitz 	if (!iwl_mvm_has_new_tx_api(mvm))
1537732d06e9SShaul Triebitz 		cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1538732d06e9SShaul Triebitz 	cmd.tid_disable_tx = cpu_to_le16(0xffff);
1539732d06e9SShaul Triebitz 
1540732d06e9SShaul Triebitz 	if (addr)
1541732d06e9SShaul Triebitz 		memcpy(cmd.addr, addr, ETH_ALEN);
1542732d06e9SShaul Triebitz 
1543732d06e9SShaul Triebitz 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1544732d06e9SShaul Triebitz 					  iwl_mvm_add_sta_cmd_size(mvm),
1545732d06e9SShaul Triebitz 					  &cmd, &status);
1546732d06e9SShaul Triebitz 	if (ret)
1547732d06e9SShaul Triebitz 		return ret;
1548732d06e9SShaul Triebitz 
1549732d06e9SShaul Triebitz 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1550732d06e9SShaul Triebitz 	case ADD_STA_SUCCESS:
1551732d06e9SShaul Triebitz 		IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1552732d06e9SShaul Triebitz 		return 0;
1553732d06e9SShaul Triebitz 	default:
1554732d06e9SShaul Triebitz 		ret = -EIO;
1555732d06e9SShaul Triebitz 		IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1556732d06e9SShaul Triebitz 			status);
1557732d06e9SShaul Triebitz 		break;
1558732d06e9SShaul Triebitz 	}
1559732d06e9SShaul Triebitz 	return ret;
1560732d06e9SShaul Triebitz }
1561732d06e9SShaul Triebitz 
1562e705c121SKalle Valo int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1563e705c121SKalle Valo 		    struct ieee80211_vif *vif,
1564e705c121SKalle Valo 		    struct ieee80211_sta *sta)
1565e705c121SKalle Valo {
1566e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1567e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1568a571f5f6SSara Sharon 	struct iwl_mvm_rxq_dup_data *dup_data;
1569e705c121SKalle Valo 	int i, ret, sta_id;
1570732d06e9SShaul Triebitz 	bool sta_update = false;
1571732d06e9SShaul Triebitz 	unsigned int sta_flags = 0;
1572e705c121SKalle Valo 
1573e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1574e705c121SKalle Valo 
1575e705c121SKalle Valo 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1576e705c121SKalle Valo 		sta_id = iwl_mvm_find_free_sta_id(mvm,
1577e705c121SKalle Valo 						  ieee80211_vif_type_p2p(vif));
1578e705c121SKalle Valo 	else
1579e705c121SKalle Valo 		sta_id = mvm_sta->sta_id;
1580e705c121SKalle Valo 
15810ae98812SSara Sharon 	if (sta_id == IWL_MVM_INVALID_STA)
1582e705c121SKalle Valo 		return -ENOSPC;
1583e705c121SKalle Valo 
1584e705c121SKalle Valo 	spin_lock_init(&mvm_sta->lock);
1585e705c121SKalle Valo 
1586c8f54701SJohannes Berg 	/* if this is a HW restart re-alloc existing queues */
1587c8f54701SJohannes Berg 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1588732d06e9SShaul Triebitz 		struct iwl_mvm_int_sta tmp_sta = {
1589732d06e9SShaul Triebitz 			.sta_id = sta_id,
1590732d06e9SShaul Triebitz 			.type = mvm_sta->sta_type,
1591732d06e9SShaul Triebitz 		};
1592732d06e9SShaul Triebitz 
1593732d06e9SShaul Triebitz 		/*
1594732d06e9SShaul Triebitz 		 * First add an empty station since allocating
1595732d06e9SShaul Triebitz 		 * a queue requires a valid station
1596732d06e9SShaul Triebitz 		 */
1597732d06e9SShaul Triebitz 		ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1598732d06e9SShaul Triebitz 						 mvmvif->id, mvmvif->color);
1599732d06e9SShaul Triebitz 		if (ret)
1600732d06e9SShaul Triebitz 			goto err;
1601732d06e9SShaul Triebitz 
1602cfbc6c4cSSara Sharon 		iwl_mvm_realloc_queues_after_restart(mvm, sta);
1603732d06e9SShaul Triebitz 		sta_update = true;
1604732d06e9SShaul Triebitz 		sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
16058d98ae6eSLiad Kaufman 		goto update_fw;
16068d98ae6eSLiad Kaufman 	}
16078d98ae6eSLiad Kaufman 
1608e705c121SKalle Valo 	mvm_sta->sta_id = sta_id;
1609e705c121SKalle Valo 	mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1610e705c121SKalle Valo 						      mvmvif->color);
1611e705c121SKalle Valo 	mvm_sta->vif = vif;
1612a58bb468SLiad Kaufman 	if (!mvm->trans->cfg->gen2)
1613e705c121SKalle Valo 		mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1614a58bb468SLiad Kaufman 	else
1615a58bb468SLiad Kaufman 		mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
1616e705c121SKalle Valo 	mvm_sta->tx_protection = 0;
1617e705c121SKalle Valo 	mvm_sta->tt_tx_protection = false;
1618ced19f26SSara Sharon 	mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
1619e705c121SKalle Valo 
1620e705c121SKalle Valo 	/* HW restart, don't assume the memory has been zeroed */
1621e705c121SKalle Valo 	mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1622e705c121SKalle Valo 	mvm_sta->tfd_queue_msk = 0;
1623e705c121SKalle Valo 
1624e705c121SKalle Valo 	/* for HW restart - reset everything but the sequence number */
162524afba76SLiad Kaufman 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1626e705c121SKalle Valo 		u16 seq = mvm_sta->tid_data[i].seq_number;
1627e705c121SKalle Valo 		memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1628e705c121SKalle Valo 		mvm_sta->tid_data[i].seq_number = seq;
162924afba76SLiad Kaufman 
163024afba76SLiad Kaufman 		/*
163124afba76SLiad Kaufman 		 * Mark all queues for this STA as unallocated and defer TX
163224afba76SLiad Kaufman 		 * frames until the queue is allocated
163324afba76SLiad Kaufman 		 */
16346862fceeSSara Sharon 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1635e705c121SKalle Valo 	}
1636cfbc6c4cSSara Sharon 
1637cfbc6c4cSSara Sharon 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1638cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
1639cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1640cfbc6c4cSSara Sharon 
1641cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1642cfbc6c4cSSara Sharon 		INIT_LIST_HEAD(&mvmtxq->list);
1643fba8248eSSara Sharon 		atomic_set(&mvmtxq->tx_request, 0);
1644cfbc6c4cSSara Sharon 	}
1645cfbc6c4cSSara Sharon 
1646e705c121SKalle Valo 	mvm_sta->agg_tids = 0;
1647e705c121SKalle Valo 
1648a571f5f6SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) &&
1649a571f5f6SSara Sharon 	    !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
165092c4dca6SJohannes Berg 		int q;
165192c4dca6SJohannes Berg 
1652a571f5f6SSara Sharon 		dup_data = kcalloc(mvm->trans->num_rx_queues,
165392c4dca6SJohannes Berg 				   sizeof(*dup_data), GFP_KERNEL);
1654a571f5f6SSara Sharon 		if (!dup_data)
1655a571f5f6SSara Sharon 			return -ENOMEM;
165692c4dca6SJohannes Berg 		/*
165792c4dca6SJohannes Berg 		 * Initialize all the last_seq values to 0xffff which can never
165892c4dca6SJohannes Berg 		 * compare equal to the frame's seq_ctrl in the check in
165992c4dca6SJohannes Berg 		 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
166092c4dca6SJohannes Berg 		 * number and fragmented packets don't reach that function.
166192c4dca6SJohannes Berg 		 *
166292c4dca6SJohannes Berg 		 * This thus allows receiving a packet with seqno 0 and the
166392c4dca6SJohannes Berg 		 * retry bit set as the very first packet on a new TID.
166492c4dca6SJohannes Berg 		 */
166592c4dca6SJohannes Berg 		for (q = 0; q < mvm->trans->num_rx_queues; q++)
166692c4dca6SJohannes Berg 			memset(dup_data[q].last_seq, 0xff,
166792c4dca6SJohannes Berg 			       sizeof(dup_data[q].last_seq));
1668a571f5f6SSara Sharon 		mvm_sta->dup_data = dup_data;
1669a571f5f6SSara Sharon 	}
1670a571f5f6SSara Sharon 
1671c8f54701SJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm)) {
1672d5216a28SLiad Kaufman 		ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1673d5216a28SLiad Kaufman 						 ieee80211_vif_type_p2p(vif));
167424afba76SLiad Kaufman 		if (ret)
167524afba76SLiad Kaufman 			goto err;
167624afba76SLiad Kaufman 	}
167724afba76SLiad Kaufman 
16789f66a397SGregory Greenman 	/*
16799f66a397SGregory Greenman 	 * if rs is registered with mac80211, then "add station" will be handled
16809f66a397SGregory Greenman 	 * via the corresponding ops, otherwise need to notify rate scaling here
16819f66a397SGregory Greenman 	 */
16824243edb4SEmmanuel Grumbach 	if (iwl_mvm_has_tlc_offload(mvm))
16839f66a397SGregory Greenman 		iwl_mvm_rs_add_sta(mvm, mvm_sta);
16849f66a397SGregory Greenman 
16850dde2440SAvraham Stern 	iwl_mvm_toggle_tx_ant(mvm, &mvm_sta->tx_ant);
16860dde2440SAvraham Stern 
16878d98ae6eSLiad Kaufman update_fw:
1688732d06e9SShaul Triebitz 	ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
1689e705c121SKalle Valo 	if (ret)
1690e705c121SKalle Valo 		goto err;
1691e705c121SKalle Valo 
1692e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION) {
1693e705c121SKalle Valo 		if (!sta->tdls) {
16940ae98812SSara Sharon 			WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
1695e705c121SKalle Valo 			mvmvif->ap_sta_id = sta_id;
1696e705c121SKalle Valo 		} else {
16970ae98812SSara Sharon 			WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
1698e705c121SKalle Valo 		}
1699e705c121SKalle Valo 	}
1700e705c121SKalle Valo 
1701e705c121SKalle Valo 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1702e705c121SKalle Valo 
1703e705c121SKalle Valo 	return 0;
1704e705c121SKalle Valo 
1705e705c121SKalle Valo err:
1706e705c121SKalle Valo 	return ret;
1707e705c121SKalle Valo }
1708e705c121SKalle Valo 
1709e705c121SKalle Valo int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1710e705c121SKalle Valo 		      bool drain)
1711e705c121SKalle Valo {
1712e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
1713e705c121SKalle Valo 	int ret;
1714e705c121SKalle Valo 	u32 status;
1715e705c121SKalle Valo 
1716e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1717e705c121SKalle Valo 
1718e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1719e705c121SKalle Valo 	cmd.sta_id = mvmsta->sta_id;
1720e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
1721e705c121SKalle Valo 	cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1722e705c121SKalle Valo 	cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1723e705c121SKalle Valo 
1724e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
1725854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1726854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
1727e705c121SKalle Valo 					  &cmd, &status);
1728e705c121SKalle Valo 	if (ret)
1729e705c121SKalle Valo 		return ret;
1730e705c121SKalle Valo 
1731837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1732e705c121SKalle Valo 	case ADD_STA_SUCCESS:
1733e705c121SKalle Valo 		IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1734e705c121SKalle Valo 			       mvmsta->sta_id);
1735e705c121SKalle Valo 		break;
1736e705c121SKalle Valo 	default:
1737e705c121SKalle Valo 		ret = -EIO;
1738e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1739e705c121SKalle Valo 			mvmsta->sta_id);
1740e705c121SKalle Valo 		break;
1741e705c121SKalle Valo 	}
1742e705c121SKalle Valo 
1743e705c121SKalle Valo 	return ret;
1744e705c121SKalle Valo }
1745e705c121SKalle Valo 
1746e705c121SKalle Valo /*
1747e705c121SKalle Valo  * Remove a station from the FW table. Before sending the command to remove
1748e705c121SKalle Valo  * the station validate that the station is indeed known to the driver (sanity
1749e705c121SKalle Valo  * only).
1750e705c121SKalle Valo  */
1751e705c121SKalle Valo static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1752e705c121SKalle Valo {
1753e705c121SKalle Valo 	struct ieee80211_sta *sta;
1754e705c121SKalle Valo 	struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1755e705c121SKalle Valo 		.sta_id = sta_id,
1756e705c121SKalle Valo 	};
1757e705c121SKalle Valo 	int ret;
1758e705c121SKalle Valo 
1759e705c121SKalle Valo 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1760e705c121SKalle Valo 					lockdep_is_held(&mvm->mutex));
1761e705c121SKalle Valo 
1762e705c121SKalle Valo 	/* Note: internal stations are marked as error values */
1763e705c121SKalle Valo 	if (!sta) {
1764e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid station id\n");
1765e705c121SKalle Valo 		return -EINVAL;
1766e705c121SKalle Valo 	}
1767e705c121SKalle Valo 
1768e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1769e705c121SKalle Valo 				   sizeof(rm_sta_cmd), &rm_sta_cmd);
1770e705c121SKalle Valo 	if (ret) {
1771e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1772e705c121SKalle Valo 		return ret;
1773e705c121SKalle Valo 	}
1774e705c121SKalle Valo 
1775e705c121SKalle Valo 	return 0;
1776e705c121SKalle Valo }
1777e705c121SKalle Valo 
177824afba76SLiad Kaufman static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
177924afba76SLiad Kaufman 				       struct ieee80211_vif *vif,
1780cfbc6c4cSSara Sharon 				       struct ieee80211_sta *sta)
178124afba76SLiad Kaufman {
1782cfbc6c4cSSara Sharon 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
178324afba76SLiad Kaufman 	int i;
178424afba76SLiad Kaufman 
178524afba76SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
178624afba76SLiad Kaufman 
178724afba76SLiad Kaufman 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
17886862fceeSSara Sharon 		if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
178924afba76SLiad Kaufman 			continue;
179024afba76SLiad Kaufman 
1791cfbc6c4cSSara Sharon 		iwl_mvm_disable_txq(mvm, sta, mvm_sta->tid_data[i].txq_id, i,
1792cfbc6c4cSSara Sharon 				    0);
17936862fceeSSara Sharon 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
179424afba76SLiad Kaufman 	}
1795cfbc6c4cSSara Sharon 
1796cfbc6c4cSSara Sharon 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1797cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
1798cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1799cfbc6c4cSSara Sharon 
1800cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1801cfbc6c4cSSara Sharon 	}
180224afba76SLiad Kaufman }
180324afba76SLiad Kaufman 
1804d6d517b7SSara Sharon int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1805d6d517b7SSara Sharon 				  struct iwl_mvm_sta *mvm_sta)
1806d6d517b7SSara Sharon {
1807bec9522aSSharon Dvir 	int i;
1808d6d517b7SSara Sharon 
1809d6d517b7SSara Sharon 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1810d6d517b7SSara Sharon 		u16 txq_id;
1811bec9522aSSharon Dvir 		int ret;
1812d6d517b7SSara Sharon 
1813d6d517b7SSara Sharon 		spin_lock_bh(&mvm_sta->lock);
1814d6d517b7SSara Sharon 		txq_id = mvm_sta->tid_data[i].txq_id;
1815d6d517b7SSara Sharon 		spin_unlock_bh(&mvm_sta->lock);
1816d6d517b7SSara Sharon 
1817d6d517b7SSara Sharon 		if (txq_id == IWL_MVM_INVALID_QUEUE)
1818d6d517b7SSara Sharon 			continue;
1819d6d517b7SSara Sharon 
1820d6d517b7SSara Sharon 		ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1821d6d517b7SSara Sharon 		if (ret)
1822bec9522aSSharon Dvir 			return ret;
1823d6d517b7SSara Sharon 	}
1824d6d517b7SSara Sharon 
1825bec9522aSSharon Dvir 	return 0;
1826d6d517b7SSara Sharon }
1827d6d517b7SSara Sharon 
1828e705c121SKalle Valo int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1829e705c121SKalle Valo 		   struct ieee80211_vif *vif,
1830e705c121SKalle Valo 		   struct ieee80211_sta *sta)
1831e705c121SKalle Valo {
1832e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1833e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
183494c3e614SSara Sharon 	u8 sta_id = mvm_sta->sta_id;
1835e705c121SKalle Valo 	int ret;
1836e705c121SKalle Valo 
1837e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1838e705c121SKalle Valo 
1839a571f5f6SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
1840a571f5f6SSara Sharon 		kfree(mvm_sta->dup_data);
1841a571f5f6SSara Sharon 
1842e705c121SKalle Valo 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1843e705c121SKalle Valo 	if (ret)
1844e705c121SKalle Valo 		return ret;
1845c8f54701SJohannes Berg 
1846e705c121SKalle Valo 	/* flush its queues here since we are freeing mvm_sta */
1847d49394a1SSara Sharon 	ret = iwl_mvm_flush_sta(mvm, mvm_sta, false, 0);
1848e705c121SKalle Valo 	if (ret)
1849e705c121SKalle Valo 		return ret;
1850d6d517b7SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
1851d6d517b7SSara Sharon 		ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1852d6d517b7SSara Sharon 	} else {
1853d6d517b7SSara Sharon 		u32 q_mask = mvm_sta->tfd_queue_msk;
1854d6d517b7SSara Sharon 
1855a1a57877SSara Sharon 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1856d6d517b7SSara Sharon 						     q_mask);
1857d6d517b7SSara Sharon 	}
1858e705c121SKalle Valo 	if (ret)
1859e705c121SKalle Valo 		return ret;
1860c8f54701SJohannes Berg 
1861e705c121SKalle Valo 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1862e705c121SKalle Valo 
1863cfbc6c4cSSara Sharon 	iwl_mvm_disable_sta_queues(mvm, vif, sta);
186456214749SLiad Kaufman 
186556214749SLiad Kaufman 	/* If there is a TXQ still marked as reserved - free it */
1866c8f54701SJohannes Berg 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1867a0315deaSLiad Kaufman 		u8 reserved_txq = mvm_sta->reserved_queue;
1868a0315deaSLiad Kaufman 		enum iwl_mvm_queue_status *status;
1869a0315deaSLiad Kaufman 
1870a0315deaSLiad Kaufman 		/*
1871a0315deaSLiad Kaufman 		 * If no traffic has gone through the reserved TXQ - it
1872a0315deaSLiad Kaufman 		 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1873a0315deaSLiad Kaufman 		 * should be manually marked as free again
1874a0315deaSLiad Kaufman 		 */
1875a0315deaSLiad Kaufman 		status = &mvm->queue_info[reserved_txq].status;
1876a0315deaSLiad Kaufman 		if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1877a0315deaSLiad Kaufman 			 (*status != IWL_MVM_QUEUE_FREE),
1878a0315deaSLiad Kaufman 			 "sta_id %d reserved txq %d status %d",
1879f3f240f9SJohannes Berg 			 sta_id, reserved_txq, *status))
1880a0315deaSLiad Kaufman 			return -EINVAL;
1881a0315deaSLiad Kaufman 
1882a0315deaSLiad Kaufman 		*status = IWL_MVM_QUEUE_FREE;
1883a0315deaSLiad Kaufman 	}
1884a0315deaSLiad Kaufman 
1885e3118ad7SLiad Kaufman 	if (vif->type == NL80211_IFTYPE_STATION &&
188694c3e614SSara Sharon 	    mvmvif->ap_sta_id == sta_id) {
1887e3118ad7SLiad Kaufman 		/* if associated - we can't remove the AP STA now */
1888e705c121SKalle Valo 		if (vif->bss_conf.assoc)
1889e705c121SKalle Valo 			return ret;
1890e705c121SKalle Valo 
1891e705c121SKalle Valo 		/* unassoc - go ahead - remove the AP STA now */
18920ae98812SSara Sharon 		mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1893e705c121SKalle Valo 
1894e705c121SKalle Valo 		/* clear d0i3_ap_sta_id if no longer relevant */
189594c3e614SSara Sharon 		if (mvm->d0i3_ap_sta_id == sta_id)
18960ae98812SSara Sharon 			mvm->d0i3_ap_sta_id = IWL_MVM_INVALID_STA;
1897e705c121SKalle Valo 	}
1898e705c121SKalle Valo 
1899e705c121SKalle Valo 	/*
1900e705c121SKalle Valo 	 * This shouldn't happen - the TDLS channel switch should be canceled
1901e705c121SKalle Valo 	 * before the STA is removed.
1902e705c121SKalle Valo 	 */
190394c3e614SSara Sharon 	if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
19040ae98812SSara Sharon 		mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1905e705c121SKalle Valo 		cancel_delayed_work(&mvm->tdls_cs.dwork);
1906e705c121SKalle Valo 	}
1907e705c121SKalle Valo 
1908e705c121SKalle Valo 	/*
1909e705c121SKalle Valo 	 * Make sure that the tx response code sees the station as -EBUSY and
1910e705c121SKalle Valo 	 * calls the drain worker.
1911e705c121SKalle Valo 	 */
1912e705c121SKalle Valo 	spin_lock_bh(&mvm_sta->lock);
1913e705c121SKalle Valo 	spin_unlock_bh(&mvm_sta->lock);
1914e705c121SKalle Valo 
1915e705c121SKalle Valo 	ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1916e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1917e705c121SKalle Valo 
1918e705c121SKalle Valo 	return ret;
1919e705c121SKalle Valo }
1920e705c121SKalle Valo 
1921e705c121SKalle Valo int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1922e705c121SKalle Valo 		      struct ieee80211_vif *vif,
1923e705c121SKalle Valo 		      u8 sta_id)
1924e705c121SKalle Valo {
1925e705c121SKalle Valo 	int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1926e705c121SKalle Valo 
1927e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1928e705c121SKalle Valo 
1929e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1930e705c121SKalle Valo 	return ret;
1931e705c121SKalle Valo }
1932e705c121SKalle Valo 
19330e39eb03SChaya Rachel Ivgi int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1934e705c121SKalle Valo 			     struct iwl_mvm_int_sta *sta,
1935ced19f26SSara Sharon 			     u32 qmask, enum nl80211_iftype iftype,
1936ced19f26SSara Sharon 			     enum iwl_sta_type type)
1937e705c121SKalle Valo {
1938df65c8d1SAvraham Stern 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1939df65c8d1SAvraham Stern 	    sta->sta_id == IWL_MVM_INVALID_STA) {
1940e705c121SKalle Valo 		sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
19410ae98812SSara Sharon 		if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
1942e705c121SKalle Valo 			return -ENOSPC;
1943e705c121SKalle Valo 	}
1944e705c121SKalle Valo 
1945e705c121SKalle Valo 	sta->tfd_queue_msk = qmask;
1946ced19f26SSara Sharon 	sta->type = type;
1947e705c121SKalle Valo 
1948e705c121SKalle Valo 	/* put a non-NULL value so iterating over the stations won't stop */
1949e705c121SKalle Valo 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1950e705c121SKalle Valo 	return 0;
1951e705c121SKalle Valo }
1952e705c121SKalle Valo 
195326d6c16bSSara Sharon void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
1954e705c121SKalle Valo {
1955e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
1956e705c121SKalle Valo 	memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
19570ae98812SSara Sharon 	sta->sta_id = IWL_MVM_INVALID_STA;
1958e705c121SKalle Valo }
1959e705c121SKalle Valo 
1960b13f43a4SEmmanuel Grumbach static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 *queue,
1961b13f43a4SEmmanuel Grumbach 					  u8 sta_id, u8 fifo)
1962e705c121SKalle Valo {
1963e705c121SKalle Valo 	unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1964e705c121SKalle Valo 					mvm->cfg->base_params->wd_timeout :
1965e705c121SKalle Valo 					IWL_WATCHDOG_DISABLED;
1966e705c121SKalle Valo 
1967310181ecSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
1968b13f43a4SEmmanuel Grumbach 		int tvqm_queue =
1969cfbc6c4cSSara Sharon 			iwl_mvm_tvqm_enable_txq(mvm, sta_id,
1970310181ecSSara Sharon 						IWL_MAX_TID_COUNT,
1971310181ecSSara Sharon 						wdg_timeout);
1972b13f43a4SEmmanuel Grumbach 		*queue = tvqm_queue;
1973c8f54701SJohannes Berg 	} else {
197428d0793eSLiad Kaufman 		struct iwl_trans_txq_scd_cfg cfg = {
1975b13f43a4SEmmanuel Grumbach 			.fifo = fifo,
1976b13f43a4SEmmanuel Grumbach 			.sta_id = sta_id,
197728d0793eSLiad Kaufman 			.tid = IWL_MAX_TID_COUNT,
197828d0793eSLiad Kaufman 			.aggregate = false,
197928d0793eSLiad Kaufman 			.frame_limit = IWL_FRAME_LIMIT,
198028d0793eSLiad Kaufman 		};
198128d0793eSLiad Kaufman 
1982cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, *queue, 0, &cfg, wdg_timeout);
198328d0793eSLiad Kaufman 	}
1984c5a719eeSSara Sharon }
1985c5a719eeSSara Sharon 
1986c5a719eeSSara Sharon int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1987c5a719eeSSara Sharon {
1988c5a719eeSSara Sharon 	int ret;
1989c5a719eeSSara Sharon 
1990c5a719eeSSara Sharon 	lockdep_assert_held(&mvm->mutex);
1991c5a719eeSSara Sharon 
1992c5a719eeSSara Sharon 	/* Allocate aux station and assign to it the aux queue */
1993c5a719eeSSara Sharon 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
1994ced19f26SSara Sharon 				       NL80211_IFTYPE_UNSPECIFIED,
1995ced19f26SSara Sharon 				       IWL_STA_AUX_ACTIVITY);
1996c5a719eeSSara Sharon 	if (ret)
1997c5a719eeSSara Sharon 		return ret;
1998c5a719eeSSara Sharon 
1999c5a719eeSSara Sharon 	/* Map Aux queue to fifo - needs to happen before adding Aux station */
2000c5a719eeSSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm))
2001b13f43a4SEmmanuel Grumbach 		iwl_mvm_enable_aux_snif_queue(mvm, &mvm->aux_queue,
2002b13f43a4SEmmanuel Grumbach 					      mvm->aux_sta.sta_id,
2003b13f43a4SEmmanuel Grumbach 					      IWL_MVM_TX_FIFO_MCAST);
200428d0793eSLiad Kaufman 
2005e705c121SKalle Valo 	ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
2006e705c121SKalle Valo 					 MAC_INDEX_AUX, 0);
2007c5a719eeSSara Sharon 	if (ret) {
2008e705c121SKalle Valo 		iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2009e705c121SKalle Valo 		return ret;
2010e705c121SKalle Valo 	}
2011e705c121SKalle Valo 
2012c5a719eeSSara Sharon 	/*
20132f7a3863SLuca Coelho 	 * For 22000 firmware and on we cannot add queue to a station unknown
2014c5a719eeSSara Sharon 	 * to firmware so enable queue here - after the station was added
2015c5a719eeSSara Sharon 	 */
2016c5a719eeSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm))
2017b13f43a4SEmmanuel Grumbach 		iwl_mvm_enable_aux_snif_queue(mvm, &mvm->aux_queue,
2018b13f43a4SEmmanuel Grumbach 					      mvm->aux_sta.sta_id,
2019b13f43a4SEmmanuel Grumbach 					      IWL_MVM_TX_FIFO_MCAST);
2020c5a719eeSSara Sharon 
2021c5a719eeSSara Sharon 	return 0;
2022c5a719eeSSara Sharon }
2023c5a719eeSSara Sharon 
20240e39eb03SChaya Rachel Ivgi int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
20250e39eb03SChaya Rachel Ivgi {
20260e39eb03SChaya Rachel Ivgi 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2027b13f43a4SEmmanuel Grumbach 	int ret;
20280e39eb03SChaya Rachel Ivgi 
20290e39eb03SChaya Rachel Ivgi 	lockdep_assert_held(&mvm->mutex);
2030b13f43a4SEmmanuel Grumbach 
2031b13f43a4SEmmanuel Grumbach 	/* Map snif queue to fifo - must happen before adding snif station */
2032b13f43a4SEmmanuel Grumbach 	if (!iwl_mvm_has_new_tx_api(mvm))
2033b13f43a4SEmmanuel Grumbach 		iwl_mvm_enable_aux_snif_queue(mvm, &mvm->snif_queue,
2034b13f43a4SEmmanuel Grumbach 					      mvm->snif_sta.sta_id,
2035b13f43a4SEmmanuel Grumbach 					      IWL_MVM_TX_FIFO_BE);
2036b13f43a4SEmmanuel Grumbach 
2037b13f43a4SEmmanuel Grumbach 	ret = iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
20380e39eb03SChaya Rachel Ivgi 					 mvmvif->id, 0);
2039b13f43a4SEmmanuel Grumbach 	if (ret)
2040b13f43a4SEmmanuel Grumbach 		return ret;
2041b13f43a4SEmmanuel Grumbach 
2042b13f43a4SEmmanuel Grumbach 	/*
2043b13f43a4SEmmanuel Grumbach 	 * For 22000 firmware and on we cannot add queue to a station unknown
2044b13f43a4SEmmanuel Grumbach 	 * to firmware so enable queue here - after the station was added
2045b13f43a4SEmmanuel Grumbach 	 */
2046b13f43a4SEmmanuel Grumbach 	if (iwl_mvm_has_new_tx_api(mvm))
2047b13f43a4SEmmanuel Grumbach 		iwl_mvm_enable_aux_snif_queue(mvm, &mvm->snif_queue,
2048b13f43a4SEmmanuel Grumbach 					      mvm->snif_sta.sta_id,
2049b13f43a4SEmmanuel Grumbach 					      IWL_MVM_TX_FIFO_BE);
2050b13f43a4SEmmanuel Grumbach 
2051b13f43a4SEmmanuel Grumbach 	return 0;
20520e39eb03SChaya Rachel Ivgi }
20530e39eb03SChaya Rachel Ivgi 
20540e39eb03SChaya Rachel Ivgi int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
20550e39eb03SChaya Rachel Ivgi {
20560e39eb03SChaya Rachel Ivgi 	int ret;
20570e39eb03SChaya Rachel Ivgi 
20580e39eb03SChaya Rachel Ivgi 	lockdep_assert_held(&mvm->mutex);
20590e39eb03SChaya Rachel Ivgi 
2060cfbc6c4cSSara Sharon 	iwl_mvm_disable_txq(mvm, NULL, mvm->snif_queue, IWL_MAX_TID_COUNT, 0);
20610e39eb03SChaya Rachel Ivgi 	ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
20620e39eb03SChaya Rachel Ivgi 	if (ret)
20630e39eb03SChaya Rachel Ivgi 		IWL_WARN(mvm, "Failed sending remove station\n");
20640e39eb03SChaya Rachel Ivgi 
20650e39eb03SChaya Rachel Ivgi 	return ret;
20660e39eb03SChaya Rachel Ivgi }
20670e39eb03SChaya Rachel Ivgi 
20680e39eb03SChaya Rachel Ivgi void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
20690e39eb03SChaya Rachel Ivgi {
20700e39eb03SChaya Rachel Ivgi 	iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
20710e39eb03SChaya Rachel Ivgi }
20720e39eb03SChaya Rachel Ivgi 
2073e705c121SKalle Valo void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
2074e705c121SKalle Valo {
2075e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2076e705c121SKalle Valo 
2077e705c121SKalle Valo 	iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2078e705c121SKalle Valo }
2079e705c121SKalle Valo 
2080e705c121SKalle Valo /*
2081e705c121SKalle Valo  * Send the add station command for the vif's broadcast station.
2082e705c121SKalle Valo  * Assumes that the station was already allocated.
2083e705c121SKalle Valo  *
2084e705c121SKalle Valo  * @mvm: the mvm component
2085e705c121SKalle Valo  * @vif: the interface to which the broadcast station is added
2086e705c121SKalle Valo  * @bsta: the broadcast station to add.
2087e705c121SKalle Valo  */
2088e705c121SKalle Valo int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2089e705c121SKalle Valo {
2090e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2091e705c121SKalle Valo 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2092e705c121SKalle Valo 	static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2093e705c121SKalle Valo 	const u8 *baddr = _baddr;
20947daa7624SJohannes Berg 	int queue;
2095df88c08dSLiad Kaufman 	int ret;
2096c5a719eeSSara Sharon 	unsigned int wdg_timeout =
2097c5a719eeSSara Sharon 		iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2098de24f638SLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
2099de24f638SLiad Kaufman 		.fifo = IWL_MVM_TX_FIFO_VO,
2100de24f638SLiad Kaufman 		.sta_id = mvmvif->bcast_sta.sta_id,
2101de24f638SLiad Kaufman 		.tid = IWL_MAX_TID_COUNT,
2102de24f638SLiad Kaufman 		.aggregate = false,
2103de24f638SLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
2104de24f638SLiad Kaufman 	};
2105de24f638SLiad Kaufman 
2106c5a719eeSSara Sharon 	lockdep_assert_held(&mvm->mutex);
2107c5a719eeSSara Sharon 
2108c8f54701SJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm)) {
21094d339989SLiad Kaufman 		if (vif->type == NL80211_IFTYPE_AP ||
21104d339989SLiad Kaufman 		    vif->type == NL80211_IFTYPE_ADHOC)
211149f71713SSara Sharon 			queue = mvm->probe_queue;
2112df88c08dSLiad Kaufman 		else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
211349f71713SSara Sharon 			queue = mvm->p2p_dev_queue;
2114df88c08dSLiad Kaufman 		else if (WARN(1, "Missing required TXQ for adding bcast STA\n"))
2115de24f638SLiad Kaufman 			return -EINVAL;
2116de24f638SLiad Kaufman 
2117df88c08dSLiad Kaufman 		bsta->tfd_queue_msk |= BIT(queue);
2118c5a719eeSSara Sharon 
2119cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2120de24f638SLiad Kaufman 	}
2121de24f638SLiad Kaufman 
2122e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_ADHOC)
2123e705c121SKalle Valo 		baddr = vif->bss_conf.bssid;
2124e705c121SKalle Valo 
21250ae98812SSara Sharon 	if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
2126e705c121SKalle Valo 		return -ENOSPC;
2127e705c121SKalle Valo 
2128df88c08dSLiad Kaufman 	ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2129e705c121SKalle Valo 					 mvmvif->id, mvmvif->color);
2130df88c08dSLiad Kaufman 	if (ret)
2131df88c08dSLiad Kaufman 		return ret;
2132df88c08dSLiad Kaufman 
2133df88c08dSLiad Kaufman 	/*
21342f7a3863SLuca Coelho 	 * For 22000 firmware and on we cannot add queue to a station unknown
2135c5a719eeSSara Sharon 	 * to firmware so enable queue here - after the station was added
2136df88c08dSLiad Kaufman 	 */
2137310181ecSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
2138cfbc6c4cSSara Sharon 		queue = iwl_mvm_tvqm_enable_txq(mvm, bsta->sta_id,
2139310181ecSSara Sharon 						IWL_MAX_TID_COUNT,
2140c5a719eeSSara Sharon 						wdg_timeout);
21417daa7624SJohannes Berg 
21427b758a11SLuca Coelho 		if (vif->type == NL80211_IFTYPE_AP ||
21437b758a11SLuca Coelho 		    vif->type == NL80211_IFTYPE_ADHOC)
2144310181ecSSara Sharon 			mvm->probe_queue = queue;
2145310181ecSSara Sharon 		else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
2146310181ecSSara Sharon 			mvm->p2p_dev_queue = queue;
2147310181ecSSara Sharon 	}
2148df88c08dSLiad Kaufman 
2149df88c08dSLiad Kaufman 	return 0;
2150df88c08dSLiad Kaufman }
2151df88c08dSLiad Kaufman 
2152df88c08dSLiad Kaufman static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2153df88c08dSLiad Kaufman 					  struct ieee80211_vif *vif)
2154df88c08dSLiad Kaufman {
2155df88c08dSLiad Kaufman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2156d167e81aSMordechai Goodstein 	int queue;
2157df88c08dSLiad Kaufman 
2158df88c08dSLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
2159df88c08dSLiad Kaufman 
2160d49394a1SSara Sharon 	iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true, 0);
2161d49394a1SSara Sharon 
2162d167e81aSMordechai Goodstein 	switch (vif->type) {
2163d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_AP:
2164d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_ADHOC:
2165d167e81aSMordechai Goodstein 		queue = mvm->probe_queue;
2166d167e81aSMordechai Goodstein 		break;
2167d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_P2P_DEVICE:
2168d167e81aSMordechai Goodstein 		queue = mvm->p2p_dev_queue;
2169d167e81aSMordechai Goodstein 		break;
2170d167e81aSMordechai Goodstein 	default:
2171d167e81aSMordechai Goodstein 		WARN(1, "Can't free bcast queue on vif type %d\n",
2172d167e81aSMordechai Goodstein 		     vif->type);
2173d167e81aSMordechai Goodstein 		return;
2174df88c08dSLiad Kaufman 	}
2175df88c08dSLiad Kaufman 
2176cfbc6c4cSSara Sharon 	iwl_mvm_disable_txq(mvm, NULL, queue, IWL_MAX_TID_COUNT, 0);
2177d167e81aSMordechai Goodstein 	if (iwl_mvm_has_new_tx_api(mvm))
2178d167e81aSMordechai Goodstein 		return;
2179d167e81aSMordechai Goodstein 
2180d167e81aSMordechai Goodstein 	WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
2181d167e81aSMordechai Goodstein 	mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
2182e705c121SKalle Valo }
2183e705c121SKalle Valo 
2184e705c121SKalle Valo /* Send the FW a request to remove the station from it's internal data
2185e705c121SKalle Valo  * structures, but DO NOT remove the entry from the local data structures. */
2186e705c121SKalle Valo int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2187e705c121SKalle Valo {
2188e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2189e705c121SKalle Valo 	int ret;
2190e705c121SKalle Valo 
2191e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2192e705c121SKalle Valo 
2193df88c08dSLiad Kaufman 	iwl_mvm_free_bcast_sta_queues(mvm, vif);
2194df88c08dSLiad Kaufman 
2195e705c121SKalle Valo 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
2196e705c121SKalle Valo 	if (ret)
2197e705c121SKalle Valo 		IWL_WARN(mvm, "Failed sending remove station\n");
2198e705c121SKalle Valo 	return ret;
2199e705c121SKalle Valo }
2200e705c121SKalle Valo 
2201e705c121SKalle Valo int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2202e705c121SKalle Valo {
2203e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2204e705c121SKalle Valo 
2205e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2206e705c121SKalle Valo 
2207c8f54701SJohannes Berg 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, 0,
2208ced19f26SSara Sharon 					ieee80211_vif_type_p2p(vif),
2209ced19f26SSara Sharon 					IWL_STA_GENERAL_PURPOSE);
2210e705c121SKalle Valo }
2211e705c121SKalle Valo 
2212e705c121SKalle Valo /* Allocate a new station entry for the broadcast station to the given vif,
2213e705c121SKalle Valo  * and send it to the FW.
2214e705c121SKalle Valo  * Note that each P2P mac should have its own broadcast station.
2215e705c121SKalle Valo  *
2216e705c121SKalle Valo  * @mvm: the mvm component
2217e705c121SKalle Valo  * @vif: the interface to which the broadcast station is added
2218e705c121SKalle Valo  * @bsta: the broadcast station to add. */
2219d197358bSLuca Coelho int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2220e705c121SKalle Valo {
2221e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2222e705c121SKalle Valo 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2223e705c121SKalle Valo 	int ret;
2224e705c121SKalle Valo 
2225e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2226e705c121SKalle Valo 
2227e705c121SKalle Valo 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2228e705c121SKalle Valo 	if (ret)
2229e705c121SKalle Valo 		return ret;
2230e705c121SKalle Valo 
2231e705c121SKalle Valo 	ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2232e705c121SKalle Valo 
2233e705c121SKalle Valo 	if (ret)
2234e705c121SKalle Valo 		iwl_mvm_dealloc_int_sta(mvm, bsta);
2235e705c121SKalle Valo 
2236e705c121SKalle Valo 	return ret;
2237e705c121SKalle Valo }
2238e705c121SKalle Valo 
2239e705c121SKalle Valo void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2240e705c121SKalle Valo {
2241e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2242e705c121SKalle Valo 
2243e705c121SKalle Valo 	iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2244e705c121SKalle Valo }
2245e705c121SKalle Valo 
2246e705c121SKalle Valo /*
2247e705c121SKalle Valo  * Send the FW a request to remove the station from it's internal data
2248e705c121SKalle Valo  * structures, and in addition remove it from the local data structure.
2249e705c121SKalle Valo  */
2250d197358bSLuca Coelho int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2251e705c121SKalle Valo {
2252e705c121SKalle Valo 	int ret;
2253e705c121SKalle Valo 
2254e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2255e705c121SKalle Valo 
2256e705c121SKalle Valo 	ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2257e705c121SKalle Valo 
2258e705c121SKalle Valo 	iwl_mvm_dealloc_bcast_sta(mvm, vif);
2259e705c121SKalle Valo 
2260e705c121SKalle Valo 	return ret;
2261e705c121SKalle Valo }
2262e705c121SKalle Valo 
226326d6c16bSSara Sharon /*
226426d6c16bSSara Sharon  * Allocate a new station entry for the multicast station to the given vif,
226526d6c16bSSara Sharon  * and send it to the FW.
226626d6c16bSSara Sharon  * Note that each AP/GO mac should have its own multicast station.
226726d6c16bSSara Sharon  *
226826d6c16bSSara Sharon  * @mvm: the mvm component
226926d6c16bSSara Sharon  * @vif: the interface to which the multicast station is added
227026d6c16bSSara Sharon  */
227126d6c16bSSara Sharon int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
227226d6c16bSSara Sharon {
227326d6c16bSSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
227426d6c16bSSara Sharon 	struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
227526d6c16bSSara Sharon 	static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
227626d6c16bSSara Sharon 	const u8 *maddr = _maddr;
227726d6c16bSSara Sharon 	struct iwl_trans_txq_scd_cfg cfg = {
227826d6c16bSSara Sharon 		.fifo = IWL_MVM_TX_FIFO_MCAST,
227926d6c16bSSara Sharon 		.sta_id = msta->sta_id,
22806508de03SIlan Peer 		.tid = 0,
228126d6c16bSSara Sharon 		.aggregate = false,
228226d6c16bSSara Sharon 		.frame_limit = IWL_FRAME_LIMIT,
228326d6c16bSSara Sharon 	};
228426d6c16bSSara Sharon 	unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
228526d6c16bSSara Sharon 	int ret;
228626d6c16bSSara Sharon 
228726d6c16bSSara Sharon 	lockdep_assert_held(&mvm->mutex);
228826d6c16bSSara Sharon 
2289ee48b722SLiad Kaufman 	if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2290ee48b722SLiad Kaufman 		    vif->type != NL80211_IFTYPE_ADHOC))
229126d6c16bSSara Sharon 		return -ENOTSUPP;
229226d6c16bSSara Sharon 
2293ced19f26SSara Sharon 	/*
2294fc07bd8cSSara Sharon 	 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2295fc07bd8cSSara Sharon 	 * invalid, so make sure we use the queue we want.
2296fc07bd8cSSara Sharon 	 * Note that this is done here as we want to avoid making DQA
2297fc07bd8cSSara Sharon 	 * changes in mac80211 layer.
2298fc07bd8cSSara Sharon 	 */
2299cfbc6c4cSSara Sharon 	if (vif->type == NL80211_IFTYPE_ADHOC)
2300cfbc6c4cSSara Sharon 		mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2301fc07bd8cSSara Sharon 
2302fc07bd8cSSara Sharon 	/*
2303ced19f26SSara Sharon 	 * While in previous FWs we had to exclude cab queue from TFD queue
2304ced19f26SSara Sharon 	 * mask, now it is needed as any other queue.
2305ced19f26SSara Sharon 	 */
2306ced19f26SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm) &&
2307ced19f26SSara Sharon 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2308cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2309cfbc6c4cSSara Sharon 				   timeout);
2310cfbc6c4cSSara Sharon 		msta->tfd_queue_msk |= BIT(mvmvif->cab_queue);
2311ced19f26SSara Sharon 	}
231226d6c16bSSara Sharon 	ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
231326d6c16bSSara Sharon 					 mvmvif->id, mvmvif->color);
231426d6c16bSSara Sharon 	if (ret) {
231526d6c16bSSara Sharon 		iwl_mvm_dealloc_int_sta(mvm, msta);
231626d6c16bSSara Sharon 		return ret;
231726d6c16bSSara Sharon 	}
231826d6c16bSSara Sharon 
231926d6c16bSSara Sharon 	/*
232026d6c16bSSara Sharon 	 * Enable cab queue after the ADD_STA command is sent.
23212f7a3863SLuca Coelho 	 * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
2322ced19f26SSara Sharon 	 * command with unknown station id, and for FW that doesn't support
2323ced19f26SSara Sharon 	 * station API since the cab queue is not included in the
2324ced19f26SSara Sharon 	 * tfd_queue_mask.
232526d6c16bSSara Sharon 	 */
2326310181ecSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
2327cfbc6c4cSSara Sharon 		int queue = iwl_mvm_tvqm_enable_txq(mvm, msta->sta_id,
23286508de03SIlan Peer 						    0,
232926d6c16bSSara Sharon 						    timeout);
2330e2af3fabSSara Sharon 		mvmvif->cab_queue = queue;
2331ced19f26SSara Sharon 	} else if (!fw_has_api(&mvm->fw->ucode_capa,
2332fc07bd8cSSara Sharon 			       IWL_UCODE_TLV_API_STA_TYPE))
2333cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2334cfbc6c4cSSara Sharon 				   timeout);
233526d6c16bSSara Sharon 
233626d6c16bSSara Sharon 	return 0;
233726d6c16bSSara Sharon }
233826d6c16bSSara Sharon 
233928916a16SEmmanuel Grumbach static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
234028916a16SEmmanuel Grumbach 				    struct ieee80211_key_conf *keyconf,
234128916a16SEmmanuel Grumbach 				    bool mcast)
234228916a16SEmmanuel Grumbach {
234328916a16SEmmanuel Grumbach 	union {
234428916a16SEmmanuel Grumbach 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
234528916a16SEmmanuel Grumbach 		struct iwl_mvm_add_sta_key_cmd cmd;
234628916a16SEmmanuel Grumbach 	} u = {};
234728916a16SEmmanuel Grumbach 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
234828916a16SEmmanuel Grumbach 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
234928916a16SEmmanuel Grumbach 	__le16 key_flags;
235028916a16SEmmanuel Grumbach 	int ret, size;
235128916a16SEmmanuel Grumbach 	u32 status;
235228916a16SEmmanuel Grumbach 
235328916a16SEmmanuel Grumbach 	/* This is a valid situation for GTK removal */
235428916a16SEmmanuel Grumbach 	if (sta_id == IWL_MVM_INVALID_STA)
235528916a16SEmmanuel Grumbach 		return 0;
235628916a16SEmmanuel Grumbach 
235728916a16SEmmanuel Grumbach 	key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
235828916a16SEmmanuel Grumbach 				 STA_KEY_FLG_KEYID_MSK);
235928916a16SEmmanuel Grumbach 	key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
236028916a16SEmmanuel Grumbach 	key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
236128916a16SEmmanuel Grumbach 
236228916a16SEmmanuel Grumbach 	if (mcast)
236328916a16SEmmanuel Grumbach 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
236428916a16SEmmanuel Grumbach 
236528916a16SEmmanuel Grumbach 	/*
236628916a16SEmmanuel Grumbach 	 * The fields assigned here are in the same location at the start
236728916a16SEmmanuel Grumbach 	 * of the command, so we can do this union trick.
236828916a16SEmmanuel Grumbach 	 */
236928916a16SEmmanuel Grumbach 	u.cmd.common.key_flags = key_flags;
237028916a16SEmmanuel Grumbach 	u.cmd.common.key_offset = keyconf->hw_key_idx;
237128916a16SEmmanuel Grumbach 	u.cmd.common.sta_id = sta_id;
237228916a16SEmmanuel Grumbach 
237328916a16SEmmanuel Grumbach 	size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
237428916a16SEmmanuel Grumbach 
237528916a16SEmmanuel Grumbach 	status = ADD_STA_SUCCESS;
237628916a16SEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
237728916a16SEmmanuel Grumbach 					  &status);
237828916a16SEmmanuel Grumbach 
237928916a16SEmmanuel Grumbach 	switch (status) {
238028916a16SEmmanuel Grumbach 	case ADD_STA_SUCCESS:
238128916a16SEmmanuel Grumbach 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
238228916a16SEmmanuel Grumbach 		break;
238328916a16SEmmanuel Grumbach 	default:
238428916a16SEmmanuel Grumbach 		ret = -EIO;
238528916a16SEmmanuel Grumbach 		IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
238628916a16SEmmanuel Grumbach 		break;
238728916a16SEmmanuel Grumbach 	}
238828916a16SEmmanuel Grumbach 
238928916a16SEmmanuel Grumbach 	return ret;
239028916a16SEmmanuel Grumbach }
239128916a16SEmmanuel Grumbach 
239226d6c16bSSara Sharon /*
239326d6c16bSSara Sharon  * Send the FW a request to remove the station from it's internal data
239426d6c16bSSara Sharon  * structures, and in addition remove it from the local data structure.
239526d6c16bSSara Sharon  */
239626d6c16bSSara Sharon int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
239726d6c16bSSara Sharon {
239826d6c16bSSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
239926d6c16bSSara Sharon 	int ret;
240026d6c16bSSara Sharon 
240126d6c16bSSara Sharon 	lockdep_assert_held(&mvm->mutex);
240226d6c16bSSara Sharon 
2403d49394a1SSara Sharon 	iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true, 0);
2404d49394a1SSara Sharon 
2405cfbc6c4cSSara Sharon 	iwl_mvm_disable_txq(mvm, NULL, mvmvif->cab_queue, 0, 0);
240626d6c16bSSara Sharon 
240726d6c16bSSara Sharon 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
240826d6c16bSSara Sharon 	if (ret)
240926d6c16bSSara Sharon 		IWL_WARN(mvm, "Failed sending remove station\n");
241026d6c16bSSara Sharon 
241126d6c16bSSara Sharon 	return ret;
241226d6c16bSSara Sharon }
241326d6c16bSSara Sharon 
2414e705c121SKalle Valo #define IWL_MAX_RX_BA_SESSIONS 16
2415e705c121SKalle Valo 
2416b915c101SSara Sharon static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
241710b2b201SSara Sharon {
2418b915c101SSara Sharon 	struct iwl_mvm_delba_notif notif = {
2419b915c101SSara Sharon 		.metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
2420b915c101SSara Sharon 		.metadata.sync = 1,
2421b915c101SSara Sharon 		.delba.baid = baid,
2422b915c101SSara Sharon 	};
2423b915c101SSara Sharon 	iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
242410b2b201SSara Sharon };
242510b2b201SSara Sharon 
2426b915c101SSara Sharon static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2427b915c101SSara Sharon 				 struct iwl_mvm_baid_data *data)
2428b915c101SSara Sharon {
2429b915c101SSara Sharon 	int i;
2430b915c101SSara Sharon 
2431b915c101SSara Sharon 	iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2432b915c101SSara Sharon 
2433b915c101SSara Sharon 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2434b915c101SSara Sharon 		int j;
2435b915c101SSara Sharon 		struct iwl_mvm_reorder_buffer *reorder_buf =
2436b915c101SSara Sharon 			&data->reorder_buf[i];
2437dfdddd92SJohannes Berg 		struct iwl_mvm_reorder_buf_entry *entries =
2438dfdddd92SJohannes Berg 			&data->entries[i * data->entries_per_queue];
2439b915c101SSara Sharon 
24400690405fSSara Sharon 		spin_lock_bh(&reorder_buf->lock);
24410690405fSSara Sharon 		if (likely(!reorder_buf->num_stored)) {
24420690405fSSara Sharon 			spin_unlock_bh(&reorder_buf->lock);
2443b915c101SSara Sharon 			continue;
24440690405fSSara Sharon 		}
2445b915c101SSara Sharon 
2446b915c101SSara Sharon 		/*
2447b915c101SSara Sharon 		 * This shouldn't happen in regular DELBA since the internal
2448b915c101SSara Sharon 		 * delBA notification should trigger a release of all frames in
2449b915c101SSara Sharon 		 * the reorder buffer.
2450b915c101SSara Sharon 		 */
2451b915c101SSara Sharon 		WARN_ON(1);
2452b915c101SSara Sharon 
2453b915c101SSara Sharon 		for (j = 0; j < reorder_buf->buf_size; j++)
2454dfdddd92SJohannes Berg 			__skb_queue_purge(&entries[j].e.frames);
24550690405fSSara Sharon 		/*
24560690405fSSara Sharon 		 * Prevent timer re-arm. This prevents a very far fetched case
24570690405fSSara Sharon 		 * where we timed out on the notification. There may be prior
24580690405fSSara Sharon 		 * RX frames pending in the RX queue before the notification
24590690405fSSara Sharon 		 * that might get processed between now and the actual deletion
24600690405fSSara Sharon 		 * and we would re-arm the timer although we are deleting the
24610690405fSSara Sharon 		 * reorder buffer.
24620690405fSSara Sharon 		 */
24630690405fSSara Sharon 		reorder_buf->removed = true;
24640690405fSSara Sharon 		spin_unlock_bh(&reorder_buf->lock);
24650690405fSSara Sharon 		del_timer_sync(&reorder_buf->reorder_timer);
2466b915c101SSara Sharon 	}
2467b915c101SSara Sharon }
2468b915c101SSara Sharon 
2469b915c101SSara Sharon static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2470b915c101SSara Sharon 					struct iwl_mvm_baid_data *data,
2471514c3069SLuca Coelho 					u16 ssn, u16 buf_size)
2472b915c101SSara Sharon {
2473b915c101SSara Sharon 	int i;
2474b915c101SSara Sharon 
2475b915c101SSara Sharon 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2476b915c101SSara Sharon 		struct iwl_mvm_reorder_buffer *reorder_buf =
2477b915c101SSara Sharon 			&data->reorder_buf[i];
2478dfdddd92SJohannes Berg 		struct iwl_mvm_reorder_buf_entry *entries =
2479dfdddd92SJohannes Berg 			&data->entries[i * data->entries_per_queue];
2480b915c101SSara Sharon 		int j;
2481b915c101SSara Sharon 
2482b915c101SSara Sharon 		reorder_buf->num_stored = 0;
2483b915c101SSara Sharon 		reorder_buf->head_sn = ssn;
2484b915c101SSara Sharon 		reorder_buf->buf_size = buf_size;
24850690405fSSara Sharon 		/* rx reorder timer */
24868cef5344SKees Cook 		timer_setup(&reorder_buf->reorder_timer,
24878cef5344SKees Cook 			    iwl_mvm_reorder_timer_expired, 0);
24880690405fSSara Sharon 		spin_lock_init(&reorder_buf->lock);
24890690405fSSara Sharon 		reorder_buf->mvm = mvm;
2490b915c101SSara Sharon 		reorder_buf->queue = i;
24915d43eab6SSara Sharon 		reorder_buf->valid = false;
2492b915c101SSara Sharon 		for (j = 0; j < reorder_buf->buf_size; j++)
2493dfdddd92SJohannes Berg 			__skb_queue_head_init(&entries[j].e.frames);
2494b915c101SSara Sharon 	}
249510b2b201SSara Sharon }
249610b2b201SSara Sharon 
2497e705c121SKalle Valo int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2498514c3069SLuca Coelho 		       int tid, u16 ssn, bool start, u16 buf_size, u16 timeout)
2499e705c121SKalle Valo {
2500e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2501e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
250210b2b201SSara Sharon 	struct iwl_mvm_baid_data *baid_data = NULL;
2503e705c121SKalle Valo 	int ret;
2504e705c121SKalle Valo 	u32 status;
2505e705c121SKalle Valo 
2506e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2507e705c121SKalle Valo 
2508e705c121SKalle Valo 	if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
2509e705c121SKalle Valo 		IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2510e705c121SKalle Valo 		return -ENOSPC;
2511e705c121SKalle Valo 	}
2512e705c121SKalle Valo 
251310b2b201SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) && start) {
2514dfdddd92SJohannes Berg 		u16 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2515dfdddd92SJohannes Berg 
2516dfdddd92SJohannes Berg 		/* sparse doesn't like the __align() so don't check */
2517dfdddd92SJohannes Berg #ifndef __CHECKER__
2518dfdddd92SJohannes Berg 		/*
2519dfdddd92SJohannes Berg 		 * The division below will be OK if either the cache line size
2520dfdddd92SJohannes Berg 		 * can be divided by the entry size (ALIGN will round up) or if
2521dfdddd92SJohannes Berg 		 * if the entry size can be divided by the cache line size, in
2522dfdddd92SJohannes Berg 		 * which case the ALIGN() will do nothing.
2523dfdddd92SJohannes Berg 		 */
2524dfdddd92SJohannes Berg 		BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2525dfdddd92SJohannes Berg 			     sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2526dfdddd92SJohannes Berg #endif
2527dfdddd92SJohannes Berg 
2528dfdddd92SJohannes Berg 		/*
2529dfdddd92SJohannes Berg 		 * Upward align the reorder buffer size to fill an entire cache
2530dfdddd92SJohannes Berg 		 * line for each queue, to avoid sharing cache lines between
2531dfdddd92SJohannes Berg 		 * different queues.
2532dfdddd92SJohannes Berg 		 */
2533dfdddd92SJohannes Berg 		reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2534dfdddd92SJohannes Berg 
253510b2b201SSara Sharon 		/*
253610b2b201SSara Sharon 		 * Allocate here so if allocation fails we can bail out early
253710b2b201SSara Sharon 		 * before starting the BA session in the firmware
253810b2b201SSara Sharon 		 */
2539b915c101SSara Sharon 		baid_data = kzalloc(sizeof(*baid_data) +
2540b915c101SSara Sharon 				    mvm->trans->num_rx_queues *
2541dfdddd92SJohannes Berg 				    reorder_buf_size,
2542b915c101SSara Sharon 				    GFP_KERNEL);
254310b2b201SSara Sharon 		if (!baid_data)
254410b2b201SSara Sharon 			return -ENOMEM;
2545dfdddd92SJohannes Berg 
2546dfdddd92SJohannes Berg 		/*
2547dfdddd92SJohannes Berg 		 * This division is why we need the above BUILD_BUG_ON(),
2548dfdddd92SJohannes Berg 		 * if that doesn't hold then this will not be right.
2549dfdddd92SJohannes Berg 		 */
2550dfdddd92SJohannes Berg 		baid_data->entries_per_queue =
2551dfdddd92SJohannes Berg 			reorder_buf_size / sizeof(baid_data->entries[0]);
255210b2b201SSara Sharon 	}
255310b2b201SSara Sharon 
2554e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2555e705c121SKalle Valo 	cmd.sta_id = mvm_sta->sta_id;
2556e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
2557e705c121SKalle Valo 	if (start) {
2558e705c121SKalle Valo 		cmd.add_immediate_ba_tid = (u8) tid;
2559e705c121SKalle Valo 		cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
2560514c3069SLuca Coelho 		cmd.rx_ba_window = cpu_to_le16(buf_size);
2561e705c121SKalle Valo 	} else {
2562e705c121SKalle Valo 		cmd.remove_immediate_ba_tid = (u8) tid;
2563e705c121SKalle Valo 	}
2564e705c121SKalle Valo 	cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2565e705c121SKalle Valo 				  STA_MODIFY_REMOVE_BA_TID;
2566e705c121SKalle Valo 
2567e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
2568854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2569854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
2570e705c121SKalle Valo 					  &cmd, &status);
2571e705c121SKalle Valo 	if (ret)
257210b2b201SSara Sharon 		goto out_free;
2573e705c121SKalle Valo 
2574837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2575e705c121SKalle Valo 	case ADD_STA_SUCCESS:
257635263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2577e705c121SKalle Valo 			     start ? "start" : "stopp");
2578e705c121SKalle Valo 		break;
2579e705c121SKalle Valo 	case ADD_STA_IMMEDIATE_BA_FAILURE:
2580e705c121SKalle Valo 		IWL_WARN(mvm, "RX BA Session refused by fw\n");
2581e705c121SKalle Valo 		ret = -ENOSPC;
2582e705c121SKalle Valo 		break;
2583e705c121SKalle Valo 	default:
2584e705c121SKalle Valo 		ret = -EIO;
2585e705c121SKalle Valo 		IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2586e705c121SKalle Valo 			start ? "start" : "stopp", status);
2587e705c121SKalle Valo 		break;
2588e705c121SKalle Valo 	}
2589e705c121SKalle Valo 
259010b2b201SSara Sharon 	if (ret)
259110b2b201SSara Sharon 		goto out_free;
259210b2b201SSara Sharon 
259310b2b201SSara Sharon 	if (start) {
259410b2b201SSara Sharon 		u8 baid;
259510b2b201SSara Sharon 
2596e705c121SKalle Valo 		mvm->rx_ba_sessions++;
259710b2b201SSara Sharon 
259810b2b201SSara Sharon 		if (!iwl_mvm_has_new_rx_api(mvm))
259910b2b201SSara Sharon 			return 0;
260010b2b201SSara Sharon 
260110b2b201SSara Sharon 		if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
260210b2b201SSara Sharon 			ret = -EINVAL;
260310b2b201SSara Sharon 			goto out_free;
260410b2b201SSara Sharon 		}
260510b2b201SSara Sharon 		baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
260610b2b201SSara Sharon 			    IWL_ADD_STA_BAID_SHIFT);
260710b2b201SSara Sharon 		baid_data->baid = baid;
260810b2b201SSara Sharon 		baid_data->timeout = timeout;
260910b2b201SSara Sharon 		baid_data->last_rx = jiffies;
26108cef5344SKees Cook 		baid_data->rcu_ptr = &mvm->baid_map[baid];
26118cef5344SKees Cook 		timer_setup(&baid_data->session_timer,
26128cef5344SKees Cook 			    iwl_mvm_rx_agg_session_expired, 0);
261310b2b201SSara Sharon 		baid_data->mvm = mvm;
261410b2b201SSara Sharon 		baid_data->tid = tid;
261510b2b201SSara Sharon 		baid_data->sta_id = mvm_sta->sta_id;
261610b2b201SSara Sharon 
261710b2b201SSara Sharon 		mvm_sta->tid_to_baid[tid] = baid;
261810b2b201SSara Sharon 		if (timeout)
261910b2b201SSara Sharon 			mod_timer(&baid_data->session_timer,
262010b2b201SSara Sharon 				  TU_TO_EXP_TIME(timeout * 2));
262110b2b201SSara Sharon 
26223f1c4c58SSara Sharon 		iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size);
262310b2b201SSara Sharon 		/*
262410b2b201SSara Sharon 		 * protect the BA data with RCU to cover a case where our
262510b2b201SSara Sharon 		 * internal RX sync mechanism will timeout (not that it's
262610b2b201SSara Sharon 		 * supposed to happen) and we will free the session data while
262710b2b201SSara Sharon 		 * RX is being processed in parallel
262810b2b201SSara Sharon 		 */
262935263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
263035263a03SSara Sharon 			     mvm_sta->sta_id, tid, baid);
263110b2b201SSara Sharon 		WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
263210b2b201SSara Sharon 		rcu_assign_pointer(mvm->baid_map[baid], baid_data);
263360dec523SSara Sharon 	} else  {
263410b2b201SSara Sharon 		u8 baid = mvm_sta->tid_to_baid[tid];
263510b2b201SSara Sharon 
263660dec523SSara Sharon 		if (mvm->rx_ba_sessions > 0)
2637e705c121SKalle Valo 			/* check that restart flow didn't zero the counter */
2638e705c121SKalle Valo 			mvm->rx_ba_sessions--;
263910b2b201SSara Sharon 		if (!iwl_mvm_has_new_rx_api(mvm))
264010b2b201SSara Sharon 			return 0;
2641e705c121SKalle Valo 
264210b2b201SSara Sharon 		if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
264310b2b201SSara Sharon 			return -EINVAL;
264410b2b201SSara Sharon 
264510b2b201SSara Sharon 		baid_data = rcu_access_pointer(mvm->baid_map[baid]);
264610b2b201SSara Sharon 		if (WARN_ON(!baid_data))
264710b2b201SSara Sharon 			return -EINVAL;
264810b2b201SSara Sharon 
264910b2b201SSara Sharon 		/* synchronize all rx queues so we can safely delete */
2650b915c101SSara Sharon 		iwl_mvm_free_reorder(mvm, baid_data);
265110b2b201SSara Sharon 		del_timer_sync(&baid_data->session_timer);
265210b2b201SSara Sharon 		RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
265310b2b201SSara Sharon 		kfree_rcu(baid_data, rcu_head);
265435263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
265510b2b201SSara Sharon 	}
265610b2b201SSara Sharon 	return 0;
265710b2b201SSara Sharon 
265810b2b201SSara Sharon out_free:
265910b2b201SSara Sharon 	kfree(baid_data);
2660e705c121SKalle Valo 	return ret;
2661e705c121SKalle Valo }
2662e705c121SKalle Valo 
26639794c64fSLiad Kaufman int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2664e705c121SKalle Valo 		       int tid, u8 queue, bool start)
2665e705c121SKalle Valo {
2666e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2667e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
2668e705c121SKalle Valo 	int ret;
2669e705c121SKalle Valo 	u32 status;
2670e705c121SKalle Valo 
2671e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2672e705c121SKalle Valo 
2673e705c121SKalle Valo 	if (start) {
2674e705c121SKalle Valo 		mvm_sta->tfd_queue_msk |= BIT(queue);
2675e705c121SKalle Valo 		mvm_sta->tid_disable_agg &= ~BIT(tid);
2676e705c121SKalle Valo 	} else {
2677cf961e16SLiad Kaufman 		/* In DQA-mode the queue isn't removed on agg termination */
2678e705c121SKalle Valo 		mvm_sta->tid_disable_agg |= BIT(tid);
2679e705c121SKalle Valo 	}
2680e705c121SKalle Valo 
2681e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2682e705c121SKalle Valo 	cmd.sta_id = mvm_sta->sta_id;
2683e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
2684bb49701bSSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm))
2685bb49701bSSara Sharon 		cmd.modify_mask = STA_MODIFY_QUEUES;
2686bb49701bSSara Sharon 	cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
2687e705c121SKalle Valo 	cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2688e705c121SKalle Valo 	cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2689e705c121SKalle Valo 
2690e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
2691854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2692854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
2693e705c121SKalle Valo 					  &cmd, &status);
2694e705c121SKalle Valo 	if (ret)
2695e705c121SKalle Valo 		return ret;
2696e705c121SKalle Valo 
2697837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2698e705c121SKalle Valo 	case ADD_STA_SUCCESS:
2699e705c121SKalle Valo 		break;
2700e705c121SKalle Valo 	default:
2701e705c121SKalle Valo 		ret = -EIO;
2702e705c121SKalle Valo 		IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2703e705c121SKalle Valo 			start ? "start" : "stopp", status);
2704e705c121SKalle Valo 		break;
2705e705c121SKalle Valo 	}
2706e705c121SKalle Valo 
2707e705c121SKalle Valo 	return ret;
2708e705c121SKalle Valo }
2709e705c121SKalle Valo 
2710e705c121SKalle Valo const u8 tid_to_mac80211_ac[] = {
2711e705c121SKalle Valo 	IEEE80211_AC_BE,
2712e705c121SKalle Valo 	IEEE80211_AC_BK,
2713e705c121SKalle Valo 	IEEE80211_AC_BK,
2714e705c121SKalle Valo 	IEEE80211_AC_BE,
2715e705c121SKalle Valo 	IEEE80211_AC_VI,
2716e705c121SKalle Valo 	IEEE80211_AC_VI,
2717e705c121SKalle Valo 	IEEE80211_AC_VO,
2718e705c121SKalle Valo 	IEEE80211_AC_VO,
27199794c64fSLiad Kaufman 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2720e705c121SKalle Valo };
2721e705c121SKalle Valo 
2722e705c121SKalle Valo static const u8 tid_to_ucode_ac[] = {
2723e705c121SKalle Valo 	AC_BE,
2724e705c121SKalle Valo 	AC_BK,
2725e705c121SKalle Valo 	AC_BK,
2726e705c121SKalle Valo 	AC_BE,
2727e705c121SKalle Valo 	AC_VI,
2728e705c121SKalle Valo 	AC_VI,
2729e705c121SKalle Valo 	AC_VO,
2730e705c121SKalle Valo 	AC_VO,
2731e705c121SKalle Valo };
2732e705c121SKalle Valo 
2733e705c121SKalle Valo int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2734e705c121SKalle Valo 			     struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2735e705c121SKalle Valo {
2736e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2737e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data;
2738dd32162dSLiad Kaufman 	u16 normalized_ssn;
2739b0d795a9SMordechay Goodstein 	u16 txq_id;
2740e705c121SKalle Valo 	int ret;
2741e705c121SKalle Valo 
2742e705c121SKalle Valo 	if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2743e705c121SKalle Valo 		return -EINVAL;
2744e705c121SKalle Valo 
2745bd800e41SNaftali Goldstein 	if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
2746bd800e41SNaftali Goldstein 	    mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2747bd800e41SNaftali Goldstein 		IWL_ERR(mvm,
2748bd800e41SNaftali Goldstein 			"Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
2749e705c121SKalle Valo 			mvmsta->tid_data[tid].state);
2750e705c121SKalle Valo 		return -ENXIO;
2751e705c121SKalle Valo 	}
2752e705c121SKalle Valo 
2753e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2754e705c121SKalle Valo 
2755bd8f3fc6SLiad Kaufman 	if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE &&
2756bd8f3fc6SLiad Kaufman 	    iwl_mvm_has_new_tx_api(mvm)) {
2757bd8f3fc6SLiad Kaufman 		u8 ac = tid_to_mac80211_ac[tid];
2758bd8f3fc6SLiad Kaufman 
2759bd8f3fc6SLiad Kaufman 		ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
2760bd8f3fc6SLiad Kaufman 		if (ret)
2761bd8f3fc6SLiad Kaufman 			return ret;
2762bd8f3fc6SLiad Kaufman 	}
2763bd8f3fc6SLiad Kaufman 
2764e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2765e705c121SKalle Valo 
2766e705c121SKalle Valo 	/* possible race condition - we entered D0i3 while starting agg */
2767e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
2768e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
2769e705c121SKalle Valo 		IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
2770e705c121SKalle Valo 		return -EIO;
2771e705c121SKalle Valo 	}
2772e705c121SKalle Valo 
2773cf961e16SLiad Kaufman 	/*
2774cf961e16SLiad Kaufman 	 * Note the possible cases:
27754a6d2e52SAvraham Stern 	 *  1. An enabled TXQ - TXQ needs to become agg'ed
27764a6d2e52SAvraham Stern 	 *  2. The TXQ hasn't yet been enabled, so find a free one and mark
27774a6d2e52SAvraham Stern 	 *	it as reserved
2778cf961e16SLiad Kaufman 	 */
2779cf961e16SLiad Kaufman 	txq_id = mvmsta->tid_data[tid].txq_id;
278034e10860SSara Sharon 	if (txq_id == IWL_MVM_INVALID_QUEUE) {
2781b0d795a9SMordechay Goodstein 		ret = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2782c8f54701SJohannes Berg 					      IWL_MVM_DQA_MIN_DATA_QUEUE,
2783c8f54701SJohannes Berg 					      IWL_MVM_DQA_MAX_DATA_QUEUE);
2784b0d795a9SMordechay Goodstein 		if (ret < 0) {
2785e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to allocate agg queue\n");
2786f3f240f9SJohannes Berg 			goto out;
2787e705c121SKalle Valo 		}
2788cf961e16SLiad Kaufman 
2789b0d795a9SMordechay Goodstein 		txq_id = ret;
2790b0d795a9SMordechay Goodstein 
2791cf961e16SLiad Kaufman 		/* TXQ hasn't yet been enabled, so mark it only as reserved */
2792cf961e16SLiad Kaufman 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2793b0d795a9SMordechay Goodstein 	} else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) {
2794b0d795a9SMordechay Goodstein 		ret = -ENXIO;
2795b0d795a9SMordechay Goodstein 		IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n",
2796b0d795a9SMordechay Goodstein 			tid, IWL_MAX_HW_QUEUES - 1);
2797b0d795a9SMordechay Goodstein 		goto out;
2798b0d795a9SMordechay Goodstein 
27994a6d2e52SAvraham Stern 	} else if (unlikely(mvm->queue_info[txq_id].status ==
28004a6d2e52SAvraham Stern 			    IWL_MVM_QUEUE_SHARED)) {
28014a6d2e52SAvraham Stern 		ret = -ENXIO;
28024a6d2e52SAvraham Stern 		IWL_DEBUG_TX_QUEUES(mvm,
28034a6d2e52SAvraham Stern 				    "Can't start tid %d agg on shared queue!\n",
28044a6d2e52SAvraham Stern 				    tid);
2805f3f240f9SJohannes Berg 		goto out;
2806cf961e16SLiad Kaufman 	}
28079f9af3d7SLiad Kaufman 
2808cf961e16SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm,
2809cf961e16SLiad Kaufman 			    "AGG for tid %d will be on queue #%d\n",
2810cf961e16SLiad Kaufman 			    tid, txq_id);
2811cf961e16SLiad Kaufman 
2812e705c121SKalle Valo 	tid_data = &mvmsta->tid_data[tid];
2813e705c121SKalle Valo 	tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2814e705c121SKalle Valo 	tid_data->txq_id = txq_id;
2815e705c121SKalle Valo 	*ssn = tid_data->ssn;
2816e705c121SKalle Valo 
2817e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm,
2818e705c121SKalle Valo 			    "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2819e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2820e705c121SKalle Valo 			    tid_data->next_reclaimed);
2821e705c121SKalle Valo 
2822dd32162dSLiad Kaufman 	/*
28232f7a3863SLuca Coelho 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
2824dd32162dSLiad Kaufman 	 * to align the wrap around of ssn so we compare relevant values.
2825dd32162dSLiad Kaufman 	 */
2826dd32162dSLiad Kaufman 	normalized_ssn = tid_data->ssn;
2827dd32162dSLiad Kaufman 	if (mvm->trans->cfg->gen2)
2828dd32162dSLiad Kaufman 		normalized_ssn &= 0xff;
2829dd32162dSLiad Kaufman 
2830dd32162dSLiad Kaufman 	if (normalized_ssn == tid_data->next_reclaimed) {
2831e705c121SKalle Valo 		tid_data->state = IWL_AGG_STARTING;
2832e705c121SKalle Valo 		ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2833e705c121SKalle Valo 	} else {
2834e705c121SKalle Valo 		tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2835e705c121SKalle Valo 	}
2836e705c121SKalle Valo 
2837e705c121SKalle Valo 	ret = 0;
2838e705c121SKalle Valo 
28399f9af3d7SLiad Kaufman out:
2840e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2841e705c121SKalle Valo 
2842e705c121SKalle Valo 	return ret;
2843e705c121SKalle Valo }
2844e705c121SKalle Valo 
2845e705c121SKalle Valo int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2846514c3069SLuca Coelho 			    struct ieee80211_sta *sta, u16 tid, u16 buf_size,
2847bb81bb68SEmmanuel Grumbach 			    bool amsdu)
2848e705c121SKalle Valo {
2849e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2850e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2851e705c121SKalle Valo 	unsigned int wdg_timeout =
2852e705c121SKalle Valo 		iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
2853eea76c36SEmmanuel Grumbach 	int queue, ret;
2854cf961e16SLiad Kaufman 	bool alloc_queue = true;
28559f9af3d7SLiad Kaufman 	enum iwl_mvm_queue_status queue_status;
2856e705c121SKalle Valo 	u16 ssn;
2857e705c121SKalle Valo 
2858eea76c36SEmmanuel Grumbach 	struct iwl_trans_txq_scd_cfg cfg = {
2859eea76c36SEmmanuel Grumbach 		.sta_id = mvmsta->sta_id,
2860eea76c36SEmmanuel Grumbach 		.tid = tid,
2861eea76c36SEmmanuel Grumbach 		.frame_limit = buf_size,
2862eea76c36SEmmanuel Grumbach 		.aggregate = true,
2863eea76c36SEmmanuel Grumbach 	};
2864eea76c36SEmmanuel Grumbach 
2865ecaf71deSGregory Greenman 	/*
2866ecaf71deSGregory Greenman 	 * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
2867ecaf71deSGregory Greenman 	 * manager, so this function should never be called in this case.
2868ecaf71deSGregory Greenman 	 */
28694243edb4SEmmanuel Grumbach 	if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
2870ecaf71deSGregory Greenman 		return -EINVAL;
2871ecaf71deSGregory Greenman 
2872e705c121SKalle Valo 	BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2873e705c121SKalle Valo 		     != IWL_MAX_TID_COUNT);
2874e705c121SKalle Valo 
2875e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2876e705c121SKalle Valo 	ssn = tid_data->ssn;
2877e705c121SKalle Valo 	queue = tid_data->txq_id;
2878e705c121SKalle Valo 	tid_data->state = IWL_AGG_ON;
2879e705c121SKalle Valo 	mvmsta->agg_tids |= BIT(tid);
2880e705c121SKalle Valo 	tid_data->ssn = 0xffff;
2881bb81bb68SEmmanuel Grumbach 	tid_data->amsdu_in_ampdu_allowed = amsdu;
2882e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2883e705c121SKalle Valo 
288434e10860SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
288534e10860SSara Sharon 		/*
28860ec9257bSSara Sharon 		 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
28870ec9257bSSara Sharon 		 * would have failed, so if we are here there is no need to
28880ec9257bSSara Sharon 		 * allocate a queue.
28890ec9257bSSara Sharon 		 * However, if aggregation size is different than the default
28900ec9257bSSara Sharon 		 * size, the scheduler should be reconfigured.
28910ec9257bSSara Sharon 		 * We cannot do this with the new TX API, so return unsupported
28920ec9257bSSara Sharon 		 * for now, until it will be offloaded to firmware..
28930ec9257bSSara Sharon 		 * Note that if SCD default value changes - this condition
28940ec9257bSSara Sharon 		 * should be updated as well.
289534e10860SSara Sharon 		 */
28960ec9257bSSara Sharon 		if (buf_size < IWL_FRAME_LIMIT)
289734e10860SSara Sharon 			return -ENOTSUPP;
289834e10860SSara Sharon 
289934e10860SSara Sharon 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
290034e10860SSara Sharon 		if (ret)
290134e10860SSara Sharon 			return -EIO;
290234e10860SSara Sharon 		goto out;
290334e10860SSara Sharon 	}
290434e10860SSara Sharon 
2905eea76c36SEmmanuel Grumbach 	cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
2906e705c121SKalle Valo 
29079f9af3d7SLiad Kaufman 	queue_status = mvm->queue_info[queue].status;
29089f9af3d7SLiad Kaufman 
2909cf961e16SLiad Kaufman 	/* Maybe there is no need to even alloc a queue... */
2910cf961e16SLiad Kaufman 	if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2911cf961e16SLiad Kaufman 		alloc_queue = false;
2912cf961e16SLiad Kaufman 
2913cf961e16SLiad Kaufman 	/*
2914cf961e16SLiad Kaufman 	 * Only reconfig the SCD for the queue if the window size has
2915cf961e16SLiad Kaufman 	 * changed from current (become smaller)
2916cf961e16SLiad Kaufman 	 */
29170ec9257bSSara Sharon 	if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
2918cf961e16SLiad Kaufman 		/*
2919cf961e16SLiad Kaufman 		 * If reconfiguring an existing queue, it first must be
2920cf961e16SLiad Kaufman 		 * drained
2921cf961e16SLiad Kaufman 		 */
2922a1a57877SSara Sharon 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2923cf961e16SLiad Kaufman 						     BIT(queue));
2924cf961e16SLiad Kaufman 		if (ret) {
2925cf961e16SLiad Kaufman 			IWL_ERR(mvm,
2926cf961e16SLiad Kaufman 				"Error draining queue before reconfig\n");
2927cf961e16SLiad Kaufman 			return ret;
2928cf961e16SLiad Kaufman 		}
2929cf961e16SLiad Kaufman 
2930cf961e16SLiad Kaufman 		ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2931cf961e16SLiad Kaufman 					   mvmsta->sta_id, tid,
2932cf961e16SLiad Kaufman 					   buf_size, ssn);
2933cf961e16SLiad Kaufman 		if (ret) {
2934cf961e16SLiad Kaufman 			IWL_ERR(mvm,
2935cf961e16SLiad Kaufman 				"Error reconfiguring TXQ #%d\n", queue);
2936cf961e16SLiad Kaufman 			return ret;
2937cf961e16SLiad Kaufman 		}
2938cf961e16SLiad Kaufman 	}
2939cf961e16SLiad Kaufman 
2940cf961e16SLiad Kaufman 	if (alloc_queue)
2941cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, sta, queue, ssn,
2942cf961e16SLiad Kaufman 				   &cfg, wdg_timeout);
2943e705c121SKalle Valo 
29449f9af3d7SLiad Kaufman 	/* Send ADD_STA command to enable aggs only if the queue isn't shared */
29459f9af3d7SLiad Kaufman 	if (queue_status != IWL_MVM_QUEUE_SHARED) {
2946e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2947e705c121SKalle Valo 		if (ret)
2948e705c121SKalle Valo 			return -EIO;
29499f9af3d7SLiad Kaufman 	}
2950e705c121SKalle Valo 
2951e705c121SKalle Valo 	/* No need to mark as reserved */
2952cf961e16SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
2953e705c121SKalle Valo 
295434e10860SSara Sharon out:
2955e705c121SKalle Valo 	/*
2956e705c121SKalle Valo 	 * Even though in theory the peer could have different
2957e705c121SKalle Valo 	 * aggregation reorder buffer sizes for different sessions,
2958e705c121SKalle Valo 	 * our ucode doesn't allow for that and has a global limit
2959e705c121SKalle Valo 	 * for each station. Therefore, use the minimum of all the
2960e705c121SKalle Valo 	 * aggregation sessions and our default value.
2961e705c121SKalle Valo 	 */
2962e705c121SKalle Valo 	mvmsta->max_agg_bufsize =
2963e705c121SKalle Valo 		min(mvmsta->max_agg_bufsize, buf_size);
2964ecaf71deSGregory Greenman 	mvmsta->lq_sta.rs_drv.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2965e705c121SKalle Valo 
2966e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2967e705c121SKalle Valo 		     sta->addr, tid);
2968e705c121SKalle Valo 
2969ecaf71deSGregory Greenman 	return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.rs_drv.lq, false);
2970e705c121SKalle Valo }
2971e705c121SKalle Valo 
297234e10860SSara Sharon static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
297334e10860SSara Sharon 					struct iwl_mvm_sta *mvmsta,
29744b387906SAvraham Stern 					struct iwl_mvm_tid_data *tid_data)
297534e10860SSara Sharon {
29764b387906SAvraham Stern 	u16 txq_id = tid_data->txq_id;
29774b387906SAvraham Stern 
2978f3f240f9SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
2979f3f240f9SJohannes Berg 
298034e10860SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm))
298134e10860SSara Sharon 		return;
298234e10860SSara Sharon 
298334e10860SSara Sharon 	/*
298434e10860SSara Sharon 	 * The TXQ is marked as reserved only if no traffic came through yet
298534e10860SSara Sharon 	 * This means no traffic has been sent on this TID (agg'd or not), so
298634e10860SSara Sharon 	 * we no longer have use for the queue. Since it hasn't even been
298734e10860SSara Sharon 	 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
298834e10860SSara Sharon 	 * free.
298934e10860SSara Sharon 	 */
29904b387906SAvraham Stern 	if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
299134e10860SSara Sharon 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
29924b387906SAvraham Stern 		tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
29934b387906SAvraham Stern 	}
299434e10860SSara Sharon }
299534e10860SSara Sharon 
2996e705c121SKalle Valo int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2997e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid)
2998e705c121SKalle Valo {
2999e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3000e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3001e705c121SKalle Valo 	u16 txq_id;
3002e705c121SKalle Valo 	int err;
3003e705c121SKalle Valo 
3004e705c121SKalle Valo 	/*
3005e705c121SKalle Valo 	 * If mac80211 is cleaning its state, then say that we finished since
3006e705c121SKalle Valo 	 * our state has been cleared anyway.
3007e705c121SKalle Valo 	 */
3008e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3009e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3010e705c121SKalle Valo 		return 0;
3011e705c121SKalle Valo 	}
3012e705c121SKalle Valo 
3013e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3014e705c121SKalle Valo 
3015e705c121SKalle Valo 	txq_id = tid_data->txq_id;
3016e705c121SKalle Valo 
3017e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
3018e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
3019e705c121SKalle Valo 
3020e705c121SKalle Valo 	mvmsta->agg_tids &= ~BIT(tid);
3021e705c121SKalle Valo 
30224b387906SAvraham Stern 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3023e705c121SKalle Valo 
3024e705c121SKalle Valo 	switch (tid_data->state) {
3025e705c121SKalle Valo 	case IWL_AGG_ON:
3026e705c121SKalle Valo 		tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3027e705c121SKalle Valo 
3028e705c121SKalle Valo 		IWL_DEBUG_TX_QUEUES(mvm,
3029e705c121SKalle Valo 				    "ssn = %d, next_recl = %d\n",
3030e705c121SKalle Valo 				    tid_data->ssn, tid_data->next_reclaimed);
3031e705c121SKalle Valo 
3032e705c121SKalle Valo 		tid_data->ssn = 0xffff;
3033e705c121SKalle Valo 		tid_data->state = IWL_AGG_OFF;
3034e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
3035e705c121SKalle Valo 
3036e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3037e705c121SKalle Valo 
3038e705c121SKalle Valo 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3039e705c121SKalle Valo 		return 0;
3040e705c121SKalle Valo 	case IWL_AGG_STARTING:
3041e705c121SKalle Valo 	case IWL_EMPTYING_HW_QUEUE_ADDBA:
3042e705c121SKalle Valo 		/*
3043e705c121SKalle Valo 		 * The agg session has been stopped before it was set up. This
3044e705c121SKalle Valo 		 * can happen when the AddBA timer times out for example.
3045e705c121SKalle Valo 		 */
3046e705c121SKalle Valo 
3047e705c121SKalle Valo 		/* No barriers since we are under mutex */
3048e705c121SKalle Valo 		lockdep_assert_held(&mvm->mutex);
3049e705c121SKalle Valo 
3050e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3051e705c121SKalle Valo 		tid_data->state = IWL_AGG_OFF;
3052e705c121SKalle Valo 		err = 0;
3053e705c121SKalle Valo 		break;
3054e705c121SKalle Valo 	default:
3055e705c121SKalle Valo 		IWL_ERR(mvm,
3056e705c121SKalle Valo 			"Stopping AGG while state not ON or starting for %d on %d (%d)\n",
3057e705c121SKalle Valo 			mvmsta->sta_id, tid, tid_data->state);
3058e705c121SKalle Valo 		IWL_ERR(mvm,
3059e705c121SKalle Valo 			"\ttid_data->txq_id = %d\n", tid_data->txq_id);
3060e705c121SKalle Valo 		err = -EINVAL;
3061e705c121SKalle Valo 	}
3062e705c121SKalle Valo 
3063e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3064e705c121SKalle Valo 
3065e705c121SKalle Valo 	return err;
3066e705c121SKalle Valo }
3067e705c121SKalle Valo 
3068e705c121SKalle Valo int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3069e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid)
3070e705c121SKalle Valo {
3071e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3072e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3073e705c121SKalle Valo 	u16 txq_id;
3074e705c121SKalle Valo 	enum iwl_mvm_agg_state old_state;
3075e705c121SKalle Valo 
3076e705c121SKalle Valo 	/*
3077e705c121SKalle Valo 	 * First set the agg state to OFF to avoid calling
3078e705c121SKalle Valo 	 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
3079e705c121SKalle Valo 	 */
3080e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3081e705c121SKalle Valo 	txq_id = tid_data->txq_id;
3082e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
3083e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
3084e705c121SKalle Valo 	old_state = tid_data->state;
3085e705c121SKalle Valo 	tid_data->state = IWL_AGG_OFF;
3086e705c121SKalle Valo 	mvmsta->agg_tids &= ~BIT(tid);
3087e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3088e705c121SKalle Valo 
30894b387906SAvraham Stern 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3090e705c121SKalle Valo 
3091e705c121SKalle Valo 	if (old_state >= IWL_AGG_ON) {
3092e705c121SKalle Valo 		iwl_mvm_drain_sta(mvm, mvmsta, true);
3093d167e81aSMordechai Goodstein 
3094d167e81aSMordechai Goodstein 		if (iwl_mvm_has_new_tx_api(mvm)) {
3095d167e81aSMordechai Goodstein 			if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
3096d167e81aSMordechai Goodstein 						   BIT(tid), 0))
3097d167e81aSMordechai Goodstein 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3098d167e81aSMordechai Goodstein 			iwl_trans_wait_txq_empty(mvm->trans, txq_id);
3099d167e81aSMordechai Goodstein 		} else {
3100e705c121SKalle Valo 			if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
3101e705c121SKalle Valo 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
31020b90964aSSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
3103d167e81aSMordechai Goodstein 		}
3104d6d517b7SSara Sharon 
3105e705c121SKalle Valo 		iwl_mvm_drain_sta(mvm, mvmsta, false);
3106e705c121SKalle Valo 
3107e705c121SKalle Valo 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3108e705c121SKalle Valo 	}
3109e705c121SKalle Valo 
3110e705c121SKalle Valo 	return 0;
3111e705c121SKalle Valo }
3112e705c121SKalle Valo 
3113e705c121SKalle Valo static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
3114e705c121SKalle Valo {
3115e705c121SKalle Valo 	int i, max = -1, max_offs = -1;
3116e705c121SKalle Valo 
3117e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3118e705c121SKalle Valo 
3119e705c121SKalle Valo 	/* Pick the unused key offset with the highest 'deleted'
3120e705c121SKalle Valo 	 * counter. Every time a key is deleted, all the counters
3121e705c121SKalle Valo 	 * are incremented and the one that was just deleted is
3122e705c121SKalle Valo 	 * reset to zero. Thus, the highest counter is the one
3123e705c121SKalle Valo 	 * that was deleted longest ago. Pick that one.
3124e705c121SKalle Valo 	 */
3125e705c121SKalle Valo 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3126e705c121SKalle Valo 		if (test_bit(i, mvm->fw_key_table))
3127e705c121SKalle Valo 			continue;
3128e705c121SKalle Valo 		if (mvm->fw_key_deleted[i] > max) {
3129e705c121SKalle Valo 			max = mvm->fw_key_deleted[i];
3130e705c121SKalle Valo 			max_offs = i;
3131e705c121SKalle Valo 		}
3132e705c121SKalle Valo 	}
3133e705c121SKalle Valo 
3134e705c121SKalle Valo 	if (max_offs < 0)
3135e705c121SKalle Valo 		return STA_KEY_IDX_INVALID;
3136e705c121SKalle Valo 
3137e705c121SKalle Valo 	return max_offs;
3138e705c121SKalle Valo }
3139e705c121SKalle Valo 
31405f7a1847SJohannes Berg static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
31414615fd15SEmmanuel Grumbach 					       struct ieee80211_vif *vif,
3142e705c121SKalle Valo 					       struct ieee80211_sta *sta)
3143e705c121SKalle Valo {
3144e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3145e705c121SKalle Valo 
31465f7a1847SJohannes Berg 	if (sta)
31475f7a1847SJohannes Berg 		return iwl_mvm_sta_from_mac80211(sta);
3148e705c121SKalle Valo 
3149e705c121SKalle Valo 	/*
3150e705c121SKalle Valo 	 * The device expects GTKs for station interfaces to be
3151e705c121SKalle Valo 	 * installed as GTKs for the AP station. If we have no
3152e705c121SKalle Valo 	 * station ID, then use AP's station ID.
3153e705c121SKalle Valo 	 */
3154e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
31550ae98812SSara Sharon 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
31564615fd15SEmmanuel Grumbach 		u8 sta_id = mvmvif->ap_sta_id;
31574615fd15SEmmanuel Grumbach 
31587d6a1ab6SEmmanuel Grumbach 		sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
31597d6a1ab6SEmmanuel Grumbach 					    lockdep_is_held(&mvm->mutex));
31607d6a1ab6SEmmanuel Grumbach 
31614615fd15SEmmanuel Grumbach 		/*
31624615fd15SEmmanuel Grumbach 		 * It is possible that the 'sta' parameter is NULL,
31634615fd15SEmmanuel Grumbach 		 * for example when a GTK is removed - the sta_id will then
31644615fd15SEmmanuel Grumbach 		 * be the AP ID, and no station was passed by mac80211.
31654615fd15SEmmanuel Grumbach 		 */
31667d6a1ab6SEmmanuel Grumbach 		if (IS_ERR_OR_NULL(sta))
31677d6a1ab6SEmmanuel Grumbach 			return NULL;
31687d6a1ab6SEmmanuel Grumbach 
31697d6a1ab6SEmmanuel Grumbach 		return iwl_mvm_sta_from_mac80211(sta);
31704615fd15SEmmanuel Grumbach 	}
3171e705c121SKalle Valo 
31725f7a1847SJohannes Berg 	return NULL;
3173e705c121SKalle Valo }
3174e705c121SKalle Valo 
3175e705c121SKalle Valo static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
317685aeb58cSDavid Spinadel 				u32 sta_id,
317745c458b4SSara Sharon 				struct ieee80211_key_conf *key, bool mcast,
31784615fd15SEmmanuel Grumbach 				u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
31794883145aSEmmanuel Grumbach 				u8 key_offset, bool mfp)
3180e705c121SKalle Valo {
318145c458b4SSara Sharon 	union {
318245c458b4SSara Sharon 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
318345c458b4SSara Sharon 		struct iwl_mvm_add_sta_key_cmd cmd;
318445c458b4SSara Sharon 	} u = {};
3185e705c121SKalle Valo 	__le16 key_flags;
3186e705c121SKalle Valo 	int ret;
3187e705c121SKalle Valo 	u32 status;
3188e705c121SKalle Valo 	u16 keyidx;
318945c458b4SSara Sharon 	u64 pn = 0;
319045c458b4SSara Sharon 	int i, size;
319145c458b4SSara Sharon 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
319245c458b4SSara Sharon 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3193e705c121SKalle Valo 
319485aeb58cSDavid Spinadel 	if (sta_id == IWL_MVM_INVALID_STA)
319585aeb58cSDavid Spinadel 		return -EINVAL;
319685aeb58cSDavid Spinadel 
319745c458b4SSara Sharon 	keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
3198e705c121SKalle Valo 		 STA_KEY_FLG_KEYID_MSK;
3199e705c121SKalle Valo 	key_flags = cpu_to_le16(keyidx);
3200e705c121SKalle Valo 	key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3201e705c121SKalle Valo 
320245c458b4SSara Sharon 	switch (key->cipher) {
3203e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
3204e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
320545c458b4SSara Sharon 		if (new_api) {
320645c458b4SSara Sharon 			memcpy((void *)&u.cmd.tx_mic_key,
320745c458b4SSara Sharon 			       &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
320845c458b4SSara Sharon 			       IWL_MIC_KEY_SIZE);
320945c458b4SSara Sharon 
321045c458b4SSara Sharon 			memcpy((void *)&u.cmd.rx_mic_key,
321145c458b4SSara Sharon 			       &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
321245c458b4SSara Sharon 			       IWL_MIC_KEY_SIZE);
321345c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
321445c458b4SSara Sharon 
321545c458b4SSara Sharon 		} else {
321645c458b4SSara Sharon 			u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3217e705c121SKalle Valo 			for (i = 0; i < 5; i++)
321845c458b4SSara Sharon 				u.cmd_v1.tkip_rx_ttak[i] =
321945c458b4SSara Sharon 					cpu_to_le16(tkip_p1k[i]);
322045c458b4SSara Sharon 		}
322145c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3222e705c121SKalle Valo 		break;
3223e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
3224e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
322545c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
322645c458b4SSara Sharon 		if (new_api)
322745c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
3228e705c121SKalle Valo 		break;
3229e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
3230e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
3231e705c121SKalle Valo 		/* fall through */
3232e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
3233e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
323445c458b4SSara Sharon 		memcpy(u.cmd.common.key + 3, key->key, key->keylen);
3235e705c121SKalle Valo 		break;
32362a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
32372a53d166SAyala Beker 		key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
32382a53d166SAyala Beker 		/* fall through */
32392a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
32402a53d166SAyala Beker 		key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
324145c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
324245c458b4SSara Sharon 		if (new_api)
324345c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
32442a53d166SAyala Beker 		break;
3245e705c121SKalle Valo 	default:
3246e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
324745c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3248e705c121SKalle Valo 	}
3249e705c121SKalle Valo 
3250e705c121SKalle Valo 	if (mcast)
3251e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
32524883145aSEmmanuel Grumbach 	if (mfp)
32534883145aSEmmanuel Grumbach 		key_flags |= cpu_to_le16(STA_KEY_MFP);
3254e705c121SKalle Valo 
325545c458b4SSara Sharon 	u.cmd.common.key_offset = key_offset;
325645c458b4SSara Sharon 	u.cmd.common.key_flags = key_flags;
325785aeb58cSDavid Spinadel 	u.cmd.common.sta_id = sta_id;
325845c458b4SSara Sharon 
325945c458b4SSara Sharon 	if (new_api) {
326045c458b4SSara Sharon 		u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
326145c458b4SSara Sharon 		size = sizeof(u.cmd);
326245c458b4SSara Sharon 	} else {
326345c458b4SSara Sharon 		size = sizeof(u.cmd_v1);
326445c458b4SSara Sharon 	}
3265e705c121SKalle Valo 
3266e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
3267e705c121SKalle Valo 	if (cmd_flags & CMD_ASYNC)
326845c458b4SSara Sharon 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
326945c458b4SSara Sharon 					   &u.cmd);
3270e705c121SKalle Valo 	else
327145c458b4SSara Sharon 		ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
327245c458b4SSara Sharon 						  &u.cmd, &status);
3273e705c121SKalle Valo 
3274e705c121SKalle Valo 	switch (status) {
3275e705c121SKalle Valo 	case ADD_STA_SUCCESS:
3276e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3277e705c121SKalle Valo 		break;
3278e705c121SKalle Valo 	default:
3279e705c121SKalle Valo 		ret = -EIO;
3280e705c121SKalle Valo 		IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3281e705c121SKalle Valo 		break;
3282e705c121SKalle Valo 	}
3283e705c121SKalle Valo 
3284e705c121SKalle Valo 	return ret;
3285e705c121SKalle Valo }
3286e705c121SKalle Valo 
3287e705c121SKalle Valo static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3288e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
3289e705c121SKalle Valo 				 u8 sta_id, bool remove_key)
3290e705c121SKalle Valo {
3291e705c121SKalle Valo 	struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3292e705c121SKalle Valo 
3293e705c121SKalle Valo 	/* verify the key details match the required command's expectations */
32948e160ab8SAyala Beker 	if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
32958e160ab8SAyala Beker 		    (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
32968e160ab8SAyala Beker 		    (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
32978e160ab8SAyala Beker 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
32988e160ab8SAyala Beker 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
32998e160ab8SAyala Beker 		return -EINVAL;
33008e160ab8SAyala Beker 
33018e160ab8SAyala Beker 	if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
33028e160ab8SAyala Beker 		    keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3303e705c121SKalle Valo 		return -EINVAL;
3304e705c121SKalle Valo 
3305e705c121SKalle Valo 	igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3306e705c121SKalle Valo 	igtk_cmd.sta_id = cpu_to_le32(sta_id);
3307e705c121SKalle Valo 
3308e705c121SKalle Valo 	if (remove_key) {
3309e705c121SKalle Valo 		igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3310e705c121SKalle Valo 	} else {
3311e705c121SKalle Valo 		struct ieee80211_key_seq seq;
3312e705c121SKalle Valo 		const u8 *pn;
3313e705c121SKalle Valo 
3314aa950524SAyala Beker 		switch (keyconf->cipher) {
3315aa950524SAyala Beker 		case WLAN_CIPHER_SUITE_AES_CMAC:
3316aa950524SAyala Beker 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3317aa950524SAyala Beker 			break;
33188e160ab8SAyala Beker 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
33198e160ab8SAyala Beker 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
33208e160ab8SAyala Beker 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
33218e160ab8SAyala Beker 			break;
3322aa950524SAyala Beker 		default:
3323aa950524SAyala Beker 			return -EINVAL;
3324aa950524SAyala Beker 		}
3325aa950524SAyala Beker 
33268e160ab8SAyala Beker 		memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
33278e160ab8SAyala Beker 		if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
33288e160ab8SAyala Beker 			igtk_cmd.ctrl_flags |=
33298e160ab8SAyala Beker 				cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3330e705c121SKalle Valo 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3331e705c121SKalle Valo 		pn = seq.aes_cmac.pn;
3332e705c121SKalle Valo 		igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3333e705c121SKalle Valo 						       ((u64) pn[4] << 8) |
3334e705c121SKalle Valo 						       ((u64) pn[3] << 16) |
3335e705c121SKalle Valo 						       ((u64) pn[2] << 24) |
3336e705c121SKalle Valo 						       ((u64) pn[1] << 32) |
3337e705c121SKalle Valo 						       ((u64) pn[0] << 40));
3338e705c121SKalle Valo 	}
3339e705c121SKalle Valo 
3340e705c121SKalle Valo 	IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
3341e705c121SKalle Valo 		       remove_key ? "removing" : "installing",
3342e705c121SKalle Valo 		       igtk_cmd.sta_id);
3343e705c121SKalle Valo 
33448e160ab8SAyala Beker 	if (!iwl_mvm_has_new_rx_api(mvm)) {
33458e160ab8SAyala Beker 		struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
33468e160ab8SAyala Beker 			.ctrl_flags = igtk_cmd.ctrl_flags,
33478e160ab8SAyala Beker 			.key_id = igtk_cmd.key_id,
33488e160ab8SAyala Beker 			.sta_id = igtk_cmd.sta_id,
33498e160ab8SAyala Beker 			.receive_seq_cnt = igtk_cmd.receive_seq_cnt
33508e160ab8SAyala Beker 		};
33518e160ab8SAyala Beker 
33528e160ab8SAyala Beker 		memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
33538e160ab8SAyala Beker 		       ARRAY_SIZE(igtk_cmd_v1.igtk));
33548e160ab8SAyala Beker 		return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
33558e160ab8SAyala Beker 					    sizeof(igtk_cmd_v1), &igtk_cmd_v1);
33568e160ab8SAyala Beker 	}
3357e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3358e705c121SKalle Valo 				    sizeof(igtk_cmd), &igtk_cmd);
3359e705c121SKalle Valo }
3360e705c121SKalle Valo 
3361e705c121SKalle Valo 
3362e705c121SKalle Valo static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3363e705c121SKalle Valo 				       struct ieee80211_vif *vif,
3364e705c121SKalle Valo 				       struct ieee80211_sta *sta)
3365e705c121SKalle Valo {
3366e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3367e705c121SKalle Valo 
3368e705c121SKalle Valo 	if (sta)
3369e705c121SKalle Valo 		return sta->addr;
3370e705c121SKalle Valo 
3371e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
33720ae98812SSara Sharon 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3373e705c121SKalle Valo 		u8 sta_id = mvmvif->ap_sta_id;
3374e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3375e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
3376e705c121SKalle Valo 		return sta->addr;
3377e705c121SKalle Valo 	}
3378e705c121SKalle Valo 
3379e705c121SKalle Valo 
3380e705c121SKalle Valo 	return NULL;
3381e705c121SKalle Valo }
3382e705c121SKalle Valo 
3383e705c121SKalle Valo static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3384e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3385e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3386e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
33874615fd15SEmmanuel Grumbach 				 u8 key_offset,
3388e705c121SKalle Valo 				 bool mcast)
3389e705c121SKalle Valo {
3390e705c121SKalle Valo 	int ret;
3391e705c121SKalle Valo 	const u8 *addr;
3392e705c121SKalle Valo 	struct ieee80211_key_seq seq;
3393e705c121SKalle Valo 	u16 p1k[5];
339485aeb58cSDavid Spinadel 	u32 sta_id;
33954883145aSEmmanuel Grumbach 	bool mfp = false;
339685aeb58cSDavid Spinadel 
339785aeb58cSDavid Spinadel 	if (sta) {
339885aeb58cSDavid Spinadel 		struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
339985aeb58cSDavid Spinadel 
340085aeb58cSDavid Spinadel 		sta_id = mvm_sta->sta_id;
34014883145aSEmmanuel Grumbach 		mfp = sta->mfp;
340285aeb58cSDavid Spinadel 	} else if (vif->type == NL80211_IFTYPE_AP &&
340385aeb58cSDavid Spinadel 		   !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
340485aeb58cSDavid Spinadel 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
340585aeb58cSDavid Spinadel 
340685aeb58cSDavid Spinadel 		sta_id = mvmvif->mcast_sta.sta_id;
340785aeb58cSDavid Spinadel 	} else {
340885aeb58cSDavid Spinadel 		IWL_ERR(mvm, "Failed to find station id\n");
340985aeb58cSDavid Spinadel 		return -EINVAL;
341085aeb58cSDavid Spinadel 	}
3411e705c121SKalle Valo 
3412e705c121SKalle Valo 	switch (keyconf->cipher) {
3413e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
3414e705c121SKalle Valo 		addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3415e705c121SKalle Valo 		/* get phase 1 key from mac80211 */
3416e705c121SKalle Valo 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3417e705c121SKalle Valo 		ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
341885aeb58cSDavid Spinadel 		ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
34194883145aSEmmanuel Grumbach 					   seq.tkip.iv32, p1k, 0, key_offset,
34204883145aSEmmanuel Grumbach 					   mfp);
3421e705c121SKalle Valo 		break;
3422e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
3423e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
3424e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
34252a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
34262a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
342785aeb58cSDavid Spinadel 		ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
34284883145aSEmmanuel Grumbach 					   0, NULL, 0, key_offset, mfp);
3429e705c121SKalle Valo 		break;
3430e705c121SKalle Valo 	default:
343185aeb58cSDavid Spinadel 		ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
34324883145aSEmmanuel Grumbach 					   0, NULL, 0, key_offset, mfp);
3433e705c121SKalle Valo 	}
3434e705c121SKalle Valo 
3435e705c121SKalle Valo 	return ret;
3436e705c121SKalle Valo }
3437e705c121SKalle Valo 
3438e705c121SKalle Valo int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3439e705c121SKalle Valo 			struct ieee80211_vif *vif,
3440e705c121SKalle Valo 			struct ieee80211_sta *sta,
3441e705c121SKalle Valo 			struct ieee80211_key_conf *keyconf,
34424615fd15SEmmanuel Grumbach 			u8 key_offset)
3443e705c121SKalle Valo {
3444e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
34455f7a1847SJohannes Berg 	struct iwl_mvm_sta *mvm_sta;
344685aeb58cSDavid Spinadel 	u8 sta_id = IWL_MVM_INVALID_STA;
3447e705c121SKalle Valo 	int ret;
3448e705c121SKalle Valo 	static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3449e705c121SKalle Valo 
3450e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3451e705c121SKalle Valo 
345285aeb58cSDavid Spinadel 	if (vif->type != NL80211_IFTYPE_AP ||
345385aeb58cSDavid Spinadel 	    keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3454e705c121SKalle Valo 		/* Get the station id from the mvm local station table */
34555f7a1847SJohannes Berg 		mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
34565f7a1847SJohannes Berg 		if (!mvm_sta) {
34575f7a1847SJohannes Berg 			IWL_ERR(mvm, "Failed to find station\n");
3458e705c121SKalle Valo 			return -EINVAL;
3459e705c121SKalle Valo 		}
34605f7a1847SJohannes Berg 		sta_id = mvm_sta->sta_id;
3461e705c121SKalle Valo 
3462e705c121SKalle Valo 		/*
3463e705c121SKalle Valo 		 * It is possible that the 'sta' parameter is NULL, and thus
346485aeb58cSDavid Spinadel 		 * there is a need to retrieve the sta from the local station
346585aeb58cSDavid Spinadel 		 * table.
3466e705c121SKalle Valo 		 */
3467e705c121SKalle Valo 		if (!sta) {
346885aeb58cSDavid Spinadel 			sta = rcu_dereference_protected(
346985aeb58cSDavid Spinadel 				mvm->fw_id_to_mac_id[sta_id],
3470e705c121SKalle Valo 				lockdep_is_held(&mvm->mutex));
3471e705c121SKalle Valo 			if (IS_ERR_OR_NULL(sta)) {
3472e705c121SKalle Valo 				IWL_ERR(mvm, "Invalid station id\n");
3473e705c121SKalle Valo 				return -EINVAL;
3474e705c121SKalle Valo 			}
3475e705c121SKalle Valo 		}
3476e705c121SKalle Valo 
3477e705c121SKalle Valo 		if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3478e705c121SKalle Valo 			return -EINVAL;
3479e829b17cSBeni Lev 	} else {
3480e829b17cSBeni Lev 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3481e829b17cSBeni Lev 
3482e829b17cSBeni Lev 		sta_id = mvmvif->mcast_sta.sta_id;
3483e829b17cSBeni Lev 	}
3484e829b17cSBeni Lev 
3485e829b17cSBeni Lev 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3486e829b17cSBeni Lev 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3487e829b17cSBeni Lev 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3488e829b17cSBeni Lev 		ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3489e829b17cSBeni Lev 		goto end;
349085aeb58cSDavid Spinadel 	}
3491e705c121SKalle Valo 
34924615fd15SEmmanuel Grumbach 	/* If the key_offset is not pre-assigned, we need to find a
34934615fd15SEmmanuel Grumbach 	 * new offset to use.  In normal cases, the offset is not
34944615fd15SEmmanuel Grumbach 	 * pre-assigned, but during HW_RESTART we want to reuse the
34954615fd15SEmmanuel Grumbach 	 * same indices, so we pass them when this function is called.
34964615fd15SEmmanuel Grumbach 	 *
34974615fd15SEmmanuel Grumbach 	 * In D3 entry, we need to hardcoded the indices (because the
34984615fd15SEmmanuel Grumbach 	 * firmware hardcodes the PTK offset to 0).  In this case, we
34994615fd15SEmmanuel Grumbach 	 * need to make sure we don't overwrite the hw_key_idx in the
35004615fd15SEmmanuel Grumbach 	 * keyconf structure, because otherwise we cannot configure
35014615fd15SEmmanuel Grumbach 	 * the original ones back when resuming.
3502e705c121SKalle Valo 	 */
35034615fd15SEmmanuel Grumbach 	if (key_offset == STA_KEY_IDX_INVALID) {
35044615fd15SEmmanuel Grumbach 		key_offset  = iwl_mvm_set_fw_key_idx(mvm);
35054615fd15SEmmanuel Grumbach 		if (key_offset == STA_KEY_IDX_INVALID)
3506e705c121SKalle Valo 			return -ENOSPC;
35074615fd15SEmmanuel Grumbach 		keyconf->hw_key_idx = key_offset;
3508e705c121SKalle Valo 	}
3509e705c121SKalle Valo 
35104615fd15SEmmanuel Grumbach 	ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
35119c3deeb5SLuca Coelho 	if (ret)
3512e705c121SKalle Valo 		goto end;
3513e705c121SKalle Valo 
3514e705c121SKalle Valo 	/*
3515e705c121SKalle Valo 	 * For WEP, the same key is used for multicast and unicast. Upload it
3516e705c121SKalle Valo 	 * again, using the same key offset, and now pointing the other one
3517e705c121SKalle Valo 	 * to the same key slot (offset).
3518e705c121SKalle Valo 	 * If this fails, remove the original as well.
3519e705c121SKalle Valo 	 */
352085aeb58cSDavid Spinadel 	if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
352185aeb58cSDavid Spinadel 	     keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
352285aeb58cSDavid Spinadel 	    sta) {
35234615fd15SEmmanuel Grumbach 		ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
35244615fd15SEmmanuel Grumbach 					    key_offset, !mcast);
3525e705c121SKalle Valo 		if (ret) {
3526e705c121SKalle Valo 			__iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
35279c3deeb5SLuca Coelho 			goto end;
3528e705c121SKalle Valo 		}
3529e705c121SKalle Valo 	}
3530e705c121SKalle Valo 
35319c3deeb5SLuca Coelho 	__set_bit(key_offset, mvm->fw_key_table);
35329c3deeb5SLuca Coelho 
3533e705c121SKalle Valo end:
3534e705c121SKalle Valo 	IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3535e705c121SKalle Valo 		      keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3536e705c121SKalle Valo 		      sta ? sta->addr : zero_addr, ret);
3537e705c121SKalle Valo 	return ret;
3538e705c121SKalle Valo }
3539e705c121SKalle Valo 
3540e705c121SKalle Valo int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3541e705c121SKalle Valo 			   struct ieee80211_vif *vif,
3542e705c121SKalle Valo 			   struct ieee80211_sta *sta,
3543e705c121SKalle Valo 			   struct ieee80211_key_conf *keyconf)
3544e705c121SKalle Valo {
3545e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
35465f7a1847SJohannes Berg 	struct iwl_mvm_sta *mvm_sta;
35470ae98812SSara Sharon 	u8 sta_id = IWL_MVM_INVALID_STA;
3548e705c121SKalle Valo 	int ret, i;
3549e705c121SKalle Valo 
3550e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3551e705c121SKalle Valo 
35525f7a1847SJohannes Berg 	/* Get the station from the mvm local station table */
35535f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
355471793b7dSLuca Coelho 	if (mvm_sta)
3555cd4d23c1SIlan Peer 		sta_id = mvm_sta->sta_id;
355685aeb58cSDavid Spinadel 	else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
355785aeb58cSDavid Spinadel 		sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
355885aeb58cSDavid Spinadel 
3559e705c121SKalle Valo 
3560e705c121SKalle Valo 	IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3561e705c121SKalle Valo 		      keyconf->keyidx, sta_id);
3562e705c121SKalle Valo 
356371793b7dSLuca Coelho 	if (mvm_sta && (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
35648e160ab8SAyala Beker 			keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
356571793b7dSLuca Coelho 			keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256))
3566e705c121SKalle Valo 		return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3567e705c121SKalle Valo 
3568e705c121SKalle Valo 	if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3569e705c121SKalle Valo 		IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3570e705c121SKalle Valo 			keyconf->hw_key_idx);
3571e705c121SKalle Valo 		return -ENOENT;
3572e705c121SKalle Valo 	}
3573e705c121SKalle Valo 
3574e705c121SKalle Valo 	/* track which key was deleted last */
3575e705c121SKalle Valo 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3576e705c121SKalle Valo 		if (mvm->fw_key_deleted[i] < U8_MAX)
3577e705c121SKalle Valo 			mvm->fw_key_deleted[i]++;
3578e705c121SKalle Valo 	}
3579e705c121SKalle Valo 	mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3580e705c121SKalle Valo 
358185aeb58cSDavid Spinadel 	if (sta && !mvm_sta) {
3582e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3583e705c121SKalle Valo 		return 0;
3584e705c121SKalle Valo 	}
3585e705c121SKalle Valo 
3586e705c121SKalle Valo 	ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3587e705c121SKalle Valo 	if (ret)
3588e705c121SKalle Valo 		return ret;
3589e705c121SKalle Valo 
3590e705c121SKalle Valo 	/* delete WEP key twice to get rid of (now useless) offset */
3591e705c121SKalle Valo 	if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3592e705c121SKalle Valo 	    keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3593e705c121SKalle Valo 		ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3594e705c121SKalle Valo 
3595e705c121SKalle Valo 	return ret;
3596e705c121SKalle Valo }
3597e705c121SKalle Valo 
3598e705c121SKalle Valo void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3599e705c121SKalle Valo 			     struct ieee80211_vif *vif,
3600e705c121SKalle Valo 			     struct ieee80211_key_conf *keyconf,
3601e705c121SKalle Valo 			     struct ieee80211_sta *sta, u32 iv32,
3602e705c121SKalle Valo 			     u16 *phase1key)
3603e705c121SKalle Valo {
3604e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta;
3605e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
36064883145aSEmmanuel Grumbach 	bool mfp = sta ? sta->mfp : false;
3607e705c121SKalle Valo 
3608e705c121SKalle Valo 	rcu_read_lock();
3609e705c121SKalle Valo 
36105f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
36115f7a1847SJohannes Berg 	if (WARN_ON_ONCE(!mvm_sta))
361212f17211SEmmanuel Grumbach 		goto unlock;
361385aeb58cSDavid Spinadel 	iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
36144883145aSEmmanuel Grumbach 			     iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx,
36154883145aSEmmanuel Grumbach 			     mfp);
361612f17211SEmmanuel Grumbach 
361712f17211SEmmanuel Grumbach  unlock:
3618e705c121SKalle Valo 	rcu_read_unlock();
3619e705c121SKalle Valo }
3620e705c121SKalle Valo 
3621e705c121SKalle Valo void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3622e705c121SKalle Valo 				struct ieee80211_sta *sta)
3623e705c121SKalle Valo {
3624e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3625e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3626e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3627e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3628e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_PS),
3629e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3630e705c121SKalle Valo 	};
3631e705c121SKalle Valo 	int ret;
3632e705c121SKalle Valo 
3633854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3634854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3635e705c121SKalle Valo 	if (ret)
3636e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3637e705c121SKalle Valo }
3638e705c121SKalle Valo 
3639e705c121SKalle Valo void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3640e705c121SKalle Valo 				       struct ieee80211_sta *sta,
3641e705c121SKalle Valo 				       enum ieee80211_frame_release_type reason,
3642e705c121SKalle Valo 				       u16 cnt, u16 tids, bool more_data,
36439a3fcf91SSara Sharon 				       bool single_sta_queue)
3644e705c121SKalle Valo {
3645e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3646e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3647e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3648e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3649e705c121SKalle Valo 		.modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3650e705c121SKalle Valo 		.sleep_tx_count = cpu_to_le16(cnt),
3651e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3652e705c121SKalle Valo 	};
3653e705c121SKalle Valo 	int tid, ret;
3654e705c121SKalle Valo 	unsigned long _tids = tids;
3655e705c121SKalle Valo 
3656e705c121SKalle Valo 	/* convert TIDs to ACs - we don't support TSPEC so that's OK
3657e705c121SKalle Valo 	 * Note that this field is reserved and unused by firmware not
3658e705c121SKalle Valo 	 * supporting GO uAPSD, so it's safe to always do this.
3659e705c121SKalle Valo 	 */
3660e705c121SKalle Valo 	for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3661e705c121SKalle Valo 		cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3662e705c121SKalle Valo 
36639a3fcf91SSara Sharon 	/* If we're releasing frames from aggregation or dqa queues then check
36649a3fcf91SSara Sharon 	 * if all the queues that we're releasing frames from, combined, have:
3665e705c121SKalle Valo 	 *  - more frames than the service period, in which case more_data
3666e705c121SKalle Valo 	 *    needs to be set
3667e705c121SKalle Valo 	 *  - fewer than 'cnt' frames, in which case we need to adjust the
3668e705c121SKalle Valo 	 *    firmware command (but do that unconditionally)
3669e705c121SKalle Valo 	 */
36709a3fcf91SSara Sharon 	if (single_sta_queue) {
3671e705c121SKalle Valo 		int remaining = cnt;
367236be0eb6SEmmanuel Grumbach 		int sleep_tx_count;
3673e705c121SKalle Valo 
3674e705c121SKalle Valo 		spin_lock_bh(&mvmsta->lock);
3675e705c121SKalle Valo 		for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3676e705c121SKalle Valo 			struct iwl_mvm_tid_data *tid_data;
3677e705c121SKalle Valo 			u16 n_queued;
3678e705c121SKalle Valo 
3679e705c121SKalle Valo 			tid_data = &mvmsta->tid_data[tid];
3680e705c121SKalle Valo 
3681dd32162dSLiad Kaufman 			n_queued = iwl_mvm_tid_queued(mvm, tid_data);
3682e705c121SKalle Valo 			if (n_queued > remaining) {
3683e705c121SKalle Valo 				more_data = true;
3684e705c121SKalle Valo 				remaining = 0;
3685e705c121SKalle Valo 				break;
3686e705c121SKalle Valo 			}
3687e705c121SKalle Valo 			remaining -= n_queued;
3688e705c121SKalle Valo 		}
368936be0eb6SEmmanuel Grumbach 		sleep_tx_count = cnt - remaining;
369036be0eb6SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
369136be0eb6SEmmanuel Grumbach 			mvmsta->sleep_tx_count = sleep_tx_count;
3692e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
3693e705c121SKalle Valo 
369436be0eb6SEmmanuel Grumbach 		cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3695e705c121SKalle Valo 		if (WARN_ON(cnt - remaining == 0)) {
3696e705c121SKalle Valo 			ieee80211_sta_eosp(sta);
3697e705c121SKalle Valo 			return;
3698e705c121SKalle Valo 		}
3699e705c121SKalle Valo 	}
3700e705c121SKalle Valo 
3701e705c121SKalle Valo 	/* Note: this is ignored by firmware not supporting GO uAPSD */
3702e705c121SKalle Valo 	if (more_data)
3703ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
3704e705c121SKalle Valo 
3705e705c121SKalle Valo 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3706e705c121SKalle Valo 		mvmsta->next_status_eosp = true;
3707ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
3708e705c121SKalle Valo 	} else {
3709ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
3710e705c121SKalle Valo 	}
3711e705c121SKalle Valo 
3712156f92f2SEmmanuel Grumbach 	/* block the Tx queues until the FW updated the sleep Tx count */
3713156f92f2SEmmanuel Grumbach 	iwl_trans_block_txq_ptrs(mvm->trans, true);
3714156f92f2SEmmanuel Grumbach 
3715156f92f2SEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3716156f92f2SEmmanuel Grumbach 				   CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3717854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3718e705c121SKalle Valo 	if (ret)
3719e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3720e705c121SKalle Valo }
3721e705c121SKalle Valo 
3722e705c121SKalle Valo void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3723e705c121SKalle Valo 			   struct iwl_rx_cmd_buffer *rxb)
3724e705c121SKalle Valo {
3725e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3726e705c121SKalle Valo 	struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3727e705c121SKalle Valo 	struct ieee80211_sta *sta;
3728e705c121SKalle Valo 	u32 sta_id = le32_to_cpu(notif->sta_id);
3729e705c121SKalle Valo 
3730e705c121SKalle Valo 	if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
3731e705c121SKalle Valo 		return;
3732e705c121SKalle Valo 
3733e705c121SKalle Valo 	rcu_read_lock();
3734e705c121SKalle Valo 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3735e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(sta))
3736e705c121SKalle Valo 		ieee80211_sta_eosp(sta);
3737e705c121SKalle Valo 	rcu_read_unlock();
3738e705c121SKalle Valo }
3739e705c121SKalle Valo 
3740e705c121SKalle Valo void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3741e705c121SKalle Valo 				   struct iwl_mvm_sta *mvmsta, bool disable)
3742e705c121SKalle Valo {
3743e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3744e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3745e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3746e705c121SKalle Valo 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3747e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3748e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3749e705c121SKalle Valo 	};
3750e705c121SKalle Valo 	int ret;
3751e705c121SKalle Valo 
3752854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3753854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3754e705c121SKalle Valo 	if (ret)
3755e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3756e705c121SKalle Valo }
3757e705c121SKalle Valo 
3758e705c121SKalle Valo void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3759e705c121SKalle Valo 				      struct ieee80211_sta *sta,
3760e705c121SKalle Valo 				      bool disable)
3761e705c121SKalle Valo {
3762e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3763e705c121SKalle Valo 
3764e705c121SKalle Valo 	spin_lock_bh(&mvm_sta->lock);
3765e705c121SKalle Valo 
3766e705c121SKalle Valo 	if (mvm_sta->disable_tx == disable) {
3767e705c121SKalle Valo 		spin_unlock_bh(&mvm_sta->lock);
3768e705c121SKalle Valo 		return;
3769e705c121SKalle Valo 	}
3770e705c121SKalle Valo 
3771e705c121SKalle Valo 	mvm_sta->disable_tx = disable;
3772e705c121SKalle Valo 
3773c8f54701SJohannes Berg 	/* Tell mac80211 to start/stop queuing tx for this station */
3774e705c121SKalle Valo 	ieee80211_sta_block_awake(mvm->hw, sta, disable);
3775e705c121SKalle Valo 
3776e705c121SKalle Valo 	iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3777e705c121SKalle Valo 
3778e705c121SKalle Valo 	spin_unlock_bh(&mvm_sta->lock);
3779e705c121SKalle Valo }
3780e705c121SKalle Valo 
3781ced19f26SSara Sharon static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
3782ced19f26SSara Sharon 					      struct iwl_mvm_vif *mvmvif,
3783ced19f26SSara Sharon 					      struct iwl_mvm_int_sta *sta,
3784ced19f26SSara Sharon 					      bool disable)
3785ced19f26SSara Sharon {
3786ced19f26SSara Sharon 	u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
3787ced19f26SSara Sharon 	struct iwl_mvm_add_sta_cmd cmd = {
3788ced19f26SSara Sharon 		.add_modify = STA_MODE_MODIFY,
3789ced19f26SSara Sharon 		.sta_id = sta->sta_id,
3790ced19f26SSara Sharon 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3791ced19f26SSara Sharon 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3792ced19f26SSara Sharon 		.mac_id_n_color = cpu_to_le32(id),
3793ced19f26SSara Sharon 	};
3794ced19f26SSara Sharon 	int ret;
3795ced19f26SSara Sharon 
3796ced19f26SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, 0,
3797ced19f26SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3798ced19f26SSara Sharon 	if (ret)
3799ced19f26SSara Sharon 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3800ced19f26SSara Sharon }
3801ced19f26SSara Sharon 
3802e705c121SKalle Valo void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3803e705c121SKalle Valo 				       struct iwl_mvm_vif *mvmvif,
3804e705c121SKalle Valo 				       bool disable)
3805e705c121SKalle Valo {
3806e705c121SKalle Valo 	struct ieee80211_sta *sta;
3807e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta;
3808e705c121SKalle Valo 	int i;
3809e705c121SKalle Valo 
3810e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3811e705c121SKalle Valo 
3812e705c121SKalle Valo 	/* Block/unblock all the stations of the given mvmvif */
38130ae98812SSara Sharon 	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
3814e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3815e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
3816e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
3817e705c121SKalle Valo 			continue;
3818e705c121SKalle Valo 
3819e705c121SKalle Valo 		mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3820e705c121SKalle Valo 		if (mvm_sta->mac_id_n_color !=
3821e705c121SKalle Valo 		    FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3822e705c121SKalle Valo 			continue;
3823e705c121SKalle Valo 
3824e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3825e705c121SKalle Valo 	}
3826ced19f26SSara Sharon 
3827ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3828ced19f26SSara Sharon 		return;
3829ced19f26SSara Sharon 
3830ced19f26SSara Sharon 	/* Need to block/unblock also multicast station */
3831ced19f26SSara Sharon 	if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
3832ced19f26SSara Sharon 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3833ced19f26SSara Sharon 						  &mvmvif->mcast_sta, disable);
3834ced19f26SSara Sharon 
3835ced19f26SSara Sharon 	/*
3836ced19f26SSara Sharon 	 * Only unblock the broadcast station (FW blocks it for immediate
3837ced19f26SSara Sharon 	 * quiet, not the driver)
3838ced19f26SSara Sharon 	 */
3839ced19f26SSara Sharon 	if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
3840ced19f26SSara Sharon 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3841ced19f26SSara Sharon 						  &mvmvif->bcast_sta, disable);
3842e705c121SKalle Valo }
3843e705c121SKalle Valo 
3844e705c121SKalle Valo void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3845e705c121SKalle Valo {
3846e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3847e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
3848e705c121SKalle Valo 
3849e705c121SKalle Valo 	rcu_read_lock();
3850e705c121SKalle Valo 
3851e705c121SKalle Valo 	mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3852e705c121SKalle Valo 
3853e705c121SKalle Valo 	if (!WARN_ON(!mvmsta))
3854e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3855e705c121SKalle Valo 
3856e705c121SKalle Valo 	rcu_read_unlock();
3857e705c121SKalle Valo }
3858dd32162dSLiad Kaufman 
3859dd32162dSLiad Kaufman u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
3860dd32162dSLiad Kaufman {
3861dd32162dSLiad Kaufman 	u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3862dd32162dSLiad Kaufman 
3863dd32162dSLiad Kaufman 	/*
38642f7a3863SLuca Coelho 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
3865dd32162dSLiad Kaufman 	 * to align the wrap around of ssn so we compare relevant values.
3866dd32162dSLiad Kaufman 	 */
3867dd32162dSLiad Kaufman 	if (mvm->trans->cfg->gen2)
3868dd32162dSLiad Kaufman 		sn &= 0xff;
3869dd32162dSLiad Kaufman 
3870dd32162dSLiad Kaufman 	return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
3871dd32162dSLiad Kaufman }
3872