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 {
7499a16ee0cSShaul Triebitz 	int queue, size = max_t(u32, IWL_DEFAULT_QUEUE_SIZE,
7509a16ee0cSShaul Triebitz 				mvm->trans->cfg->min_256_ba_txq_size);
75199448a8cSJohannes Berg 
75299448a8cSJohannes Berg 	if (tid == IWL_MAX_TID_COUNT) {
75399448a8cSJohannes Berg 		tid = IWL_MGMT_TID;
754ff911dcaSShaul Triebitz 		size = max_t(u32, IWL_MGMT_QUEUE_SIZE,
755ff911dcaSShaul Triebitz 			     mvm->trans->cfg->min_txq_size);
75699448a8cSJohannes Berg 	}
75799448a8cSJohannes Berg 	queue = iwl_trans_txq_alloc(mvm->trans,
75899448a8cSJohannes Berg 				    cpu_to_le16(TX_QUEUE_CFG_ENABLE_QUEUE),
75999448a8cSJohannes Berg 				    sta_id, tid, SCD_QUEUE_CFG, size, timeout);
76099448a8cSJohannes Berg 
76199448a8cSJohannes Berg 	if (queue < 0) {
76299448a8cSJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm,
76399448a8cSJohannes Berg 				    "Failed allocating TXQ for sta %d tid %d, ret: %d\n",
76499448a8cSJohannes Berg 				    sta_id, tid, queue);
76599448a8cSJohannes Berg 		return queue;
76699448a8cSJohannes Berg 	}
76799448a8cSJohannes Berg 
76899448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d for sta %d tid %d\n",
76999448a8cSJohannes Berg 			    queue, sta_id, tid);
77099448a8cSJohannes Berg 
771cfbc6c4cSSara Sharon 	IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d\n", queue);
77299448a8cSJohannes Berg 
77399448a8cSJohannes Berg 	return queue;
77499448a8cSJohannes Berg }
77599448a8cSJohannes Berg 
776310181ecSSara Sharon static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
777310181ecSSara Sharon 					struct ieee80211_sta *sta, u8 ac,
778310181ecSSara Sharon 					int tid)
779310181ecSSara Sharon {
780310181ecSSara Sharon 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
781cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq =
782cfbc6c4cSSara Sharon 		iwl_mvm_txq_from_tid(sta, tid);
783310181ecSSara Sharon 	unsigned int wdg_timeout =
784310181ecSSara Sharon 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
785310181ecSSara Sharon 	int queue = -1;
786310181ecSSara Sharon 
787310181ecSSara Sharon 	lockdep_assert_held(&mvm->mutex);
788310181ecSSara Sharon 
789310181ecSSara Sharon 	IWL_DEBUG_TX_QUEUES(mvm,
790310181ecSSara Sharon 			    "Allocating queue for sta %d on tid %d\n",
791310181ecSSara Sharon 			    mvmsta->sta_id, tid);
792cfbc6c4cSSara Sharon 	queue = iwl_mvm_tvqm_enable_txq(mvm, mvmsta->sta_id, tid, wdg_timeout);
793310181ecSSara Sharon 	if (queue < 0)
794310181ecSSara Sharon 		return queue;
795310181ecSSara Sharon 
796cfbc6c4cSSara Sharon 	mvmtxq->txq_id = queue;
797cfbc6c4cSSara Sharon 	mvm->tvqm_info[queue].txq_tid = tid;
798cfbc6c4cSSara Sharon 	mvm->tvqm_info[queue].sta_id = mvmsta->sta_id;
799cfbc6c4cSSara Sharon 
800310181ecSSara Sharon 	IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
801310181ecSSara Sharon 
802310181ecSSara Sharon 	spin_lock_bh(&mvmsta->lock);
803310181ecSSara Sharon 	mvmsta->tid_data[tid].txq_id = queue;
804310181ecSSara Sharon 	spin_unlock_bh(&mvmsta->lock);
805310181ecSSara Sharon 
806310181ecSSara Sharon 	return 0;
807310181ecSSara Sharon }
808310181ecSSara Sharon 
809cfbc6c4cSSara Sharon static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm,
810cfbc6c4cSSara Sharon 				       struct ieee80211_sta *sta,
811cfbc6c4cSSara Sharon 				       int queue, u8 sta_id, u8 tid)
81299448a8cSJohannes Berg {
81399448a8cSJohannes Berg 	bool enable_queue = true;
81499448a8cSJohannes Berg 
81599448a8cSJohannes Berg 	/* Make sure this TID isn't already enabled */
81699448a8cSJohannes Berg 	if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) {
81799448a8cSJohannes Berg 		IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n",
81899448a8cSJohannes Berg 			queue, tid);
81999448a8cSJohannes Berg 		return false;
82099448a8cSJohannes Berg 	}
82199448a8cSJohannes Berg 
82299448a8cSJohannes Berg 	/* Update mappings and refcounts */
8231c14089eSJohannes Berg 	if (mvm->queue_info[queue].tid_bitmap)
82499448a8cSJohannes Berg 		enable_queue = false;
82599448a8cSJohannes Berg 
82699448a8cSJohannes Berg 	mvm->queue_info[queue].tid_bitmap |= BIT(tid);
82799448a8cSJohannes Berg 	mvm->queue_info[queue].ra_sta_id = sta_id;
82899448a8cSJohannes Berg 
82999448a8cSJohannes Berg 	if (enable_queue) {
83099448a8cSJohannes Berg 		if (tid != IWL_MAX_TID_COUNT)
83199448a8cSJohannes Berg 			mvm->queue_info[queue].mac80211_ac =
83299448a8cSJohannes Berg 				tid_to_mac80211_ac[tid];
83399448a8cSJohannes Berg 		else
83499448a8cSJohannes Berg 			mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO;
83599448a8cSJohannes Berg 
83699448a8cSJohannes Berg 		mvm->queue_info[queue].txq_tid = tid;
83799448a8cSJohannes Berg 	}
83899448a8cSJohannes Berg 
839cfbc6c4cSSara Sharon 	if (sta) {
840cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
841cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_tid(sta, tid);
842cfbc6c4cSSara Sharon 
843cfbc6c4cSSara Sharon 		mvmtxq->txq_id = queue;
844cfbc6c4cSSara Sharon 	}
845cfbc6c4cSSara Sharon 
84699448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
847cfbc6c4cSSara Sharon 			    "Enabling TXQ #%d tids=0x%x\n",
848cfbc6c4cSSara Sharon 			    queue, mvm->queue_info[queue].tid_bitmap);
84999448a8cSJohannes Berg 
85099448a8cSJohannes Berg 	return enable_queue;
85199448a8cSJohannes Berg }
85299448a8cSJohannes Berg 
853cfbc6c4cSSara Sharon static bool iwl_mvm_enable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
854cfbc6c4cSSara Sharon 			       int queue, u16 ssn,
85599448a8cSJohannes Berg 			       const struct iwl_trans_txq_scd_cfg *cfg,
85699448a8cSJohannes Berg 			       unsigned int wdg_timeout)
85799448a8cSJohannes Berg {
85899448a8cSJohannes Berg 	struct iwl_scd_txq_cfg_cmd cmd = {
85999448a8cSJohannes Berg 		.scd_queue = queue,
86099448a8cSJohannes Berg 		.action = SCD_CFG_ENABLE_QUEUE,
86199448a8cSJohannes Berg 		.window = cfg->frame_limit,
86299448a8cSJohannes Berg 		.sta_id = cfg->sta_id,
86399448a8cSJohannes Berg 		.ssn = cpu_to_le16(ssn),
86499448a8cSJohannes Berg 		.tx_fifo = cfg->fifo,
86599448a8cSJohannes Berg 		.aggregate = cfg->aggregate,
86699448a8cSJohannes Berg 		.tid = cfg->tid,
86799448a8cSJohannes Berg 	};
86899448a8cSJohannes Berg 	bool inc_ssn;
86999448a8cSJohannes Berg 
87099448a8cSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
87199448a8cSJohannes Berg 		return false;
87299448a8cSJohannes Berg 
87399448a8cSJohannes Berg 	/* Send the enabling command if we need to */
874cfbc6c4cSSara Sharon 	if (!iwl_mvm_update_txq_mapping(mvm, sta, queue, cfg->sta_id, cfg->tid))
87599448a8cSJohannes Berg 		return false;
87699448a8cSJohannes Berg 
87799448a8cSJohannes Berg 	inc_ssn = iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn,
87899448a8cSJohannes Berg 					   NULL, wdg_timeout);
87999448a8cSJohannes Berg 	if (inc_ssn)
88099448a8cSJohannes Berg 		le16_add_cpu(&cmd.ssn, 1);
88199448a8cSJohannes Berg 
88299448a8cSJohannes Berg 	WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd),
88399448a8cSJohannes Berg 	     "Failed to configure queue %d on FIFO %d\n", queue, cfg->fifo);
88499448a8cSJohannes Berg 
88599448a8cSJohannes Berg 	return inc_ssn;
88699448a8cSJohannes Berg }
88799448a8cSJohannes Berg 
888b3a87f11SJohannes Berg static void iwl_mvm_change_queue_tid(struct iwl_mvm *mvm, int queue)
88919aefa45SLiad Kaufman {
89019aefa45SLiad Kaufman 	struct iwl_scd_txq_cfg_cmd cmd = {
89119aefa45SLiad Kaufman 		.scd_queue = queue,
89219aefa45SLiad Kaufman 		.action = SCD_CFG_UPDATE_QUEUE_TID,
89319aefa45SLiad Kaufman 	};
89419aefa45SLiad Kaufman 	int tid;
89519aefa45SLiad Kaufman 	unsigned long tid_bitmap;
89619aefa45SLiad Kaufman 	int ret;
89719aefa45SLiad Kaufman 
89819aefa45SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
89919aefa45SLiad Kaufman 
900bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
901bb49701bSSara Sharon 		return;
902bb49701bSSara Sharon 
90319aefa45SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
90419aefa45SLiad Kaufman 
90519aefa45SLiad Kaufman 	if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
90619aefa45SLiad Kaufman 		return;
90719aefa45SLiad Kaufman 
90819aefa45SLiad Kaufman 	/* Find any TID for queue */
90919aefa45SLiad Kaufman 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
91019aefa45SLiad Kaufman 	cmd.tid = tid;
91119aefa45SLiad Kaufman 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
91219aefa45SLiad Kaufman 
91319aefa45SLiad Kaufman 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
914341ca402SLiad Kaufman 	if (ret) {
91519aefa45SLiad Kaufman 		IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
91619aefa45SLiad Kaufman 			queue, ret);
917341ca402SLiad Kaufman 		return;
918341ca402SLiad Kaufman 	}
919341ca402SLiad Kaufman 
920341ca402SLiad Kaufman 	mvm->queue_info[queue].txq_tid = tid;
92119aefa45SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
92219aefa45SLiad Kaufman 			    queue, tid);
92319aefa45SLiad Kaufman }
92419aefa45SLiad Kaufman 
9259f9af3d7SLiad Kaufman static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
9269f9af3d7SLiad Kaufman {
9279f9af3d7SLiad Kaufman 	struct ieee80211_sta *sta;
9289f9af3d7SLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
929806911daSSharon Dvir 	u8 sta_id;
9309f9af3d7SLiad Kaufman 	int tid = -1;
9319f9af3d7SLiad Kaufman 	unsigned long tid_bitmap;
9329f9af3d7SLiad Kaufman 	unsigned int wdg_timeout;
9339f9af3d7SLiad Kaufman 	int ssn;
9349f9af3d7SLiad Kaufman 	int ret = true;
9359f9af3d7SLiad Kaufman 
936bb49701bSSara Sharon 	/* queue sharing is disabled on new TX path */
937bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
938bb49701bSSara Sharon 		return;
939bb49701bSSara Sharon 
9409f9af3d7SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
9419f9af3d7SLiad Kaufman 
9429f9af3d7SLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
9439f9af3d7SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
9449f9af3d7SLiad Kaufman 
9459f9af3d7SLiad Kaufman 	/* Find TID for queue, and make sure it is the only one on the queue */
9469f9af3d7SLiad Kaufman 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
9479f9af3d7SLiad Kaufman 	if (tid_bitmap != BIT(tid)) {
9489f9af3d7SLiad Kaufman 		IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
9499f9af3d7SLiad Kaufman 			queue, tid_bitmap);
9509f9af3d7SLiad Kaufman 		return;
9519f9af3d7SLiad Kaufman 	}
9529f9af3d7SLiad Kaufman 
9539f9af3d7SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
9549f9af3d7SLiad Kaufman 			    tid);
9559f9af3d7SLiad Kaufman 
9569f9af3d7SLiad Kaufman 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
9579f9af3d7SLiad Kaufman 					lockdep_is_held(&mvm->mutex));
9589f9af3d7SLiad Kaufman 
9599f9af3d7SLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
9609f9af3d7SLiad Kaufman 		return;
9619f9af3d7SLiad Kaufman 
9629f9af3d7SLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
9639f9af3d7SLiad Kaufman 	wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
9649f9af3d7SLiad Kaufman 
9659f9af3d7SLiad Kaufman 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
9669f9af3d7SLiad Kaufman 
967cfbc6c4cSSara Sharon 	ret = iwl_mvm_redirect_queue(mvm, queue, tid,
9689f9af3d7SLiad Kaufman 				     tid_to_mac80211_ac[tid], ssn,
969cfbc6c4cSSara Sharon 				     wdg_timeout, true,
970cfbc6c4cSSara Sharon 				     iwl_mvm_txq_from_tid(sta, tid));
9719f9af3d7SLiad Kaufman 	if (ret) {
9729f9af3d7SLiad Kaufman 		IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
9739f9af3d7SLiad Kaufman 		return;
9749f9af3d7SLiad Kaufman 	}
9759f9af3d7SLiad Kaufman 
9769f9af3d7SLiad Kaufman 	/* If aggs should be turned back on - do it */
9779f9af3d7SLiad Kaufman 	if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
9789cd70e80SEmmanuel Grumbach 		struct iwl_mvm_add_sta_cmd cmd = {0};
9799f9af3d7SLiad Kaufman 
9809f9af3d7SLiad Kaufman 		mvmsta->tid_disable_agg &= ~BIT(tid);
9819f9af3d7SLiad Kaufman 
9829f9af3d7SLiad Kaufman 		cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
9839f9af3d7SLiad Kaufman 		cmd.sta_id = mvmsta->sta_id;
9849f9af3d7SLiad Kaufman 		cmd.add_modify = STA_MODE_MODIFY;
9859f9af3d7SLiad Kaufman 		cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
9869f9af3d7SLiad Kaufman 		cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
9879f9af3d7SLiad Kaufman 		cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
9889f9af3d7SLiad Kaufman 
9899f9af3d7SLiad Kaufman 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
9909f9af3d7SLiad Kaufman 					   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
9919f9af3d7SLiad Kaufman 		if (!ret) {
9929f9af3d7SLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm,
9939f9af3d7SLiad Kaufman 					    "TXQ #%d is now aggregated again\n",
9949f9af3d7SLiad Kaufman 					    queue);
9959f9af3d7SLiad Kaufman 
9969f9af3d7SLiad Kaufman 			/* Mark queue intenally as aggregating again */
9979f9af3d7SLiad Kaufman 			iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
9989f9af3d7SLiad Kaufman 		}
9999f9af3d7SLiad Kaufman 	}
10009f9af3d7SLiad Kaufman 
10019f9af3d7SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
10029f9af3d7SLiad Kaufman }
10039f9af3d7SLiad Kaufman 
100499448a8cSJohannes Berg /*
100599448a8cSJohannes Berg  * Remove inactive TIDs of a given queue.
100699448a8cSJohannes Berg  * If all queue TIDs are inactive - mark the queue as inactive
100799448a8cSJohannes Berg  * If only some the queue TIDs are inactive - unmap them from the queue
1008724fe771SJohannes Berg  *
1009724fe771SJohannes Berg  * Returns %true if all TIDs were removed and the queue could be reused.
101099448a8cSJohannes Berg  */
1011724fe771SJohannes Berg static bool iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
101299448a8cSJohannes Berg 					 struct iwl_mvm_sta *mvmsta, int queue,
101390d2d94cSJohannes Berg 					 unsigned long tid_bitmap,
1014b3a87f11SJohannes Berg 					 unsigned long *unshare_queues,
1015b3a87f11SJohannes Berg 					 unsigned long *changetid_queues)
101699448a8cSJohannes Berg {
101799448a8cSJohannes Berg 	int tid;
101899448a8cSJohannes Berg 
101999448a8cSJohannes Berg 	lockdep_assert_held(&mvmsta->lock);
1020f3f240f9SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
102199448a8cSJohannes Berg 
102299448a8cSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1023724fe771SJohannes Berg 		return false;
102499448a8cSJohannes Berg 
102599448a8cSJohannes Berg 	/* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
102699448a8cSJohannes Berg 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
102799448a8cSJohannes Berg 		/* If some TFDs are still queued - don't mark TID as inactive */
102899448a8cSJohannes Berg 		if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid]))
102999448a8cSJohannes Berg 			tid_bitmap &= ~BIT(tid);
103099448a8cSJohannes Berg 
103199448a8cSJohannes Berg 		/* Don't mark as inactive any TID that has an active BA */
103299448a8cSJohannes Berg 		if (mvmsta->tid_data[tid].state != IWL_AGG_OFF)
103399448a8cSJohannes Berg 			tid_bitmap &= ~BIT(tid);
103499448a8cSJohannes Berg 	}
103599448a8cSJohannes Berg 
1036724fe771SJohannes Berg 	/* If all TIDs in the queue are inactive - return it can be reused */
103799448a8cSJohannes Berg 	if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
1038724fe771SJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Queue %d is inactive\n", queue);
1039724fe771SJohannes Berg 		return true;
104099448a8cSJohannes Berg 	}
104199448a8cSJohannes Berg 
104299448a8cSJohannes Berg 	/*
104399448a8cSJohannes Berg 	 * If we are here, this is a shared queue and not all TIDs timed-out.
104499448a8cSJohannes Berg 	 * Remove the ones that did.
104599448a8cSJohannes Berg 	 */
104699448a8cSJohannes Berg 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1047b3a87f11SJohannes Berg 		u16 tid_bitmap;
104899448a8cSJohannes Berg 
104999448a8cSJohannes Berg 		mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
105099448a8cSJohannes Berg 		mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
105199448a8cSJohannes Berg 
1052b3a87f11SJohannes Berg 		tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1053b3a87f11SJohannes Berg 
1054b3a87f11SJohannes Berg 		/*
1055b3a87f11SJohannes Berg 		 * We need to take into account a situation in which a TXQ was
1056b3a87f11SJohannes Berg 		 * allocated to TID x, and then turned shared by adding TIDs y
1057b3a87f11SJohannes Berg 		 * and z. If TID x becomes inactive and is removed from the TXQ,
1058b3a87f11SJohannes Berg 		 * ownership must be given to one of the remaining TIDs.
1059b3a87f11SJohannes Berg 		 * This is mainly because if TID x continues - a new queue can't
1060b3a87f11SJohannes Berg 		 * be allocated for it as long as it is an owner of another TXQ.
1061b3a87f11SJohannes Berg 		 *
1062b3a87f11SJohannes Berg 		 * Mark this queue in the right bitmap, we'll send the command
1063b3a87f11SJohannes Berg 		 * to the firmware later.
1064b3a87f11SJohannes Berg 		 */
1065b3a87f11SJohannes Berg 		if (!(tid_bitmap & BIT(mvm->queue_info[queue].txq_tid)))
1066b3a87f11SJohannes Berg 			set_bit(queue, changetid_queues);
1067b3a87f11SJohannes Berg 
106899448a8cSJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm,
106999448a8cSJohannes Berg 				    "Removing inactive TID %d from shared Q:%d\n",
107099448a8cSJohannes Berg 				    tid, queue);
107199448a8cSJohannes Berg 	}
107299448a8cSJohannes Berg 
107399448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
107499448a8cSJohannes Berg 			    "TXQ #%d left with tid bitmap 0x%x\n", queue,
107599448a8cSJohannes Berg 			    mvm->queue_info[queue].tid_bitmap);
107699448a8cSJohannes Berg 
107799448a8cSJohannes Berg 	/*
107899448a8cSJohannes Berg 	 * There may be different TIDs with the same mac queues, so make
107999448a8cSJohannes Berg 	 * sure all TIDs have existing corresponding mac queues enabled
108099448a8cSJohannes Berg 	 */
108199448a8cSJohannes Berg 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
108299448a8cSJohannes Berg 
108399448a8cSJohannes Berg 	/* If the queue is marked as shared - "unshare" it */
10841c14089eSJohannes Berg 	if (hweight16(mvm->queue_info[queue].tid_bitmap) == 1 &&
108599448a8cSJohannes Berg 	    mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
108699448a8cSJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
108799448a8cSJohannes Berg 				    queue);
108890d2d94cSJohannes Berg 		set_bit(queue, unshare_queues);
108999448a8cSJohannes Berg 	}
1090724fe771SJohannes Berg 
1091724fe771SJohannes Berg 	return false;
109299448a8cSJohannes Berg }
109399448a8cSJohannes Berg 
1094724fe771SJohannes Berg /*
1095724fe771SJohannes Berg  * Check for inactivity - this includes checking if any queue
1096724fe771SJohannes Berg  * can be unshared and finding one (and only one) that can be
1097724fe771SJohannes Berg  * reused.
1098724fe771SJohannes Berg  * This function is also invoked as a sort of clean-up task,
1099724fe771SJohannes Berg  * in which case @alloc_for_sta is IWL_MVM_INVALID_STA.
1100724fe771SJohannes Berg  *
1101724fe771SJohannes Berg  * Returns the queue number, or -ENOSPC.
1102724fe771SJohannes Berg  */
1103724fe771SJohannes Berg static int iwl_mvm_inactivity_check(struct iwl_mvm *mvm, u8 alloc_for_sta)
110499448a8cSJohannes Berg {
110599448a8cSJohannes Berg 	unsigned long now = jiffies;
110690d2d94cSJohannes Berg 	unsigned long unshare_queues = 0;
1107b3a87f11SJohannes Berg 	unsigned long changetid_queues = 0;
1108724fe771SJohannes Berg 	int i, ret, free_queue = -ENOSPC;
1109cfbc6c4cSSara Sharon 	struct ieee80211_sta *queue_owner  = NULL;
111099448a8cSJohannes Berg 
1111df2a2245SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
1112df2a2245SJohannes Berg 
111399448a8cSJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm))
1114724fe771SJohannes Berg 		return -ENOSPC;
111599448a8cSJohannes Berg 
111699448a8cSJohannes Berg 	rcu_read_lock();
111799448a8cSJohannes Berg 
1118459ab045SJohannes Berg 	/* we skip the CMD queue below by starting at 1 */
1119459ab045SJohannes Berg 	BUILD_BUG_ON(IWL_MVM_DQA_CMD_QUEUE != 0);
1120459ab045SJohannes Berg 
1121459ab045SJohannes Berg 	for (i = 1; i < IWL_MAX_HW_QUEUES; i++) {
112299448a8cSJohannes Berg 		struct ieee80211_sta *sta;
112399448a8cSJohannes Berg 		struct iwl_mvm_sta *mvmsta;
112499448a8cSJohannes Berg 		u8 sta_id;
112599448a8cSJohannes Berg 		int tid;
112699448a8cSJohannes Berg 		unsigned long inactive_tid_bitmap = 0;
112799448a8cSJohannes Berg 		unsigned long queue_tid_bitmap;
112899448a8cSJohannes Berg 
112999448a8cSJohannes Berg 		queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
1130459ab045SJohannes Berg 		if (!queue_tid_bitmap)
1131459ab045SJohannes Berg 			continue;
113299448a8cSJohannes Berg 
113399448a8cSJohannes Berg 		/* If TXQ isn't in active use anyway - nothing to do here... */
113499448a8cSJohannes Berg 		if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
1135459ab045SJohannes Berg 		    mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED)
113699448a8cSJohannes Berg 			continue;
113799448a8cSJohannes Berg 
113899448a8cSJohannes Berg 		/* Check to see if there are inactive TIDs on this queue */
113999448a8cSJohannes Berg 		for_each_set_bit(tid, &queue_tid_bitmap,
114099448a8cSJohannes Berg 				 IWL_MAX_TID_COUNT + 1) {
114199448a8cSJohannes Berg 			if (time_after(mvm->queue_info[i].last_frame_time[tid] +
114299448a8cSJohannes Berg 				       IWL_MVM_DQA_QUEUE_TIMEOUT, now))
114399448a8cSJohannes Berg 				continue;
114499448a8cSJohannes Berg 
114599448a8cSJohannes Berg 			inactive_tid_bitmap |= BIT(tid);
114699448a8cSJohannes Berg 		}
114799448a8cSJohannes Berg 
114899448a8cSJohannes Berg 		/* If all TIDs are active - finish check on this queue */
114999448a8cSJohannes Berg 		if (!inactive_tid_bitmap)
115099448a8cSJohannes Berg 			continue;
115199448a8cSJohannes Berg 
115299448a8cSJohannes Berg 		/*
115399448a8cSJohannes Berg 		 * If we are here - the queue hadn't been served recently and is
115499448a8cSJohannes Berg 		 * in use
115599448a8cSJohannes Berg 		 */
115699448a8cSJohannes Berg 
115799448a8cSJohannes Berg 		sta_id = mvm->queue_info[i].ra_sta_id;
115899448a8cSJohannes Berg 		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
115999448a8cSJohannes Berg 
116099448a8cSJohannes Berg 		/*
116199448a8cSJohannes Berg 		 * If the STA doesn't exist anymore, it isn't an error. It could
116299448a8cSJohannes Berg 		 * be that it was removed since getting the queues, and in this
116399448a8cSJohannes Berg 		 * case it should've inactivated its queues anyway.
116499448a8cSJohannes Berg 		 */
116599448a8cSJohannes Berg 		if (IS_ERR_OR_NULL(sta))
116699448a8cSJohannes Berg 			continue;
116799448a8cSJohannes Berg 
116899448a8cSJohannes Berg 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
116999448a8cSJohannes Berg 
1170f3f240f9SJohannes Berg 		spin_lock_bh(&mvmsta->lock);
1171724fe771SJohannes Berg 		ret = iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
117290d2d94cSJohannes Berg 						   inactive_tid_bitmap,
1173b3a87f11SJohannes Berg 						   &unshare_queues,
1174b3a87f11SJohannes Berg 						   &changetid_queues);
1175cfbc6c4cSSara Sharon 		if (ret >= 0 && free_queue < 0) {
1176cfbc6c4cSSara Sharon 			queue_owner = sta;
1177724fe771SJohannes Berg 			free_queue = ret;
1178cfbc6c4cSSara Sharon 		}
1179459ab045SJohannes Berg 		/* only unlock sta lock - we still need the queue info lock */
1180f3f240f9SJohannes Berg 		spin_unlock_bh(&mvmsta->lock);
118199448a8cSJohannes Berg 	}
118299448a8cSJohannes Berg 
1183df2a2245SJohannes Berg 
1184df2a2245SJohannes Berg 	/* Reconfigure queues requiring reconfiguation */
118590d2d94cSJohannes Berg 	for_each_set_bit(i, &unshare_queues, IWL_MAX_HW_QUEUES)
118690d2d94cSJohannes Berg 		iwl_mvm_unshare_queue(mvm, i);
1187b3a87f11SJohannes Berg 	for_each_set_bit(i, &changetid_queues, IWL_MAX_HW_QUEUES)
1188b3a87f11SJohannes Berg 		iwl_mvm_change_queue_tid(mvm, i);
1189724fe771SJohannes Berg 
1190724fe771SJohannes Berg 	if (free_queue >= 0 && alloc_for_sta != IWL_MVM_INVALID_STA) {
1191cfbc6c4cSSara Sharon 		ret = iwl_mvm_free_inactive_queue(mvm, free_queue, queue_owner,
1192724fe771SJohannes Berg 						  alloc_for_sta);
1193cfbc6c4cSSara Sharon 		if (ret) {
1194cfbc6c4cSSara Sharon 			rcu_read_unlock();
1195724fe771SJohannes Berg 			return ret;
1196724fe771SJohannes Berg 		}
1197cfbc6c4cSSara Sharon 	}
1198cfbc6c4cSSara Sharon 
1199cfbc6c4cSSara Sharon 	rcu_read_unlock();
1200724fe771SJohannes Berg 
1201724fe771SJohannes Berg 	return free_queue;
120299448a8cSJohannes Berg }
120399448a8cSJohannes Berg 
1204c20e08b0SJohannes Berg static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
1205cfbc6c4cSSara Sharon 				   struct ieee80211_sta *sta, u8 ac, int tid)
1206c20e08b0SJohannes Berg {
1207c20e08b0SJohannes Berg 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1208c20e08b0SJohannes Berg 	struct iwl_trans_txq_scd_cfg cfg = {
1209c20e08b0SJohannes Berg 		.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
1210c20e08b0SJohannes Berg 		.sta_id = mvmsta->sta_id,
1211c20e08b0SJohannes Berg 		.tid = tid,
1212c20e08b0SJohannes Berg 		.frame_limit = IWL_FRAME_LIMIT,
1213c20e08b0SJohannes Berg 	};
1214c20e08b0SJohannes Berg 	unsigned int wdg_timeout =
1215c20e08b0SJohannes Berg 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1216c20e08b0SJohannes Berg 	int queue = -1;
1217c20e08b0SJohannes Berg 	unsigned long disable_agg_tids = 0;
1218c20e08b0SJohannes Berg 	enum iwl_mvm_agg_state queue_state;
1219c20e08b0SJohannes Berg 	bool shared_queue = false, inc_ssn;
1220c20e08b0SJohannes Berg 	int ssn;
1221c20e08b0SJohannes Berg 	unsigned long tfd_queue_mask;
1222c20e08b0SJohannes Berg 	int ret;
1223c20e08b0SJohannes Berg 
1224c20e08b0SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
1225c20e08b0SJohannes Berg 
1226c20e08b0SJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm))
1227c20e08b0SJohannes Berg 		return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
1228c20e08b0SJohannes Berg 
1229c20e08b0SJohannes Berg 	spin_lock_bh(&mvmsta->lock);
1230c20e08b0SJohannes Berg 	tfd_queue_mask = mvmsta->tfd_queue_msk;
123135739348SSara Sharon 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1232c20e08b0SJohannes Berg 	spin_unlock_bh(&mvmsta->lock);
1233c20e08b0SJohannes Berg 
1234cfbc6c4cSSara Sharon 	if (tid == IWL_MAX_TID_COUNT) {
1235c20e08b0SJohannes Berg 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1236c20e08b0SJohannes Berg 						IWL_MVM_DQA_MIN_MGMT_QUEUE,
1237c20e08b0SJohannes Berg 						IWL_MVM_DQA_MAX_MGMT_QUEUE);
1238c20e08b0SJohannes Berg 		if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
1239c20e08b0SJohannes Berg 			IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
1240c20e08b0SJohannes Berg 					    queue);
1241c20e08b0SJohannes Berg 
1242c20e08b0SJohannes Berg 		/* If no such queue is found, we'll use a DATA queue instead */
1243c20e08b0SJohannes Berg 	}
1244c20e08b0SJohannes Berg 
1245c20e08b0SJohannes Berg 	if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
1246c20e08b0SJohannes Berg 	    (mvm->queue_info[mvmsta->reserved_queue].status ==
1247724fe771SJohannes Berg 			IWL_MVM_QUEUE_RESERVED)) {
1248c20e08b0SJohannes Berg 		queue = mvmsta->reserved_queue;
1249c20e08b0SJohannes Berg 		mvm->queue_info[queue].reserved = true;
1250c20e08b0SJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
1251c20e08b0SJohannes Berg 	}
1252c20e08b0SJohannes Berg 
1253c20e08b0SJohannes Berg 	if (queue < 0)
1254c20e08b0SJohannes Berg 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1255c20e08b0SJohannes Berg 						IWL_MVM_DQA_MIN_DATA_QUEUE,
1256c20e08b0SJohannes Berg 						IWL_MVM_DQA_MAX_DATA_QUEUE);
1257724fe771SJohannes Berg 	if (queue < 0) {
1258724fe771SJohannes Berg 		/* try harder - perhaps kill an inactive queue */
1259724fe771SJohannes Berg 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1260c20e08b0SJohannes Berg 	}
1261c20e08b0SJohannes Berg 
1262c20e08b0SJohannes Berg 	/* No free queue - we'll have to share */
1263c20e08b0SJohannes Berg 	if (queue <= 0) {
1264c20e08b0SJohannes Berg 		queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
1265c20e08b0SJohannes Berg 		if (queue > 0) {
1266c20e08b0SJohannes Berg 			shared_queue = true;
1267c20e08b0SJohannes Berg 			mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
1268c20e08b0SJohannes Berg 		}
1269c20e08b0SJohannes Berg 	}
1270c20e08b0SJohannes Berg 
1271c20e08b0SJohannes Berg 	/*
1272c20e08b0SJohannes Berg 	 * Mark TXQ as ready, even though it hasn't been fully configured yet,
1273c20e08b0SJohannes Berg 	 * to make sure no one else takes it.
1274c20e08b0SJohannes Berg 	 * This will allow avoiding re-acquiring the lock at the end of the
1275c20e08b0SJohannes Berg 	 * configuration. On error we'll mark it back as free.
1276c20e08b0SJohannes Berg 	 */
1277c20e08b0SJohannes Berg 	if (queue > 0 && !shared_queue)
1278c20e08b0SJohannes Berg 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1279c20e08b0SJohannes Berg 
1280c20e08b0SJohannes Berg 	/* This shouldn't happen - out of queues */
1281c20e08b0SJohannes Berg 	if (WARN_ON(queue <= 0)) {
1282c20e08b0SJohannes Berg 		IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
1283c20e08b0SJohannes Berg 			tid, cfg.sta_id);
1284c20e08b0SJohannes Berg 		return queue;
1285c20e08b0SJohannes Berg 	}
1286c20e08b0SJohannes Berg 
1287c20e08b0SJohannes Berg 	/*
1288c20e08b0SJohannes Berg 	 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
1289c20e08b0SJohannes Berg 	 * but for configuring the SCD to send A-MPDUs we need to mark the queue
1290c20e08b0SJohannes Berg 	 * as aggregatable.
1291c20e08b0SJohannes Berg 	 * Mark all DATA queues as allowing to be aggregated at some point
1292c20e08b0SJohannes Berg 	 */
1293c20e08b0SJohannes Berg 	cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1294c20e08b0SJohannes Berg 			 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1295c20e08b0SJohannes Berg 
1296c20e08b0SJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
1297c20e08b0SJohannes Berg 			    "Allocating %squeue #%d to sta %d on tid %d\n",
1298c20e08b0SJohannes Berg 			    shared_queue ? "shared " : "", queue,
1299c20e08b0SJohannes Berg 			    mvmsta->sta_id, tid);
1300c20e08b0SJohannes Berg 
1301c20e08b0SJohannes Berg 	if (shared_queue) {
1302c20e08b0SJohannes Berg 		/* Disable any open aggs on this queue */
1303c20e08b0SJohannes Berg 		disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
1304c20e08b0SJohannes Berg 
1305c20e08b0SJohannes Berg 		if (disable_agg_tids) {
1306c20e08b0SJohannes Berg 			IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
1307c20e08b0SJohannes Berg 					    queue);
1308c20e08b0SJohannes Berg 			iwl_mvm_invalidate_sta_queue(mvm, queue,
1309c20e08b0SJohannes Berg 						     disable_agg_tids, false);
1310c20e08b0SJohannes Berg 		}
1311c20e08b0SJohannes Berg 	}
1312c20e08b0SJohannes Berg 
1313cfbc6c4cSSara Sharon 	inc_ssn = iwl_mvm_enable_txq(mvm, sta, queue, ssn, &cfg, wdg_timeout);
1314c20e08b0SJohannes Berg 
1315c20e08b0SJohannes Berg 	/*
1316c20e08b0SJohannes Berg 	 * Mark queue as shared in transport if shared
1317c20e08b0SJohannes Berg 	 * Note this has to be done after queue enablement because enablement
1318c20e08b0SJohannes Berg 	 * can also set this value, and there is no indication there to shared
1319c20e08b0SJohannes Berg 	 * queues
1320c20e08b0SJohannes Berg 	 */
1321c20e08b0SJohannes Berg 	if (shared_queue)
1322c20e08b0SJohannes Berg 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
1323c20e08b0SJohannes Berg 
1324c20e08b0SJohannes Berg 	spin_lock_bh(&mvmsta->lock);
1325c20e08b0SJohannes Berg 	/*
1326c20e08b0SJohannes Berg 	 * This looks racy, but it is not. We have only one packet for
1327c20e08b0SJohannes Berg 	 * this ra/tid in our Tx path since we stop the Qdisc when we
1328c20e08b0SJohannes Berg 	 * need to allocate a new TFD queue.
1329c20e08b0SJohannes Berg 	 */
133035739348SSara Sharon 	if (inc_ssn) {
1331c20e08b0SJohannes Berg 		mvmsta->tid_data[tid].seq_number += 0x10;
133235739348SSara Sharon 		ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
133335739348SSara Sharon 	}
1334c20e08b0SJohannes Berg 	mvmsta->tid_data[tid].txq_id = queue;
1335c20e08b0SJohannes Berg 	mvmsta->tfd_queue_msk |= BIT(queue);
1336c20e08b0SJohannes Berg 	queue_state = mvmsta->tid_data[tid].state;
1337c20e08b0SJohannes Berg 
1338c20e08b0SJohannes Berg 	if (mvmsta->reserved_queue == queue)
1339c20e08b0SJohannes Berg 		mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
1340c20e08b0SJohannes Berg 	spin_unlock_bh(&mvmsta->lock);
1341c20e08b0SJohannes Berg 
1342c20e08b0SJohannes Berg 	if (!shared_queue) {
1343c20e08b0SJohannes Berg 		ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
1344c20e08b0SJohannes Berg 		if (ret)
1345c20e08b0SJohannes Berg 			goto out_err;
1346c20e08b0SJohannes Berg 
1347c20e08b0SJohannes Berg 		/* If we need to re-enable aggregations... */
1348c20e08b0SJohannes Berg 		if (queue_state == IWL_AGG_ON) {
1349c20e08b0SJohannes Berg 			ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1350c20e08b0SJohannes Berg 			if (ret)
1351c20e08b0SJohannes Berg 				goto out_err;
1352c20e08b0SJohannes Berg 		}
1353c20e08b0SJohannes Berg 	} else {
1354c20e08b0SJohannes Berg 		/* Redirect queue, if needed */
1355cfbc6c4cSSara Sharon 		ret = iwl_mvm_redirect_queue(mvm, queue, tid, ac, ssn,
1356cfbc6c4cSSara Sharon 					     wdg_timeout, false,
1357cfbc6c4cSSara Sharon 					     iwl_mvm_txq_from_tid(sta, tid));
1358c20e08b0SJohannes Berg 		if (ret)
1359c20e08b0SJohannes Berg 			goto out_err;
1360c20e08b0SJohannes Berg 	}
1361c20e08b0SJohannes Berg 
1362c20e08b0SJohannes Berg 	return 0;
1363c20e08b0SJohannes Berg 
1364c20e08b0SJohannes Berg out_err:
1365cfbc6c4cSSara Sharon 	iwl_mvm_disable_txq(mvm, sta, queue, tid, 0);
1366c20e08b0SJohannes Berg 
1367c20e08b0SJohannes Berg 	return ret;
1368c20e08b0SJohannes Berg }
1369c20e08b0SJohannes Berg 
137024afba76SLiad Kaufman static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
137124afba76SLiad Kaufman {
137224afba76SLiad Kaufman 	if (tid == IWL_MAX_TID_COUNT)
137324afba76SLiad Kaufman 		return IEEE80211_AC_VO; /* MGMT */
137424afba76SLiad Kaufman 
137524afba76SLiad Kaufman 	return tid_to_mac80211_ac[tid];
137624afba76SLiad Kaufman }
137724afba76SLiad Kaufman 
137824afba76SLiad Kaufman void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
137924afba76SLiad Kaufman {
138024afba76SLiad Kaufman 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
138124afba76SLiad Kaufman 					   add_stream_wk);
138224afba76SLiad Kaufman 
138324afba76SLiad Kaufman 	mutex_lock(&mvm->mutex);
138424afba76SLiad Kaufman 
1385724fe771SJohannes Berg 	iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
13869f9af3d7SLiad Kaufman 
1387cfbc6c4cSSara Sharon 	while (!list_empty(&mvm->add_stream_txqs)) {
1388cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq;
1389cfbc6c4cSSara Sharon 		struct ieee80211_txq *txq;
1390cfbc6c4cSSara Sharon 		u8 tid;
139124afba76SLiad Kaufman 
1392cfbc6c4cSSara Sharon 		mvmtxq = list_first_entry(&mvm->add_stream_txqs,
1393cfbc6c4cSSara Sharon 					  struct iwl_mvm_txq, list);
139424afba76SLiad Kaufman 
1395cfbc6c4cSSara Sharon 		txq = container_of((void *)mvmtxq, struct ieee80211_txq,
1396cfbc6c4cSSara Sharon 				   drv_priv);
1397cfbc6c4cSSara Sharon 		tid = txq->tid;
1398cfbc6c4cSSara Sharon 		if (tid == IEEE80211_NUM_TIDS)
1399cfbc6c4cSSara Sharon 			tid = IWL_MAX_TID_COUNT;
1400cfbc6c4cSSara Sharon 
1401cfbc6c4cSSara Sharon 		iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, tid);
1402cfbc6c4cSSara Sharon 		list_del_init(&mvmtxq->list);
1403f5ae2f93SJohannes Berg 		local_bh_disable();
1404cfbc6c4cSSara Sharon 		iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1405f5ae2f93SJohannes Berg 		local_bh_enable();
140624afba76SLiad Kaufman 	}
140724afba76SLiad Kaufman 
140824afba76SLiad Kaufman 	mutex_unlock(&mvm->mutex);
140924afba76SLiad Kaufman }
141024afba76SLiad Kaufman 
141124afba76SLiad Kaufman static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1412d5216a28SLiad Kaufman 				      struct ieee80211_sta *sta,
1413d5216a28SLiad Kaufman 				      enum nl80211_iftype vif_type)
141424afba76SLiad Kaufman {
141524afba76SLiad Kaufman 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
141624afba76SLiad Kaufman 	int queue;
141724afba76SLiad Kaufman 
1418396952eeSSara Sharon 	/* queue reserving is disabled on new TX path */
1419396952eeSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1420396952eeSSara Sharon 		return 0;
1421396952eeSSara Sharon 
1422724fe771SJohannes Berg 	/* run the general cleanup/unsharing of queues */
1423724fe771SJohannes Berg 	iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
14249794c64fSLiad Kaufman 
142524afba76SLiad Kaufman 	/* Make sure we have free resources for this STA */
1426d5216a28SLiad Kaufman 	if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
14271c14089eSJohannes Berg 	    !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].tid_bitmap &&
1428cf961e16SLiad Kaufman 	    (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1429cf961e16SLiad Kaufman 	     IWL_MVM_QUEUE_FREE))
1430d5216a28SLiad Kaufman 		queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1431d5216a28SLiad Kaufman 	else
14329794c64fSLiad Kaufman 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
14339794c64fSLiad Kaufman 						IWL_MVM_DQA_MIN_DATA_QUEUE,
143424afba76SLiad Kaufman 						IWL_MVM_DQA_MAX_DATA_QUEUE);
143524afba76SLiad Kaufman 	if (queue < 0) {
1436724fe771SJohannes Berg 		/* try again - this time kick out a queue if needed */
1437724fe771SJohannes Berg 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1438724fe771SJohannes Berg 		if (queue < 0) {
143924afba76SLiad Kaufman 			IWL_ERR(mvm, "No available queues for new station\n");
144024afba76SLiad Kaufman 			return -ENOSPC;
1441724fe771SJohannes Berg 		}
144224afba76SLiad Kaufman 	}
1443cf961e16SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
144424afba76SLiad Kaufman 
144524afba76SLiad Kaufman 	mvmsta->reserved_queue = queue;
144624afba76SLiad Kaufman 
144724afba76SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
144824afba76SLiad Kaufman 			    queue, mvmsta->sta_id);
144924afba76SLiad Kaufman 
145024afba76SLiad Kaufman 	return 0;
145124afba76SLiad Kaufman }
145224afba76SLiad Kaufman 
14538d98ae6eSLiad Kaufman /*
14548d98ae6eSLiad Kaufman  * In DQA mode, after a HW restart the queues should be allocated as before, in
14558d98ae6eSLiad Kaufman  * order to avoid race conditions when there are shared queues. This function
14568d98ae6eSLiad Kaufman  * does the re-mapping and queue allocation.
14578d98ae6eSLiad Kaufman  *
14588d98ae6eSLiad Kaufman  * Note that re-enabling aggregations isn't done in this function.
14598d98ae6eSLiad Kaufman  */
14608d98ae6eSLiad Kaufman static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1461cfbc6c4cSSara Sharon 						 struct ieee80211_sta *sta)
14628d98ae6eSLiad Kaufman {
1463cfbc6c4cSSara Sharon 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1464cfbc6c4cSSara Sharon 	unsigned int wdg =
14658d98ae6eSLiad Kaufman 		iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
14668d98ae6eSLiad Kaufman 	int i;
14678d98ae6eSLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
14688d98ae6eSLiad Kaufman 		.sta_id = mvm_sta->sta_id,
14698d98ae6eSLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
14708d98ae6eSLiad Kaufman 	};
14718d98ae6eSLiad Kaufman 
147203c902bfSJohannes Berg 	/* Make sure reserved queue is still marked as such (if allocated) */
147303c902bfSJohannes Berg 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
14748d98ae6eSLiad Kaufman 		mvm->queue_info[mvm_sta->reserved_queue].status =
14758d98ae6eSLiad Kaufman 			IWL_MVM_QUEUE_RESERVED;
14768d98ae6eSLiad Kaufman 
14778d98ae6eSLiad Kaufman 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
14788d98ae6eSLiad Kaufman 		struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
14798d98ae6eSLiad Kaufman 		int txq_id = tid_data->txq_id;
14808d98ae6eSLiad Kaufman 		int ac;
14818d98ae6eSLiad Kaufman 
14826862fceeSSara Sharon 		if (txq_id == IWL_MVM_INVALID_QUEUE)
14838d98ae6eSLiad Kaufman 			continue;
14848d98ae6eSLiad Kaufman 
14858d98ae6eSLiad Kaufman 		ac = tid_to_mac80211_ac[i];
14868d98ae6eSLiad Kaufman 
1487310181ecSSara Sharon 		if (iwl_mvm_has_new_tx_api(mvm)) {
1488310181ecSSara Sharon 			IWL_DEBUG_TX_QUEUES(mvm,
1489310181ecSSara Sharon 					    "Re-mapping sta %d tid %d\n",
1490310181ecSSara Sharon 					    mvm_sta->sta_id, i);
1491cfbc6c4cSSara Sharon 			txq_id = iwl_mvm_tvqm_enable_txq(mvm, mvm_sta->sta_id,
1492cfbc6c4cSSara Sharon 							 i, wdg);
1493310181ecSSara Sharon 			tid_data->txq_id = txq_id;
14945d39051aSLiad Kaufman 
14955d39051aSLiad Kaufman 			/*
14965d39051aSLiad Kaufman 			 * Since we don't set the seq number after reset, and HW
14975d39051aSLiad Kaufman 			 * sets it now, FW reset will cause the seq num to start
14985d39051aSLiad Kaufman 			 * at 0 again, so driver will need to update it
14995d39051aSLiad Kaufman 			 * internally as well, so it keeps in sync with real val
15005d39051aSLiad Kaufman 			 */
15015d39051aSLiad Kaufman 			tid_data->seq_number = 0;
1502310181ecSSara Sharon 		} else {
1503310181ecSSara Sharon 			u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1504310181ecSSara Sharon 
15058d98ae6eSLiad Kaufman 			cfg.tid = i;
1506cf6c6ea3SEmmanuel Grumbach 			cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
15078d98ae6eSLiad Kaufman 			cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1508310181ecSSara Sharon 					 txq_id ==
1509310181ecSSara Sharon 					 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
15108d98ae6eSLiad Kaufman 
15118d98ae6eSLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm,
15128d98ae6eSLiad Kaufman 					    "Re-mapping sta %d tid %d to queue %d\n",
15138d98ae6eSLiad Kaufman 					    mvm_sta->sta_id, i, txq_id);
15148d98ae6eSLiad Kaufman 
1515cfbc6c4cSSara Sharon 			iwl_mvm_enable_txq(mvm, sta, txq_id, seq, &cfg, wdg);
15168d98ae6eSLiad Kaufman 			mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
15178d98ae6eSLiad Kaufman 		}
151834e10860SSara Sharon 	}
15198d98ae6eSLiad Kaufman }
15208d98ae6eSLiad Kaufman 
1521732d06e9SShaul Triebitz static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1522732d06e9SShaul Triebitz 				      struct iwl_mvm_int_sta *sta,
1523732d06e9SShaul Triebitz 				      const u8 *addr,
1524732d06e9SShaul Triebitz 				      u16 mac_id, u16 color)
1525732d06e9SShaul Triebitz {
1526732d06e9SShaul Triebitz 	struct iwl_mvm_add_sta_cmd cmd;
1527732d06e9SShaul Triebitz 	int ret;
15283f497de9SLuca Coelho 	u32 status = ADD_STA_SUCCESS;
1529732d06e9SShaul Triebitz 
1530732d06e9SShaul Triebitz 	lockdep_assert_held(&mvm->mutex);
1531732d06e9SShaul Triebitz 
1532732d06e9SShaul Triebitz 	memset(&cmd, 0, sizeof(cmd));
1533732d06e9SShaul Triebitz 	cmd.sta_id = sta->sta_id;
1534732d06e9SShaul Triebitz 	cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1535732d06e9SShaul Triebitz 							     color));
1536732d06e9SShaul Triebitz 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1537732d06e9SShaul Triebitz 		cmd.station_type = sta->type;
1538732d06e9SShaul Triebitz 
1539732d06e9SShaul Triebitz 	if (!iwl_mvm_has_new_tx_api(mvm))
1540732d06e9SShaul Triebitz 		cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1541732d06e9SShaul Triebitz 	cmd.tid_disable_tx = cpu_to_le16(0xffff);
1542732d06e9SShaul Triebitz 
1543732d06e9SShaul Triebitz 	if (addr)
1544732d06e9SShaul Triebitz 		memcpy(cmd.addr, addr, ETH_ALEN);
1545732d06e9SShaul Triebitz 
1546732d06e9SShaul Triebitz 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1547732d06e9SShaul Triebitz 					  iwl_mvm_add_sta_cmd_size(mvm),
1548732d06e9SShaul Triebitz 					  &cmd, &status);
1549732d06e9SShaul Triebitz 	if (ret)
1550732d06e9SShaul Triebitz 		return ret;
1551732d06e9SShaul Triebitz 
1552732d06e9SShaul Triebitz 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1553732d06e9SShaul Triebitz 	case ADD_STA_SUCCESS:
1554732d06e9SShaul Triebitz 		IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1555732d06e9SShaul Triebitz 		return 0;
1556732d06e9SShaul Triebitz 	default:
1557732d06e9SShaul Triebitz 		ret = -EIO;
1558732d06e9SShaul Triebitz 		IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1559732d06e9SShaul Triebitz 			status);
1560732d06e9SShaul Triebitz 		break;
1561732d06e9SShaul Triebitz 	}
1562732d06e9SShaul Triebitz 	return ret;
1563732d06e9SShaul Triebitz }
1564732d06e9SShaul Triebitz 
1565e705c121SKalle Valo int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1566e705c121SKalle Valo 		    struct ieee80211_vif *vif,
1567e705c121SKalle Valo 		    struct ieee80211_sta *sta)
1568e705c121SKalle Valo {
1569e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1570e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1571a571f5f6SSara Sharon 	struct iwl_mvm_rxq_dup_data *dup_data;
1572e705c121SKalle Valo 	int i, ret, sta_id;
1573732d06e9SShaul Triebitz 	bool sta_update = false;
1574732d06e9SShaul Triebitz 	unsigned int sta_flags = 0;
1575e705c121SKalle Valo 
1576e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1577e705c121SKalle Valo 
1578e705c121SKalle Valo 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1579e705c121SKalle Valo 		sta_id = iwl_mvm_find_free_sta_id(mvm,
1580e705c121SKalle Valo 						  ieee80211_vif_type_p2p(vif));
1581e705c121SKalle Valo 	else
1582e705c121SKalle Valo 		sta_id = mvm_sta->sta_id;
1583e705c121SKalle Valo 
15840ae98812SSara Sharon 	if (sta_id == IWL_MVM_INVALID_STA)
1585e705c121SKalle Valo 		return -ENOSPC;
1586e705c121SKalle Valo 
1587e705c121SKalle Valo 	spin_lock_init(&mvm_sta->lock);
1588e705c121SKalle Valo 
1589c8f54701SJohannes Berg 	/* if this is a HW restart re-alloc existing queues */
1590c8f54701SJohannes Berg 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1591732d06e9SShaul Triebitz 		struct iwl_mvm_int_sta tmp_sta = {
1592732d06e9SShaul Triebitz 			.sta_id = sta_id,
1593732d06e9SShaul Triebitz 			.type = mvm_sta->sta_type,
1594732d06e9SShaul Triebitz 		};
1595732d06e9SShaul Triebitz 
1596732d06e9SShaul Triebitz 		/*
1597732d06e9SShaul Triebitz 		 * First add an empty station since allocating
1598732d06e9SShaul Triebitz 		 * a queue requires a valid station
1599732d06e9SShaul Triebitz 		 */
1600732d06e9SShaul Triebitz 		ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1601732d06e9SShaul Triebitz 						 mvmvif->id, mvmvif->color);
1602732d06e9SShaul Triebitz 		if (ret)
1603732d06e9SShaul Triebitz 			goto err;
1604732d06e9SShaul Triebitz 
1605cfbc6c4cSSara Sharon 		iwl_mvm_realloc_queues_after_restart(mvm, sta);
1606732d06e9SShaul Triebitz 		sta_update = true;
1607732d06e9SShaul Triebitz 		sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
16088d98ae6eSLiad Kaufman 		goto update_fw;
16098d98ae6eSLiad Kaufman 	}
16108d98ae6eSLiad Kaufman 
1611e705c121SKalle Valo 	mvm_sta->sta_id = sta_id;
1612e705c121SKalle Valo 	mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1613e705c121SKalle Valo 						      mvmvif->color);
1614e705c121SKalle Valo 	mvm_sta->vif = vif;
1615a58bb468SLiad Kaufman 	if (!mvm->trans->cfg->gen2)
1616e705c121SKalle Valo 		mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1617a58bb468SLiad Kaufman 	else
1618a58bb468SLiad Kaufman 		mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
1619e705c121SKalle Valo 	mvm_sta->tx_protection = 0;
1620e705c121SKalle Valo 	mvm_sta->tt_tx_protection = false;
1621ced19f26SSara Sharon 	mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
1622e705c121SKalle Valo 
1623e705c121SKalle Valo 	/* HW restart, don't assume the memory has been zeroed */
1624e705c121SKalle Valo 	mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1625e705c121SKalle Valo 	mvm_sta->tfd_queue_msk = 0;
1626e705c121SKalle Valo 
1627e705c121SKalle Valo 	/* for HW restart - reset everything but the sequence number */
162824afba76SLiad Kaufman 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1629e705c121SKalle Valo 		u16 seq = mvm_sta->tid_data[i].seq_number;
1630e705c121SKalle Valo 		memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1631e705c121SKalle Valo 		mvm_sta->tid_data[i].seq_number = seq;
163224afba76SLiad Kaufman 
163324afba76SLiad Kaufman 		/*
163424afba76SLiad Kaufman 		 * Mark all queues for this STA as unallocated and defer TX
163524afba76SLiad Kaufman 		 * frames until the queue is allocated
163624afba76SLiad Kaufman 		 */
16376862fceeSSara Sharon 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1638e705c121SKalle Valo 	}
1639cfbc6c4cSSara Sharon 
1640cfbc6c4cSSara Sharon 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1641cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
1642cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1643cfbc6c4cSSara Sharon 
1644cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1645cfbc6c4cSSara Sharon 		INIT_LIST_HEAD(&mvmtxq->list);
1646fba8248eSSara Sharon 		atomic_set(&mvmtxq->tx_request, 0);
1647cfbc6c4cSSara Sharon 	}
1648cfbc6c4cSSara Sharon 
1649e705c121SKalle Valo 	mvm_sta->agg_tids = 0;
1650e705c121SKalle Valo 
1651a571f5f6SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) &&
1652a571f5f6SSara Sharon 	    !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
165392c4dca6SJohannes Berg 		int q;
165492c4dca6SJohannes Berg 
1655a571f5f6SSara Sharon 		dup_data = kcalloc(mvm->trans->num_rx_queues,
165692c4dca6SJohannes Berg 				   sizeof(*dup_data), GFP_KERNEL);
1657a571f5f6SSara Sharon 		if (!dup_data)
1658a571f5f6SSara Sharon 			return -ENOMEM;
165992c4dca6SJohannes Berg 		/*
166092c4dca6SJohannes Berg 		 * Initialize all the last_seq values to 0xffff which can never
166192c4dca6SJohannes Berg 		 * compare equal to the frame's seq_ctrl in the check in
166292c4dca6SJohannes Berg 		 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
166392c4dca6SJohannes Berg 		 * number and fragmented packets don't reach that function.
166492c4dca6SJohannes Berg 		 *
166592c4dca6SJohannes Berg 		 * This thus allows receiving a packet with seqno 0 and the
166692c4dca6SJohannes Berg 		 * retry bit set as the very first packet on a new TID.
166792c4dca6SJohannes Berg 		 */
166892c4dca6SJohannes Berg 		for (q = 0; q < mvm->trans->num_rx_queues; q++)
166992c4dca6SJohannes Berg 			memset(dup_data[q].last_seq, 0xff,
167092c4dca6SJohannes Berg 			       sizeof(dup_data[q].last_seq));
1671a571f5f6SSara Sharon 		mvm_sta->dup_data = dup_data;
1672a571f5f6SSara Sharon 	}
1673a571f5f6SSara Sharon 
1674c8f54701SJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm)) {
1675d5216a28SLiad Kaufman 		ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1676d5216a28SLiad Kaufman 						 ieee80211_vif_type_p2p(vif));
167724afba76SLiad Kaufman 		if (ret)
167824afba76SLiad Kaufman 			goto err;
167924afba76SLiad Kaufman 	}
168024afba76SLiad Kaufman 
16819f66a397SGregory Greenman 	/*
16829f66a397SGregory Greenman 	 * if rs is registered with mac80211, then "add station" will be handled
16839f66a397SGregory Greenman 	 * via the corresponding ops, otherwise need to notify rate scaling here
16849f66a397SGregory Greenman 	 */
16854243edb4SEmmanuel Grumbach 	if (iwl_mvm_has_tlc_offload(mvm))
16869f66a397SGregory Greenman 		iwl_mvm_rs_add_sta(mvm, mvm_sta);
16870f8084cdSMordechay Goodstein 	else
16880f8084cdSMordechay Goodstein 		mutex_init(&mvm_sta->lq_sta.rs_drv.mutex);
16890f8084cdSMordechay Goodstein 
16900f8084cdSMordechay Goodstein 	INIT_WORK(&mvm_sta->rs_init_wk, iwl_mvm_rs_init_wk);
16919f66a397SGregory Greenman 
16920dde2440SAvraham Stern 	iwl_mvm_toggle_tx_ant(mvm, &mvm_sta->tx_ant);
16930dde2440SAvraham Stern 
16948d98ae6eSLiad Kaufman update_fw:
1695732d06e9SShaul Triebitz 	ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
1696e705c121SKalle Valo 	if (ret)
1697e705c121SKalle Valo 		goto err;
1698e705c121SKalle Valo 
1699e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION) {
1700e705c121SKalle Valo 		if (!sta->tdls) {
17010ae98812SSara Sharon 			WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
1702e705c121SKalle Valo 			mvmvif->ap_sta_id = sta_id;
1703e705c121SKalle Valo 		} else {
17040ae98812SSara Sharon 			WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
1705e705c121SKalle Valo 		}
1706e705c121SKalle Valo 	}
1707e705c121SKalle Valo 
1708e705c121SKalle Valo 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1709e705c121SKalle Valo 
1710e705c121SKalle Valo 	return 0;
1711e705c121SKalle Valo 
1712e705c121SKalle Valo err:
1713e705c121SKalle Valo 	return ret;
1714e705c121SKalle Valo }
1715e705c121SKalle Valo 
1716e705c121SKalle Valo int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1717e705c121SKalle Valo 		      bool drain)
1718e705c121SKalle Valo {
1719e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
1720e705c121SKalle Valo 	int ret;
1721e705c121SKalle Valo 	u32 status;
1722e705c121SKalle Valo 
1723e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1724e705c121SKalle Valo 
1725e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1726e705c121SKalle Valo 	cmd.sta_id = mvmsta->sta_id;
1727e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
1728e705c121SKalle Valo 	cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1729e705c121SKalle Valo 	cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1730e705c121SKalle Valo 
1731e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
1732854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1733854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
1734e705c121SKalle Valo 					  &cmd, &status);
1735e705c121SKalle Valo 	if (ret)
1736e705c121SKalle Valo 		return ret;
1737e705c121SKalle Valo 
1738837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1739e705c121SKalle Valo 	case ADD_STA_SUCCESS:
1740e705c121SKalle Valo 		IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1741e705c121SKalle Valo 			       mvmsta->sta_id);
1742e705c121SKalle Valo 		break;
1743e705c121SKalle Valo 	default:
1744e705c121SKalle Valo 		ret = -EIO;
1745e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1746e705c121SKalle Valo 			mvmsta->sta_id);
1747e705c121SKalle Valo 		break;
1748e705c121SKalle Valo 	}
1749e705c121SKalle Valo 
1750e705c121SKalle Valo 	return ret;
1751e705c121SKalle Valo }
1752e705c121SKalle Valo 
1753e705c121SKalle Valo /*
1754e705c121SKalle Valo  * Remove a station from the FW table. Before sending the command to remove
1755e705c121SKalle Valo  * the station validate that the station is indeed known to the driver (sanity
1756e705c121SKalle Valo  * only).
1757e705c121SKalle Valo  */
1758e705c121SKalle Valo static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1759e705c121SKalle Valo {
1760e705c121SKalle Valo 	struct ieee80211_sta *sta;
1761e705c121SKalle Valo 	struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1762e705c121SKalle Valo 		.sta_id = sta_id,
1763e705c121SKalle Valo 	};
1764e705c121SKalle Valo 	int ret;
1765e705c121SKalle Valo 
1766e705c121SKalle Valo 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1767e705c121SKalle Valo 					lockdep_is_held(&mvm->mutex));
1768e705c121SKalle Valo 
1769e705c121SKalle Valo 	/* Note: internal stations are marked as error values */
1770e705c121SKalle Valo 	if (!sta) {
1771e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid station id\n");
1772e705c121SKalle Valo 		return -EINVAL;
1773e705c121SKalle Valo 	}
1774e705c121SKalle Valo 
1775e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1776e705c121SKalle Valo 				   sizeof(rm_sta_cmd), &rm_sta_cmd);
1777e705c121SKalle Valo 	if (ret) {
1778e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1779e705c121SKalle Valo 		return ret;
1780e705c121SKalle Valo 	}
1781e705c121SKalle Valo 
1782e705c121SKalle Valo 	return 0;
1783e705c121SKalle Valo }
1784e705c121SKalle Valo 
178524afba76SLiad Kaufman static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
178624afba76SLiad Kaufman 				       struct ieee80211_vif *vif,
1787cfbc6c4cSSara Sharon 				       struct ieee80211_sta *sta)
178824afba76SLiad Kaufman {
1789cfbc6c4cSSara Sharon 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
179024afba76SLiad Kaufman 	int i;
179124afba76SLiad Kaufman 
179224afba76SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
179324afba76SLiad Kaufman 
179424afba76SLiad Kaufman 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
17956862fceeSSara Sharon 		if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
179624afba76SLiad Kaufman 			continue;
179724afba76SLiad Kaufman 
1798cfbc6c4cSSara Sharon 		iwl_mvm_disable_txq(mvm, sta, mvm_sta->tid_data[i].txq_id, i,
1799cfbc6c4cSSara Sharon 				    0);
18006862fceeSSara Sharon 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
180124afba76SLiad Kaufman 	}
1802cfbc6c4cSSara Sharon 
1803cfbc6c4cSSara Sharon 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1804cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
1805cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1806cfbc6c4cSSara Sharon 
1807cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1808cfbc6c4cSSara Sharon 	}
180924afba76SLiad Kaufman }
181024afba76SLiad Kaufman 
1811d6d517b7SSara Sharon int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1812d6d517b7SSara Sharon 				  struct iwl_mvm_sta *mvm_sta)
1813d6d517b7SSara Sharon {
1814bec9522aSSharon Dvir 	int i;
1815d6d517b7SSara Sharon 
1816d6d517b7SSara Sharon 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1817d6d517b7SSara Sharon 		u16 txq_id;
1818bec9522aSSharon Dvir 		int ret;
1819d6d517b7SSara Sharon 
1820d6d517b7SSara Sharon 		spin_lock_bh(&mvm_sta->lock);
1821d6d517b7SSara Sharon 		txq_id = mvm_sta->tid_data[i].txq_id;
1822d6d517b7SSara Sharon 		spin_unlock_bh(&mvm_sta->lock);
1823d6d517b7SSara Sharon 
1824d6d517b7SSara Sharon 		if (txq_id == IWL_MVM_INVALID_QUEUE)
1825d6d517b7SSara Sharon 			continue;
1826d6d517b7SSara Sharon 
1827d6d517b7SSara Sharon 		ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1828d6d517b7SSara Sharon 		if (ret)
1829bec9522aSSharon Dvir 			return ret;
1830d6d517b7SSara Sharon 	}
1831d6d517b7SSara Sharon 
1832bec9522aSSharon Dvir 	return 0;
1833d6d517b7SSara Sharon }
1834d6d517b7SSara Sharon 
1835e705c121SKalle Valo int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1836e705c121SKalle Valo 		   struct ieee80211_vif *vif,
1837e705c121SKalle Valo 		   struct ieee80211_sta *sta)
1838e705c121SKalle Valo {
1839e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1840e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
184194c3e614SSara Sharon 	u8 sta_id = mvm_sta->sta_id;
1842e705c121SKalle Valo 	int ret;
1843e705c121SKalle Valo 
1844e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1845e705c121SKalle Valo 
1846a571f5f6SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
1847a571f5f6SSara Sharon 		kfree(mvm_sta->dup_data);
1848a571f5f6SSara Sharon 
1849e705c121SKalle Valo 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1850e705c121SKalle Valo 	if (ret)
1851e705c121SKalle Valo 		return ret;
1852c8f54701SJohannes Berg 
18530f8084cdSMordechay Goodstein 	cancel_work_sync(&mvm_sta->rs_init_wk);
18540f8084cdSMordechay Goodstein 
1855e705c121SKalle Valo 	/* flush its queues here since we are freeing mvm_sta */
1856d49394a1SSara Sharon 	ret = iwl_mvm_flush_sta(mvm, mvm_sta, false, 0);
1857e705c121SKalle Valo 	if (ret)
1858e705c121SKalle Valo 		return ret;
1859d6d517b7SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
1860d6d517b7SSara Sharon 		ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1861d6d517b7SSara Sharon 	} else {
1862d6d517b7SSara Sharon 		u32 q_mask = mvm_sta->tfd_queue_msk;
1863d6d517b7SSara Sharon 
1864a1a57877SSara Sharon 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1865d6d517b7SSara Sharon 						     q_mask);
1866d6d517b7SSara Sharon 	}
1867e705c121SKalle Valo 	if (ret)
1868e705c121SKalle Valo 		return ret;
1869c8f54701SJohannes Berg 
1870e705c121SKalle Valo 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1871e705c121SKalle Valo 
1872cfbc6c4cSSara Sharon 	iwl_mvm_disable_sta_queues(mvm, vif, sta);
187356214749SLiad Kaufman 
187456214749SLiad Kaufman 	/* If there is a TXQ still marked as reserved - free it */
1875c8f54701SJohannes Berg 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1876a0315deaSLiad Kaufman 		u8 reserved_txq = mvm_sta->reserved_queue;
1877a0315deaSLiad Kaufman 		enum iwl_mvm_queue_status *status;
1878a0315deaSLiad Kaufman 
1879a0315deaSLiad Kaufman 		/*
1880a0315deaSLiad Kaufman 		 * If no traffic has gone through the reserved TXQ - it
1881a0315deaSLiad Kaufman 		 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1882a0315deaSLiad Kaufman 		 * should be manually marked as free again
1883a0315deaSLiad Kaufman 		 */
1884a0315deaSLiad Kaufman 		status = &mvm->queue_info[reserved_txq].status;
1885a0315deaSLiad Kaufman 		if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1886a0315deaSLiad Kaufman 			 (*status != IWL_MVM_QUEUE_FREE),
1887a0315deaSLiad Kaufman 			 "sta_id %d reserved txq %d status %d",
1888f3f240f9SJohannes Berg 			 sta_id, reserved_txq, *status))
1889a0315deaSLiad Kaufman 			return -EINVAL;
1890a0315deaSLiad Kaufman 
1891a0315deaSLiad Kaufman 		*status = IWL_MVM_QUEUE_FREE;
1892a0315deaSLiad Kaufman 	}
1893a0315deaSLiad Kaufman 
1894e3118ad7SLiad Kaufman 	if (vif->type == NL80211_IFTYPE_STATION &&
189594c3e614SSara Sharon 	    mvmvif->ap_sta_id == sta_id) {
1896e3118ad7SLiad Kaufman 		/* if associated - we can't remove the AP STA now */
1897e705c121SKalle Valo 		if (vif->bss_conf.assoc)
1898e705c121SKalle Valo 			return ret;
1899e705c121SKalle Valo 
1900e705c121SKalle Valo 		/* unassoc - go ahead - remove the AP STA now */
19010ae98812SSara Sharon 		mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1902e705c121SKalle Valo 
1903e705c121SKalle Valo 		/* clear d0i3_ap_sta_id if no longer relevant */
190494c3e614SSara Sharon 		if (mvm->d0i3_ap_sta_id == sta_id)
19050ae98812SSara Sharon 			mvm->d0i3_ap_sta_id = IWL_MVM_INVALID_STA;
1906e705c121SKalle Valo 	}
1907e705c121SKalle Valo 
1908e705c121SKalle Valo 	/*
1909e705c121SKalle Valo 	 * This shouldn't happen - the TDLS channel switch should be canceled
1910e705c121SKalle Valo 	 * before the STA is removed.
1911e705c121SKalle Valo 	 */
191294c3e614SSara Sharon 	if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
19130ae98812SSara Sharon 		mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1914e705c121SKalle Valo 		cancel_delayed_work(&mvm->tdls_cs.dwork);
1915e705c121SKalle Valo 	}
1916e705c121SKalle Valo 
1917e705c121SKalle Valo 	/*
1918e705c121SKalle Valo 	 * Make sure that the tx response code sees the station as -EBUSY and
1919e705c121SKalle Valo 	 * calls the drain worker.
1920e705c121SKalle Valo 	 */
1921e705c121SKalle Valo 	spin_lock_bh(&mvm_sta->lock);
1922e705c121SKalle Valo 	spin_unlock_bh(&mvm_sta->lock);
1923e705c121SKalle Valo 
1924e705c121SKalle Valo 	ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1925e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1926e705c121SKalle Valo 
1927e705c121SKalle Valo 	return ret;
1928e705c121SKalle Valo }
1929e705c121SKalle Valo 
1930e705c121SKalle Valo int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1931e705c121SKalle Valo 		      struct ieee80211_vif *vif,
1932e705c121SKalle Valo 		      u8 sta_id)
1933e705c121SKalle Valo {
1934e705c121SKalle Valo 	int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1935e705c121SKalle Valo 
1936e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1937e705c121SKalle Valo 
1938e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1939e705c121SKalle Valo 	return ret;
1940e705c121SKalle Valo }
1941e705c121SKalle Valo 
19420e39eb03SChaya Rachel Ivgi int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1943e705c121SKalle Valo 			     struct iwl_mvm_int_sta *sta,
1944ced19f26SSara Sharon 			     u32 qmask, enum nl80211_iftype iftype,
1945ced19f26SSara Sharon 			     enum iwl_sta_type type)
1946e705c121SKalle Valo {
1947df65c8d1SAvraham Stern 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1948df65c8d1SAvraham Stern 	    sta->sta_id == IWL_MVM_INVALID_STA) {
1949e705c121SKalle Valo 		sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
19500ae98812SSara Sharon 		if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
1951e705c121SKalle Valo 			return -ENOSPC;
1952e705c121SKalle Valo 	}
1953e705c121SKalle Valo 
1954e705c121SKalle Valo 	sta->tfd_queue_msk = qmask;
1955ced19f26SSara Sharon 	sta->type = type;
1956e705c121SKalle Valo 
1957e705c121SKalle Valo 	/* put a non-NULL value so iterating over the stations won't stop */
1958e705c121SKalle Valo 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1959e705c121SKalle Valo 	return 0;
1960e705c121SKalle Valo }
1961e705c121SKalle Valo 
196226d6c16bSSara Sharon void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
1963e705c121SKalle Valo {
1964e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
1965e705c121SKalle Valo 	memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
19660ae98812SSara Sharon 	sta->sta_id = IWL_MVM_INVALID_STA;
1967e705c121SKalle Valo }
1968e705c121SKalle Valo 
1969b13f43a4SEmmanuel Grumbach static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 *queue,
1970b13f43a4SEmmanuel Grumbach 					  u8 sta_id, u8 fifo)
1971e705c121SKalle Valo {
1972e705c121SKalle Valo 	unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1973e705c121SKalle Valo 					mvm->cfg->base_params->wd_timeout :
1974e705c121SKalle Valo 					IWL_WATCHDOG_DISABLED;
1975e705c121SKalle Valo 
1976310181ecSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
1977b13f43a4SEmmanuel Grumbach 		int tvqm_queue =
1978cfbc6c4cSSara Sharon 			iwl_mvm_tvqm_enable_txq(mvm, sta_id,
1979310181ecSSara Sharon 						IWL_MAX_TID_COUNT,
1980310181ecSSara Sharon 						wdg_timeout);
1981b13f43a4SEmmanuel Grumbach 		*queue = tvqm_queue;
1982c8f54701SJohannes Berg 	} else {
198328d0793eSLiad Kaufman 		struct iwl_trans_txq_scd_cfg cfg = {
1984b13f43a4SEmmanuel Grumbach 			.fifo = fifo,
1985b13f43a4SEmmanuel Grumbach 			.sta_id = sta_id,
198628d0793eSLiad Kaufman 			.tid = IWL_MAX_TID_COUNT,
198728d0793eSLiad Kaufman 			.aggregate = false,
198828d0793eSLiad Kaufman 			.frame_limit = IWL_FRAME_LIMIT,
198928d0793eSLiad Kaufman 		};
199028d0793eSLiad Kaufman 
1991cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, *queue, 0, &cfg, wdg_timeout);
199228d0793eSLiad Kaufman 	}
1993c5a719eeSSara Sharon }
1994c5a719eeSSara Sharon 
1995c5a719eeSSara Sharon int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1996c5a719eeSSara Sharon {
1997c5a719eeSSara Sharon 	int ret;
1998c5a719eeSSara Sharon 
1999c5a719eeSSara Sharon 	lockdep_assert_held(&mvm->mutex);
2000c5a719eeSSara Sharon 
2001c5a719eeSSara Sharon 	/* Allocate aux station and assign to it the aux queue */
2002c5a719eeSSara Sharon 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
2003ced19f26SSara Sharon 				       NL80211_IFTYPE_UNSPECIFIED,
2004ced19f26SSara Sharon 				       IWL_STA_AUX_ACTIVITY);
2005c5a719eeSSara Sharon 	if (ret)
2006c5a719eeSSara Sharon 		return ret;
2007c5a719eeSSara Sharon 
2008c5a719eeSSara Sharon 	/* Map Aux queue to fifo - needs to happen before adding Aux station */
2009c5a719eeSSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm))
2010b13f43a4SEmmanuel Grumbach 		iwl_mvm_enable_aux_snif_queue(mvm, &mvm->aux_queue,
2011b13f43a4SEmmanuel Grumbach 					      mvm->aux_sta.sta_id,
2012b13f43a4SEmmanuel Grumbach 					      IWL_MVM_TX_FIFO_MCAST);
201328d0793eSLiad Kaufman 
2014e705c121SKalle Valo 	ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
2015e705c121SKalle Valo 					 MAC_INDEX_AUX, 0);
2016c5a719eeSSara Sharon 	if (ret) {
2017e705c121SKalle Valo 		iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2018e705c121SKalle Valo 		return ret;
2019e705c121SKalle Valo 	}
2020e705c121SKalle Valo 
2021c5a719eeSSara Sharon 	/*
20222f7a3863SLuca Coelho 	 * For 22000 firmware and on we cannot add queue to a station unknown
2023c5a719eeSSara Sharon 	 * to firmware so enable queue here - after the station was added
2024c5a719eeSSara Sharon 	 */
2025c5a719eeSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm))
2026b13f43a4SEmmanuel Grumbach 		iwl_mvm_enable_aux_snif_queue(mvm, &mvm->aux_queue,
2027b13f43a4SEmmanuel Grumbach 					      mvm->aux_sta.sta_id,
2028b13f43a4SEmmanuel Grumbach 					      IWL_MVM_TX_FIFO_MCAST);
2029c5a719eeSSara Sharon 
2030c5a719eeSSara Sharon 	return 0;
2031c5a719eeSSara Sharon }
2032c5a719eeSSara Sharon 
20330e39eb03SChaya Rachel Ivgi int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
20340e39eb03SChaya Rachel Ivgi {
20350e39eb03SChaya Rachel Ivgi 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2036b13f43a4SEmmanuel Grumbach 	int ret;
20370e39eb03SChaya Rachel Ivgi 
20380e39eb03SChaya Rachel Ivgi 	lockdep_assert_held(&mvm->mutex);
2039b13f43a4SEmmanuel Grumbach 
2040b13f43a4SEmmanuel Grumbach 	/* Map snif queue to fifo - must happen before adding snif station */
2041b13f43a4SEmmanuel Grumbach 	if (!iwl_mvm_has_new_tx_api(mvm))
2042b13f43a4SEmmanuel Grumbach 		iwl_mvm_enable_aux_snif_queue(mvm, &mvm->snif_queue,
2043b13f43a4SEmmanuel Grumbach 					      mvm->snif_sta.sta_id,
2044b13f43a4SEmmanuel Grumbach 					      IWL_MVM_TX_FIFO_BE);
2045b13f43a4SEmmanuel Grumbach 
2046b13f43a4SEmmanuel Grumbach 	ret = iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
20470e39eb03SChaya Rachel Ivgi 					 mvmvif->id, 0);
2048b13f43a4SEmmanuel Grumbach 	if (ret)
2049b13f43a4SEmmanuel Grumbach 		return ret;
2050b13f43a4SEmmanuel Grumbach 
2051b13f43a4SEmmanuel Grumbach 	/*
2052b13f43a4SEmmanuel Grumbach 	 * For 22000 firmware and on we cannot add queue to a station unknown
2053b13f43a4SEmmanuel Grumbach 	 * to firmware so enable queue here - after the station was added
2054b13f43a4SEmmanuel Grumbach 	 */
2055b13f43a4SEmmanuel Grumbach 	if (iwl_mvm_has_new_tx_api(mvm))
2056b13f43a4SEmmanuel Grumbach 		iwl_mvm_enable_aux_snif_queue(mvm, &mvm->snif_queue,
2057b13f43a4SEmmanuel Grumbach 					      mvm->snif_sta.sta_id,
2058b13f43a4SEmmanuel Grumbach 					      IWL_MVM_TX_FIFO_BE);
2059b13f43a4SEmmanuel Grumbach 
2060b13f43a4SEmmanuel Grumbach 	return 0;
20610e39eb03SChaya Rachel Ivgi }
20620e39eb03SChaya Rachel Ivgi 
20630e39eb03SChaya Rachel Ivgi int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
20640e39eb03SChaya Rachel Ivgi {
20650e39eb03SChaya Rachel Ivgi 	int ret;
20660e39eb03SChaya Rachel Ivgi 
20670e39eb03SChaya Rachel Ivgi 	lockdep_assert_held(&mvm->mutex);
20680e39eb03SChaya Rachel Ivgi 
2069cfbc6c4cSSara Sharon 	iwl_mvm_disable_txq(mvm, NULL, mvm->snif_queue, IWL_MAX_TID_COUNT, 0);
20700e39eb03SChaya Rachel Ivgi 	ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
20710e39eb03SChaya Rachel Ivgi 	if (ret)
20720e39eb03SChaya Rachel Ivgi 		IWL_WARN(mvm, "Failed sending remove station\n");
20730e39eb03SChaya Rachel Ivgi 
20740e39eb03SChaya Rachel Ivgi 	return ret;
20750e39eb03SChaya Rachel Ivgi }
20760e39eb03SChaya Rachel Ivgi 
20770e39eb03SChaya Rachel Ivgi void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
20780e39eb03SChaya Rachel Ivgi {
20790e39eb03SChaya Rachel Ivgi 	iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
20800e39eb03SChaya Rachel Ivgi }
20810e39eb03SChaya Rachel Ivgi 
2082e705c121SKalle Valo void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
2083e705c121SKalle Valo {
2084e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2085e705c121SKalle Valo 
2086e705c121SKalle Valo 	iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2087e705c121SKalle Valo }
2088e705c121SKalle Valo 
2089e705c121SKalle Valo /*
2090e705c121SKalle Valo  * Send the add station command for the vif's broadcast station.
2091e705c121SKalle Valo  * Assumes that the station was already allocated.
2092e705c121SKalle Valo  *
2093e705c121SKalle Valo  * @mvm: the mvm component
2094e705c121SKalle Valo  * @vif: the interface to which the broadcast station is added
2095e705c121SKalle Valo  * @bsta: the broadcast station to add.
2096e705c121SKalle Valo  */
2097e705c121SKalle Valo int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2098e705c121SKalle Valo {
2099e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2100e705c121SKalle Valo 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2101e705c121SKalle Valo 	static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2102e705c121SKalle Valo 	const u8 *baddr = _baddr;
21037daa7624SJohannes Berg 	int queue;
2104df88c08dSLiad Kaufman 	int ret;
2105c5a719eeSSara Sharon 	unsigned int wdg_timeout =
2106c5a719eeSSara Sharon 		iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2107de24f638SLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
2108de24f638SLiad Kaufman 		.fifo = IWL_MVM_TX_FIFO_VO,
2109de24f638SLiad Kaufman 		.sta_id = mvmvif->bcast_sta.sta_id,
2110de24f638SLiad Kaufman 		.tid = IWL_MAX_TID_COUNT,
2111de24f638SLiad Kaufman 		.aggregate = false,
2112de24f638SLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
2113de24f638SLiad Kaufman 	};
2114de24f638SLiad Kaufman 
2115c5a719eeSSara Sharon 	lockdep_assert_held(&mvm->mutex);
2116c5a719eeSSara Sharon 
2117c8f54701SJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm)) {
21184d339989SLiad Kaufman 		if (vif->type == NL80211_IFTYPE_AP ||
2119f8510d67SNathan Chancellor 		    vif->type == NL80211_IFTYPE_ADHOC) {
212049f71713SSara Sharon 			queue = mvm->probe_queue;
2121f8510d67SNathan Chancellor 		} else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
212249f71713SSara Sharon 			queue = mvm->p2p_dev_queue;
2123f8510d67SNathan Chancellor 		} else {
2124f8510d67SNathan Chancellor 			WARN(1, "Missing required TXQ for adding bcast STA\n");
2125de24f638SLiad Kaufman 			return -EINVAL;
2126f8510d67SNathan Chancellor 		}
2127de24f638SLiad Kaufman 
2128df88c08dSLiad Kaufman 		bsta->tfd_queue_msk |= BIT(queue);
2129c5a719eeSSara Sharon 
2130cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2131de24f638SLiad Kaufman 	}
2132de24f638SLiad Kaufman 
2133e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_ADHOC)
2134e705c121SKalle Valo 		baddr = vif->bss_conf.bssid;
2135e705c121SKalle Valo 
21360ae98812SSara Sharon 	if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
2137e705c121SKalle Valo 		return -ENOSPC;
2138e705c121SKalle Valo 
2139df88c08dSLiad Kaufman 	ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2140e705c121SKalle Valo 					 mvmvif->id, mvmvif->color);
2141df88c08dSLiad Kaufman 	if (ret)
2142df88c08dSLiad Kaufman 		return ret;
2143df88c08dSLiad Kaufman 
2144df88c08dSLiad Kaufman 	/*
21452f7a3863SLuca Coelho 	 * For 22000 firmware and on we cannot add queue to a station unknown
2146c5a719eeSSara Sharon 	 * to firmware so enable queue here - after the station was added
2147df88c08dSLiad Kaufman 	 */
2148310181ecSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
2149cfbc6c4cSSara Sharon 		queue = iwl_mvm_tvqm_enable_txq(mvm, bsta->sta_id,
2150310181ecSSara Sharon 						IWL_MAX_TID_COUNT,
2151c5a719eeSSara Sharon 						wdg_timeout);
21527daa7624SJohannes Berg 
21537b758a11SLuca Coelho 		if (vif->type == NL80211_IFTYPE_AP ||
21547b758a11SLuca Coelho 		    vif->type == NL80211_IFTYPE_ADHOC)
2155310181ecSSara Sharon 			mvm->probe_queue = queue;
2156310181ecSSara Sharon 		else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
2157310181ecSSara Sharon 			mvm->p2p_dev_queue = queue;
2158310181ecSSara Sharon 	}
2159df88c08dSLiad Kaufman 
2160df88c08dSLiad Kaufman 	return 0;
2161df88c08dSLiad Kaufman }
2162df88c08dSLiad Kaufman 
2163df88c08dSLiad Kaufman static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2164df88c08dSLiad Kaufman 					  struct ieee80211_vif *vif)
2165df88c08dSLiad Kaufman {
2166df88c08dSLiad Kaufman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2167d167e81aSMordechai Goodstein 	int queue;
2168df88c08dSLiad Kaufman 
2169df88c08dSLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
2170df88c08dSLiad Kaufman 
2171d49394a1SSara Sharon 	iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true, 0);
2172d49394a1SSara Sharon 
2173d167e81aSMordechai Goodstein 	switch (vif->type) {
2174d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_AP:
2175d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_ADHOC:
2176d167e81aSMordechai Goodstein 		queue = mvm->probe_queue;
2177d167e81aSMordechai Goodstein 		break;
2178d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_P2P_DEVICE:
2179d167e81aSMordechai Goodstein 		queue = mvm->p2p_dev_queue;
2180d167e81aSMordechai Goodstein 		break;
2181d167e81aSMordechai Goodstein 	default:
2182d167e81aSMordechai Goodstein 		WARN(1, "Can't free bcast queue on vif type %d\n",
2183d167e81aSMordechai Goodstein 		     vif->type);
2184d167e81aSMordechai Goodstein 		return;
2185df88c08dSLiad Kaufman 	}
2186df88c08dSLiad Kaufman 
2187cfbc6c4cSSara Sharon 	iwl_mvm_disable_txq(mvm, NULL, queue, IWL_MAX_TID_COUNT, 0);
2188d167e81aSMordechai Goodstein 	if (iwl_mvm_has_new_tx_api(mvm))
2189d167e81aSMordechai Goodstein 		return;
2190d167e81aSMordechai Goodstein 
2191d167e81aSMordechai Goodstein 	WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
2192d167e81aSMordechai Goodstein 	mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
2193e705c121SKalle Valo }
2194e705c121SKalle Valo 
2195e705c121SKalle Valo /* Send the FW a request to remove the station from it's internal data
2196e705c121SKalle Valo  * structures, but DO NOT remove the entry from the local data structures. */
2197e705c121SKalle Valo int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2198e705c121SKalle Valo {
2199e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2200e705c121SKalle Valo 	int ret;
2201e705c121SKalle Valo 
2202e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2203e705c121SKalle Valo 
2204df88c08dSLiad Kaufman 	iwl_mvm_free_bcast_sta_queues(mvm, vif);
2205df88c08dSLiad Kaufman 
2206e705c121SKalle Valo 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
2207e705c121SKalle Valo 	if (ret)
2208e705c121SKalle Valo 		IWL_WARN(mvm, "Failed sending remove station\n");
2209e705c121SKalle Valo 	return ret;
2210e705c121SKalle Valo }
2211e705c121SKalle Valo 
2212e705c121SKalle Valo int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2213e705c121SKalle Valo {
2214e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2215e705c121SKalle Valo 
2216e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2217e705c121SKalle Valo 
2218c8f54701SJohannes Berg 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, 0,
2219ced19f26SSara Sharon 					ieee80211_vif_type_p2p(vif),
2220ced19f26SSara Sharon 					IWL_STA_GENERAL_PURPOSE);
2221e705c121SKalle Valo }
2222e705c121SKalle Valo 
2223e705c121SKalle Valo /* Allocate a new station entry for the broadcast station to the given vif,
2224e705c121SKalle Valo  * and send it to the FW.
2225e705c121SKalle Valo  * Note that each P2P mac should have its own broadcast station.
2226e705c121SKalle Valo  *
2227e705c121SKalle Valo  * @mvm: the mvm component
2228e705c121SKalle Valo  * @vif: the interface to which the broadcast station is added
2229e705c121SKalle Valo  * @bsta: the broadcast station to add. */
2230d197358bSLuca Coelho int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2231e705c121SKalle Valo {
2232e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2233e705c121SKalle Valo 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2234e705c121SKalle Valo 	int ret;
2235e705c121SKalle Valo 
2236e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2237e705c121SKalle Valo 
2238e705c121SKalle Valo 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2239e705c121SKalle Valo 	if (ret)
2240e705c121SKalle Valo 		return ret;
2241e705c121SKalle Valo 
2242e705c121SKalle Valo 	ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2243e705c121SKalle Valo 
2244e705c121SKalle Valo 	if (ret)
2245e705c121SKalle Valo 		iwl_mvm_dealloc_int_sta(mvm, bsta);
2246e705c121SKalle Valo 
2247e705c121SKalle Valo 	return ret;
2248e705c121SKalle Valo }
2249e705c121SKalle Valo 
2250e705c121SKalle Valo void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2251e705c121SKalle Valo {
2252e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2253e705c121SKalle Valo 
2254e705c121SKalle Valo 	iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2255e705c121SKalle Valo }
2256e705c121SKalle Valo 
2257e705c121SKalle Valo /*
2258e705c121SKalle Valo  * Send the FW a request to remove the station from it's internal data
2259e705c121SKalle Valo  * structures, and in addition remove it from the local data structure.
2260e705c121SKalle Valo  */
2261d197358bSLuca Coelho int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2262e705c121SKalle Valo {
2263e705c121SKalle Valo 	int ret;
2264e705c121SKalle Valo 
2265e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2266e705c121SKalle Valo 
2267e705c121SKalle Valo 	ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2268e705c121SKalle Valo 
2269e705c121SKalle Valo 	iwl_mvm_dealloc_bcast_sta(mvm, vif);
2270e705c121SKalle Valo 
2271e705c121SKalle Valo 	return ret;
2272e705c121SKalle Valo }
2273e705c121SKalle Valo 
227426d6c16bSSara Sharon /*
227526d6c16bSSara Sharon  * Allocate a new station entry for the multicast station to the given vif,
227626d6c16bSSara Sharon  * and send it to the FW.
227726d6c16bSSara Sharon  * Note that each AP/GO mac should have its own multicast station.
227826d6c16bSSara Sharon  *
227926d6c16bSSara Sharon  * @mvm: the mvm component
228026d6c16bSSara Sharon  * @vif: the interface to which the multicast station is added
228126d6c16bSSara Sharon  */
228226d6c16bSSara Sharon int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
228326d6c16bSSara Sharon {
228426d6c16bSSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
228526d6c16bSSara Sharon 	struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
228626d6c16bSSara Sharon 	static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
228726d6c16bSSara Sharon 	const u8 *maddr = _maddr;
228826d6c16bSSara Sharon 	struct iwl_trans_txq_scd_cfg cfg = {
2289192a7e1fSJohannes Berg 		.fifo = vif->type == NL80211_IFTYPE_AP ?
2290192a7e1fSJohannes Berg 			IWL_MVM_TX_FIFO_MCAST : IWL_MVM_TX_FIFO_BE,
229126d6c16bSSara Sharon 		.sta_id = msta->sta_id,
22926508de03SIlan Peer 		.tid = 0,
229326d6c16bSSara Sharon 		.aggregate = false,
229426d6c16bSSara Sharon 		.frame_limit = IWL_FRAME_LIMIT,
229526d6c16bSSara Sharon 	};
229626d6c16bSSara Sharon 	unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
229726d6c16bSSara Sharon 	int ret;
229826d6c16bSSara Sharon 
229926d6c16bSSara Sharon 	lockdep_assert_held(&mvm->mutex);
230026d6c16bSSara Sharon 
2301ee48b722SLiad Kaufman 	if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2302ee48b722SLiad Kaufman 		    vif->type != NL80211_IFTYPE_ADHOC))
230326d6c16bSSara Sharon 		return -ENOTSUPP;
230426d6c16bSSara Sharon 
2305ced19f26SSara Sharon 	/*
2306fc07bd8cSSara Sharon 	 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2307fc07bd8cSSara Sharon 	 * invalid, so make sure we use the queue we want.
2308fc07bd8cSSara Sharon 	 * Note that this is done here as we want to avoid making DQA
2309fc07bd8cSSara Sharon 	 * changes in mac80211 layer.
2310fc07bd8cSSara Sharon 	 */
2311cfbc6c4cSSara Sharon 	if (vif->type == NL80211_IFTYPE_ADHOC)
2312cfbc6c4cSSara Sharon 		mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2313fc07bd8cSSara Sharon 
2314fc07bd8cSSara Sharon 	/*
2315ced19f26SSara Sharon 	 * While in previous FWs we had to exclude cab queue from TFD queue
2316ced19f26SSara Sharon 	 * mask, now it is needed as any other queue.
2317ced19f26SSara Sharon 	 */
2318ced19f26SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm) &&
2319ced19f26SSara Sharon 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2320cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2321cfbc6c4cSSara Sharon 				   timeout);
2322cfbc6c4cSSara Sharon 		msta->tfd_queue_msk |= BIT(mvmvif->cab_queue);
2323ced19f26SSara Sharon 	}
232426d6c16bSSara Sharon 	ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
232526d6c16bSSara Sharon 					 mvmvif->id, mvmvif->color);
232626d6c16bSSara Sharon 	if (ret) {
232726d6c16bSSara Sharon 		iwl_mvm_dealloc_int_sta(mvm, msta);
232826d6c16bSSara Sharon 		return ret;
232926d6c16bSSara Sharon 	}
233026d6c16bSSara Sharon 
233126d6c16bSSara Sharon 	/*
233226d6c16bSSara Sharon 	 * Enable cab queue after the ADD_STA command is sent.
23332f7a3863SLuca Coelho 	 * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
2334ced19f26SSara Sharon 	 * command with unknown station id, and for FW that doesn't support
2335ced19f26SSara Sharon 	 * station API since the cab queue is not included in the
2336ced19f26SSara Sharon 	 * tfd_queue_mask.
233726d6c16bSSara Sharon 	 */
2338310181ecSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
2339cfbc6c4cSSara Sharon 		int queue = iwl_mvm_tvqm_enable_txq(mvm, msta->sta_id,
23406508de03SIlan Peer 						    0,
234126d6c16bSSara Sharon 						    timeout);
2342e2af3fabSSara Sharon 		mvmvif->cab_queue = queue;
2343ced19f26SSara Sharon 	} else if (!fw_has_api(&mvm->fw->ucode_capa,
2344fc07bd8cSSara Sharon 			       IWL_UCODE_TLV_API_STA_TYPE))
2345cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2346cfbc6c4cSSara Sharon 				   timeout);
234726d6c16bSSara Sharon 
234826d6c16bSSara Sharon 	return 0;
234926d6c16bSSara Sharon }
235026d6c16bSSara Sharon 
235128916a16SEmmanuel Grumbach static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
235228916a16SEmmanuel Grumbach 				    struct ieee80211_key_conf *keyconf,
235328916a16SEmmanuel Grumbach 				    bool mcast)
235428916a16SEmmanuel Grumbach {
235528916a16SEmmanuel Grumbach 	union {
235628916a16SEmmanuel Grumbach 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
235728916a16SEmmanuel Grumbach 		struct iwl_mvm_add_sta_key_cmd cmd;
235828916a16SEmmanuel Grumbach 	} u = {};
235928916a16SEmmanuel Grumbach 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
236028916a16SEmmanuel Grumbach 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
236128916a16SEmmanuel Grumbach 	__le16 key_flags;
236228916a16SEmmanuel Grumbach 	int ret, size;
236328916a16SEmmanuel Grumbach 	u32 status;
236428916a16SEmmanuel Grumbach 
236528916a16SEmmanuel Grumbach 	/* This is a valid situation for GTK removal */
236628916a16SEmmanuel Grumbach 	if (sta_id == IWL_MVM_INVALID_STA)
236728916a16SEmmanuel Grumbach 		return 0;
236828916a16SEmmanuel Grumbach 
236928916a16SEmmanuel Grumbach 	key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
237028916a16SEmmanuel Grumbach 				 STA_KEY_FLG_KEYID_MSK);
237128916a16SEmmanuel Grumbach 	key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
237228916a16SEmmanuel Grumbach 	key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
237328916a16SEmmanuel Grumbach 
237428916a16SEmmanuel Grumbach 	if (mcast)
237528916a16SEmmanuel Grumbach 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
237628916a16SEmmanuel Grumbach 
237728916a16SEmmanuel Grumbach 	/*
237828916a16SEmmanuel Grumbach 	 * The fields assigned here are in the same location at the start
237928916a16SEmmanuel Grumbach 	 * of the command, so we can do this union trick.
238028916a16SEmmanuel Grumbach 	 */
238128916a16SEmmanuel Grumbach 	u.cmd.common.key_flags = key_flags;
238228916a16SEmmanuel Grumbach 	u.cmd.common.key_offset = keyconf->hw_key_idx;
238328916a16SEmmanuel Grumbach 	u.cmd.common.sta_id = sta_id;
238428916a16SEmmanuel Grumbach 
238528916a16SEmmanuel Grumbach 	size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
238628916a16SEmmanuel Grumbach 
238728916a16SEmmanuel Grumbach 	status = ADD_STA_SUCCESS;
238828916a16SEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
238928916a16SEmmanuel Grumbach 					  &status);
239028916a16SEmmanuel Grumbach 
239128916a16SEmmanuel Grumbach 	switch (status) {
239228916a16SEmmanuel Grumbach 	case ADD_STA_SUCCESS:
239328916a16SEmmanuel Grumbach 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
239428916a16SEmmanuel Grumbach 		break;
239528916a16SEmmanuel Grumbach 	default:
239628916a16SEmmanuel Grumbach 		ret = -EIO;
239728916a16SEmmanuel Grumbach 		IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
239828916a16SEmmanuel Grumbach 		break;
239928916a16SEmmanuel Grumbach 	}
240028916a16SEmmanuel Grumbach 
240128916a16SEmmanuel Grumbach 	return ret;
240228916a16SEmmanuel Grumbach }
240328916a16SEmmanuel Grumbach 
240426d6c16bSSara Sharon /*
240526d6c16bSSara Sharon  * Send the FW a request to remove the station from it's internal data
240626d6c16bSSara Sharon  * structures, and in addition remove it from the local data structure.
240726d6c16bSSara Sharon  */
240826d6c16bSSara Sharon int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
240926d6c16bSSara Sharon {
241026d6c16bSSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
241126d6c16bSSara Sharon 	int ret;
241226d6c16bSSara Sharon 
241326d6c16bSSara Sharon 	lockdep_assert_held(&mvm->mutex);
241426d6c16bSSara Sharon 
2415d49394a1SSara Sharon 	iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true, 0);
2416d49394a1SSara Sharon 
2417cfbc6c4cSSara Sharon 	iwl_mvm_disable_txq(mvm, NULL, mvmvif->cab_queue, 0, 0);
241826d6c16bSSara Sharon 
241926d6c16bSSara Sharon 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
242026d6c16bSSara Sharon 	if (ret)
242126d6c16bSSara Sharon 		IWL_WARN(mvm, "Failed sending remove station\n");
242226d6c16bSSara Sharon 
242326d6c16bSSara Sharon 	return ret;
242426d6c16bSSara Sharon }
242526d6c16bSSara Sharon 
2426e705c121SKalle Valo #define IWL_MAX_RX_BA_SESSIONS 16
2427e705c121SKalle Valo 
2428b915c101SSara Sharon static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
242910b2b201SSara Sharon {
24306b2dbce5SEmmanuel Grumbach 	struct iwl_mvm_rss_sync_notif notif = {
2431b915c101SSara Sharon 		.metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
2432b915c101SSara Sharon 		.metadata.sync = 1,
2433b915c101SSara Sharon 		.delba.baid = baid,
2434b915c101SSara Sharon 	};
2435b915c101SSara Sharon 	iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
243610b2b201SSara Sharon };
243710b2b201SSara Sharon 
2438b915c101SSara Sharon static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2439b915c101SSara Sharon 				 struct iwl_mvm_baid_data *data)
2440b915c101SSara Sharon {
2441b915c101SSara Sharon 	int i;
2442b915c101SSara Sharon 
2443b915c101SSara Sharon 	iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2444b915c101SSara Sharon 
2445b915c101SSara Sharon 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2446b915c101SSara Sharon 		int j;
2447b915c101SSara Sharon 		struct iwl_mvm_reorder_buffer *reorder_buf =
2448b915c101SSara Sharon 			&data->reorder_buf[i];
2449dfdddd92SJohannes Berg 		struct iwl_mvm_reorder_buf_entry *entries =
2450dfdddd92SJohannes Berg 			&data->entries[i * data->entries_per_queue];
2451b915c101SSara Sharon 
24520690405fSSara Sharon 		spin_lock_bh(&reorder_buf->lock);
24530690405fSSara Sharon 		if (likely(!reorder_buf->num_stored)) {
24540690405fSSara Sharon 			spin_unlock_bh(&reorder_buf->lock);
2455b915c101SSara Sharon 			continue;
24560690405fSSara Sharon 		}
2457b915c101SSara Sharon 
2458b915c101SSara Sharon 		/*
2459b915c101SSara Sharon 		 * This shouldn't happen in regular DELBA since the internal
2460b915c101SSara Sharon 		 * delBA notification should trigger a release of all frames in
2461b915c101SSara Sharon 		 * the reorder buffer.
2462b915c101SSara Sharon 		 */
2463b915c101SSara Sharon 		WARN_ON(1);
2464b915c101SSara Sharon 
2465b915c101SSara Sharon 		for (j = 0; j < reorder_buf->buf_size; j++)
2466dfdddd92SJohannes Berg 			__skb_queue_purge(&entries[j].e.frames);
24670690405fSSara Sharon 		/*
24680690405fSSara Sharon 		 * Prevent timer re-arm. This prevents a very far fetched case
24690690405fSSara Sharon 		 * where we timed out on the notification. There may be prior
24700690405fSSara Sharon 		 * RX frames pending in the RX queue before the notification
24710690405fSSara Sharon 		 * that might get processed between now and the actual deletion
24720690405fSSara Sharon 		 * and we would re-arm the timer although we are deleting the
24730690405fSSara Sharon 		 * reorder buffer.
24740690405fSSara Sharon 		 */
24750690405fSSara Sharon 		reorder_buf->removed = true;
24760690405fSSara Sharon 		spin_unlock_bh(&reorder_buf->lock);
24770690405fSSara Sharon 		del_timer_sync(&reorder_buf->reorder_timer);
2478b915c101SSara Sharon 	}
2479b915c101SSara Sharon }
2480b915c101SSara Sharon 
2481b915c101SSara Sharon static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2482b915c101SSara Sharon 					struct iwl_mvm_baid_data *data,
2483514c3069SLuca Coelho 					u16 ssn, u16 buf_size)
2484b915c101SSara Sharon {
2485b915c101SSara Sharon 	int i;
2486b915c101SSara Sharon 
2487b915c101SSara Sharon 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2488b915c101SSara Sharon 		struct iwl_mvm_reorder_buffer *reorder_buf =
2489b915c101SSara Sharon 			&data->reorder_buf[i];
2490dfdddd92SJohannes Berg 		struct iwl_mvm_reorder_buf_entry *entries =
2491dfdddd92SJohannes Berg 			&data->entries[i * data->entries_per_queue];
2492b915c101SSara Sharon 		int j;
2493b915c101SSara Sharon 
2494b915c101SSara Sharon 		reorder_buf->num_stored = 0;
2495b915c101SSara Sharon 		reorder_buf->head_sn = ssn;
2496b915c101SSara Sharon 		reorder_buf->buf_size = buf_size;
24970690405fSSara Sharon 		/* rx reorder timer */
24988cef5344SKees Cook 		timer_setup(&reorder_buf->reorder_timer,
24998cef5344SKees Cook 			    iwl_mvm_reorder_timer_expired, 0);
25000690405fSSara Sharon 		spin_lock_init(&reorder_buf->lock);
25010690405fSSara Sharon 		reorder_buf->mvm = mvm;
2502b915c101SSara Sharon 		reorder_buf->queue = i;
25035d43eab6SSara Sharon 		reorder_buf->valid = false;
2504b915c101SSara Sharon 		for (j = 0; j < reorder_buf->buf_size; j++)
2505dfdddd92SJohannes Berg 			__skb_queue_head_init(&entries[j].e.frames);
2506b915c101SSara Sharon 	}
250710b2b201SSara Sharon }
250810b2b201SSara Sharon 
2509e705c121SKalle Valo int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2510514c3069SLuca Coelho 		       int tid, u16 ssn, bool start, u16 buf_size, u16 timeout)
2511e705c121SKalle Valo {
2512e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2513e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
251410b2b201SSara Sharon 	struct iwl_mvm_baid_data *baid_data = NULL;
2515e705c121SKalle Valo 	int ret;
2516e705c121SKalle Valo 	u32 status;
2517e705c121SKalle Valo 
2518e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2519e705c121SKalle Valo 
2520e705c121SKalle Valo 	if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
2521e705c121SKalle Valo 		IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2522e705c121SKalle Valo 		return -ENOSPC;
2523e705c121SKalle Valo 	}
2524e705c121SKalle Valo 
252510b2b201SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) && start) {
2526dfdddd92SJohannes Berg 		u16 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2527dfdddd92SJohannes Berg 
2528dfdddd92SJohannes Berg 		/* sparse doesn't like the __align() so don't check */
2529dfdddd92SJohannes Berg #ifndef __CHECKER__
2530dfdddd92SJohannes Berg 		/*
2531dfdddd92SJohannes Berg 		 * The division below will be OK if either the cache line size
2532dfdddd92SJohannes Berg 		 * can be divided by the entry size (ALIGN will round up) or if
2533dfdddd92SJohannes Berg 		 * if the entry size can be divided by the cache line size, in
2534dfdddd92SJohannes Berg 		 * which case the ALIGN() will do nothing.
2535dfdddd92SJohannes Berg 		 */
2536dfdddd92SJohannes Berg 		BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2537dfdddd92SJohannes Berg 			     sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2538dfdddd92SJohannes Berg #endif
2539dfdddd92SJohannes Berg 
2540dfdddd92SJohannes Berg 		/*
2541dfdddd92SJohannes Berg 		 * Upward align the reorder buffer size to fill an entire cache
2542dfdddd92SJohannes Berg 		 * line for each queue, to avoid sharing cache lines between
2543dfdddd92SJohannes Berg 		 * different queues.
2544dfdddd92SJohannes Berg 		 */
2545dfdddd92SJohannes Berg 		reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2546dfdddd92SJohannes Berg 
254710b2b201SSara Sharon 		/*
254810b2b201SSara Sharon 		 * Allocate here so if allocation fails we can bail out early
254910b2b201SSara Sharon 		 * before starting the BA session in the firmware
255010b2b201SSara Sharon 		 */
2551b915c101SSara Sharon 		baid_data = kzalloc(sizeof(*baid_data) +
2552b915c101SSara Sharon 				    mvm->trans->num_rx_queues *
2553dfdddd92SJohannes Berg 				    reorder_buf_size,
2554b915c101SSara Sharon 				    GFP_KERNEL);
255510b2b201SSara Sharon 		if (!baid_data)
255610b2b201SSara Sharon 			return -ENOMEM;
2557dfdddd92SJohannes Berg 
2558dfdddd92SJohannes Berg 		/*
2559dfdddd92SJohannes Berg 		 * This division is why we need the above BUILD_BUG_ON(),
2560dfdddd92SJohannes Berg 		 * if that doesn't hold then this will not be right.
2561dfdddd92SJohannes Berg 		 */
2562dfdddd92SJohannes Berg 		baid_data->entries_per_queue =
2563dfdddd92SJohannes Berg 			reorder_buf_size / sizeof(baid_data->entries[0]);
256410b2b201SSara Sharon 	}
256510b2b201SSara Sharon 
2566e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2567e705c121SKalle Valo 	cmd.sta_id = mvm_sta->sta_id;
2568e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
2569e705c121SKalle Valo 	if (start) {
2570e705c121SKalle Valo 		cmd.add_immediate_ba_tid = (u8) tid;
2571e705c121SKalle Valo 		cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
2572514c3069SLuca Coelho 		cmd.rx_ba_window = cpu_to_le16(buf_size);
2573e705c121SKalle Valo 	} else {
2574e705c121SKalle Valo 		cmd.remove_immediate_ba_tid = (u8) tid;
2575e705c121SKalle Valo 	}
2576e705c121SKalle Valo 	cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2577e705c121SKalle Valo 				  STA_MODIFY_REMOVE_BA_TID;
2578e705c121SKalle Valo 
2579e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
2580854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2581854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
2582e705c121SKalle Valo 					  &cmd, &status);
2583e705c121SKalle Valo 	if (ret)
258410b2b201SSara Sharon 		goto out_free;
2585e705c121SKalle Valo 
2586837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2587e705c121SKalle Valo 	case ADD_STA_SUCCESS:
258835263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2589e705c121SKalle Valo 			     start ? "start" : "stopp");
2590e705c121SKalle Valo 		break;
2591e705c121SKalle Valo 	case ADD_STA_IMMEDIATE_BA_FAILURE:
2592e705c121SKalle Valo 		IWL_WARN(mvm, "RX BA Session refused by fw\n");
2593e705c121SKalle Valo 		ret = -ENOSPC;
2594e705c121SKalle Valo 		break;
2595e705c121SKalle Valo 	default:
2596e705c121SKalle Valo 		ret = -EIO;
2597e705c121SKalle Valo 		IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2598e705c121SKalle Valo 			start ? "start" : "stopp", status);
2599e705c121SKalle Valo 		break;
2600e705c121SKalle Valo 	}
2601e705c121SKalle Valo 
260210b2b201SSara Sharon 	if (ret)
260310b2b201SSara Sharon 		goto out_free;
260410b2b201SSara Sharon 
260510b2b201SSara Sharon 	if (start) {
260610b2b201SSara Sharon 		u8 baid;
260710b2b201SSara Sharon 
2608e705c121SKalle Valo 		mvm->rx_ba_sessions++;
260910b2b201SSara Sharon 
261010b2b201SSara Sharon 		if (!iwl_mvm_has_new_rx_api(mvm))
261110b2b201SSara Sharon 			return 0;
261210b2b201SSara Sharon 
261310b2b201SSara Sharon 		if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
261410b2b201SSara Sharon 			ret = -EINVAL;
261510b2b201SSara Sharon 			goto out_free;
261610b2b201SSara Sharon 		}
261710b2b201SSara Sharon 		baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
261810b2b201SSara Sharon 			    IWL_ADD_STA_BAID_SHIFT);
261910b2b201SSara Sharon 		baid_data->baid = baid;
262010b2b201SSara Sharon 		baid_data->timeout = timeout;
262110b2b201SSara Sharon 		baid_data->last_rx = jiffies;
26228cef5344SKees Cook 		baid_data->rcu_ptr = &mvm->baid_map[baid];
26238cef5344SKees Cook 		timer_setup(&baid_data->session_timer,
26248cef5344SKees Cook 			    iwl_mvm_rx_agg_session_expired, 0);
262510b2b201SSara Sharon 		baid_data->mvm = mvm;
262610b2b201SSara Sharon 		baid_data->tid = tid;
262710b2b201SSara Sharon 		baid_data->sta_id = mvm_sta->sta_id;
262810b2b201SSara Sharon 
262910b2b201SSara Sharon 		mvm_sta->tid_to_baid[tid] = baid;
263010b2b201SSara Sharon 		if (timeout)
263110b2b201SSara Sharon 			mod_timer(&baid_data->session_timer,
263210b2b201SSara Sharon 				  TU_TO_EXP_TIME(timeout * 2));
263310b2b201SSara Sharon 
26343f1c4c58SSara Sharon 		iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size);
263510b2b201SSara Sharon 		/*
263610b2b201SSara Sharon 		 * protect the BA data with RCU to cover a case where our
263710b2b201SSara Sharon 		 * internal RX sync mechanism will timeout (not that it's
263810b2b201SSara Sharon 		 * supposed to happen) and we will free the session data while
263910b2b201SSara Sharon 		 * RX is being processed in parallel
264010b2b201SSara Sharon 		 */
264135263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
264235263a03SSara Sharon 			     mvm_sta->sta_id, tid, baid);
264310b2b201SSara Sharon 		WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
264410b2b201SSara Sharon 		rcu_assign_pointer(mvm->baid_map[baid], baid_data);
264560dec523SSara Sharon 	} else  {
264610b2b201SSara Sharon 		u8 baid = mvm_sta->tid_to_baid[tid];
264710b2b201SSara Sharon 
264860dec523SSara Sharon 		if (mvm->rx_ba_sessions > 0)
2649e705c121SKalle Valo 			/* check that restart flow didn't zero the counter */
2650e705c121SKalle Valo 			mvm->rx_ba_sessions--;
265110b2b201SSara Sharon 		if (!iwl_mvm_has_new_rx_api(mvm))
265210b2b201SSara Sharon 			return 0;
2653e705c121SKalle Valo 
265410b2b201SSara Sharon 		if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
265510b2b201SSara Sharon 			return -EINVAL;
265610b2b201SSara Sharon 
265710b2b201SSara Sharon 		baid_data = rcu_access_pointer(mvm->baid_map[baid]);
265810b2b201SSara Sharon 		if (WARN_ON(!baid_data))
265910b2b201SSara Sharon 			return -EINVAL;
266010b2b201SSara Sharon 
266110b2b201SSara Sharon 		/* synchronize all rx queues so we can safely delete */
2662b915c101SSara Sharon 		iwl_mvm_free_reorder(mvm, baid_data);
266310b2b201SSara Sharon 		del_timer_sync(&baid_data->session_timer);
266410b2b201SSara Sharon 		RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
266510b2b201SSara Sharon 		kfree_rcu(baid_data, rcu_head);
266635263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
266710b2b201SSara Sharon 	}
266810b2b201SSara Sharon 	return 0;
266910b2b201SSara Sharon 
267010b2b201SSara Sharon out_free:
267110b2b201SSara Sharon 	kfree(baid_data);
2672e705c121SKalle Valo 	return ret;
2673e705c121SKalle Valo }
2674e705c121SKalle Valo 
26759794c64fSLiad Kaufman int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2676e705c121SKalle Valo 		       int tid, u8 queue, bool start)
2677e705c121SKalle Valo {
2678e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2679e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
2680e705c121SKalle Valo 	int ret;
2681e705c121SKalle Valo 	u32 status;
2682e705c121SKalle Valo 
2683e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2684e705c121SKalle Valo 
2685e705c121SKalle Valo 	if (start) {
2686e705c121SKalle Valo 		mvm_sta->tfd_queue_msk |= BIT(queue);
2687e705c121SKalle Valo 		mvm_sta->tid_disable_agg &= ~BIT(tid);
2688e705c121SKalle Valo 	} else {
2689cf961e16SLiad Kaufman 		/* In DQA-mode the queue isn't removed on agg termination */
2690e705c121SKalle Valo 		mvm_sta->tid_disable_agg |= BIT(tid);
2691e705c121SKalle Valo 	}
2692e705c121SKalle Valo 
2693e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2694e705c121SKalle Valo 	cmd.sta_id = mvm_sta->sta_id;
2695e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
2696bb49701bSSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm))
2697bb49701bSSara Sharon 		cmd.modify_mask = STA_MODIFY_QUEUES;
2698bb49701bSSara Sharon 	cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
2699e705c121SKalle Valo 	cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2700e705c121SKalle Valo 	cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2701e705c121SKalle Valo 
2702e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
2703854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2704854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
2705e705c121SKalle Valo 					  &cmd, &status);
2706e705c121SKalle Valo 	if (ret)
2707e705c121SKalle Valo 		return ret;
2708e705c121SKalle Valo 
2709837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2710e705c121SKalle Valo 	case ADD_STA_SUCCESS:
2711e705c121SKalle Valo 		break;
2712e705c121SKalle Valo 	default:
2713e705c121SKalle Valo 		ret = -EIO;
2714e705c121SKalle Valo 		IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2715e705c121SKalle Valo 			start ? "start" : "stopp", status);
2716e705c121SKalle Valo 		break;
2717e705c121SKalle Valo 	}
2718e705c121SKalle Valo 
2719e705c121SKalle Valo 	return ret;
2720e705c121SKalle Valo }
2721e705c121SKalle Valo 
2722e705c121SKalle Valo const u8 tid_to_mac80211_ac[] = {
2723e705c121SKalle Valo 	IEEE80211_AC_BE,
2724e705c121SKalle Valo 	IEEE80211_AC_BK,
2725e705c121SKalle Valo 	IEEE80211_AC_BK,
2726e705c121SKalle Valo 	IEEE80211_AC_BE,
2727e705c121SKalle Valo 	IEEE80211_AC_VI,
2728e705c121SKalle Valo 	IEEE80211_AC_VI,
2729e705c121SKalle Valo 	IEEE80211_AC_VO,
2730e705c121SKalle Valo 	IEEE80211_AC_VO,
27319794c64fSLiad Kaufman 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2732e705c121SKalle Valo };
2733e705c121SKalle Valo 
2734e705c121SKalle Valo static const u8 tid_to_ucode_ac[] = {
2735e705c121SKalle Valo 	AC_BE,
2736e705c121SKalle Valo 	AC_BK,
2737e705c121SKalle Valo 	AC_BK,
2738e705c121SKalle Valo 	AC_BE,
2739e705c121SKalle Valo 	AC_VI,
2740e705c121SKalle Valo 	AC_VI,
2741e705c121SKalle Valo 	AC_VO,
2742e705c121SKalle Valo 	AC_VO,
2743e705c121SKalle Valo };
2744e705c121SKalle Valo 
2745e705c121SKalle Valo int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2746e705c121SKalle Valo 			     struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2747e705c121SKalle Valo {
2748e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2749e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data;
2750dd32162dSLiad Kaufman 	u16 normalized_ssn;
2751b0d795a9SMordechay Goodstein 	u16 txq_id;
2752e705c121SKalle Valo 	int ret;
2753e705c121SKalle Valo 
2754e705c121SKalle Valo 	if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2755e705c121SKalle Valo 		return -EINVAL;
2756e705c121SKalle Valo 
2757bd800e41SNaftali Goldstein 	if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
2758bd800e41SNaftali Goldstein 	    mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2759bd800e41SNaftali Goldstein 		IWL_ERR(mvm,
2760bd800e41SNaftali Goldstein 			"Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
2761e705c121SKalle Valo 			mvmsta->tid_data[tid].state);
2762e705c121SKalle Valo 		return -ENXIO;
2763e705c121SKalle Valo 	}
2764e705c121SKalle Valo 
2765e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2766e705c121SKalle Valo 
2767bd8f3fc6SLiad Kaufman 	if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE &&
2768bd8f3fc6SLiad Kaufman 	    iwl_mvm_has_new_tx_api(mvm)) {
2769bd8f3fc6SLiad Kaufman 		u8 ac = tid_to_mac80211_ac[tid];
2770bd8f3fc6SLiad Kaufman 
2771bd8f3fc6SLiad Kaufman 		ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
2772bd8f3fc6SLiad Kaufman 		if (ret)
2773bd8f3fc6SLiad Kaufman 			return ret;
2774bd8f3fc6SLiad Kaufman 	}
2775bd8f3fc6SLiad Kaufman 
2776e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2777e705c121SKalle Valo 
2778e705c121SKalle Valo 	/* possible race condition - we entered D0i3 while starting agg */
2779e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
2780e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
2781e705c121SKalle Valo 		IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
2782e705c121SKalle Valo 		return -EIO;
2783e705c121SKalle Valo 	}
2784e705c121SKalle Valo 
2785cf961e16SLiad Kaufman 	/*
2786cf961e16SLiad Kaufman 	 * Note the possible cases:
27874a6d2e52SAvraham Stern 	 *  1. An enabled TXQ - TXQ needs to become agg'ed
27884a6d2e52SAvraham Stern 	 *  2. The TXQ hasn't yet been enabled, so find a free one and mark
27894a6d2e52SAvraham Stern 	 *	it as reserved
2790cf961e16SLiad Kaufman 	 */
2791cf961e16SLiad Kaufman 	txq_id = mvmsta->tid_data[tid].txq_id;
279234e10860SSara Sharon 	if (txq_id == IWL_MVM_INVALID_QUEUE) {
2793b0d795a9SMordechay Goodstein 		ret = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2794c8f54701SJohannes Berg 					      IWL_MVM_DQA_MIN_DATA_QUEUE,
2795c8f54701SJohannes Berg 					      IWL_MVM_DQA_MAX_DATA_QUEUE);
2796b0d795a9SMordechay Goodstein 		if (ret < 0) {
2797e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to allocate agg queue\n");
2798f3f240f9SJohannes Berg 			goto out;
2799e705c121SKalle Valo 		}
2800cf961e16SLiad Kaufman 
2801b0d795a9SMordechay Goodstein 		txq_id = ret;
2802b0d795a9SMordechay Goodstein 
2803cf961e16SLiad Kaufman 		/* TXQ hasn't yet been enabled, so mark it only as reserved */
2804cf961e16SLiad Kaufman 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2805b0d795a9SMordechay Goodstein 	} else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) {
2806b0d795a9SMordechay Goodstein 		ret = -ENXIO;
2807b0d795a9SMordechay Goodstein 		IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n",
2808b0d795a9SMordechay Goodstein 			tid, IWL_MAX_HW_QUEUES - 1);
2809b0d795a9SMordechay Goodstein 		goto out;
2810b0d795a9SMordechay Goodstein 
28114a6d2e52SAvraham Stern 	} else if (unlikely(mvm->queue_info[txq_id].status ==
28124a6d2e52SAvraham Stern 			    IWL_MVM_QUEUE_SHARED)) {
28134a6d2e52SAvraham Stern 		ret = -ENXIO;
28144a6d2e52SAvraham Stern 		IWL_DEBUG_TX_QUEUES(mvm,
28154a6d2e52SAvraham Stern 				    "Can't start tid %d agg on shared queue!\n",
28164a6d2e52SAvraham Stern 				    tid);
2817f3f240f9SJohannes Berg 		goto out;
2818cf961e16SLiad Kaufman 	}
28199f9af3d7SLiad Kaufman 
2820cf961e16SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm,
2821cf961e16SLiad Kaufman 			    "AGG for tid %d will be on queue #%d\n",
2822cf961e16SLiad Kaufman 			    tid, txq_id);
2823cf961e16SLiad Kaufman 
2824e705c121SKalle Valo 	tid_data = &mvmsta->tid_data[tid];
2825e705c121SKalle Valo 	tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2826e705c121SKalle Valo 	tid_data->txq_id = txq_id;
2827e705c121SKalle Valo 	*ssn = tid_data->ssn;
2828e705c121SKalle Valo 
2829e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm,
2830e705c121SKalle Valo 			    "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2831e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2832e705c121SKalle Valo 			    tid_data->next_reclaimed);
2833e705c121SKalle Valo 
2834dd32162dSLiad Kaufman 	/*
28352f7a3863SLuca Coelho 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
2836dd32162dSLiad Kaufman 	 * to align the wrap around of ssn so we compare relevant values.
2837dd32162dSLiad Kaufman 	 */
2838dd32162dSLiad Kaufman 	normalized_ssn = tid_data->ssn;
2839dd32162dSLiad Kaufman 	if (mvm->trans->cfg->gen2)
2840dd32162dSLiad Kaufman 		normalized_ssn &= 0xff;
2841dd32162dSLiad Kaufman 
2842dd32162dSLiad Kaufman 	if (normalized_ssn == tid_data->next_reclaimed) {
2843e705c121SKalle Valo 		tid_data->state = IWL_AGG_STARTING;
2844e705c121SKalle Valo 		ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2845e705c121SKalle Valo 	} else {
2846e705c121SKalle Valo 		tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2847e705c121SKalle Valo 	}
2848e705c121SKalle Valo 
2849e705c121SKalle Valo 	ret = 0;
2850e705c121SKalle Valo 
28519f9af3d7SLiad Kaufman out:
2852e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2853e705c121SKalle Valo 
2854e705c121SKalle Valo 	return ret;
2855e705c121SKalle Valo }
2856e705c121SKalle Valo 
2857e705c121SKalle Valo int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2858514c3069SLuca Coelho 			    struct ieee80211_sta *sta, u16 tid, u16 buf_size,
2859bb81bb68SEmmanuel Grumbach 			    bool amsdu)
2860e705c121SKalle Valo {
2861e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2862e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2863e705c121SKalle Valo 	unsigned int wdg_timeout =
2864e705c121SKalle Valo 		iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
2865eea76c36SEmmanuel Grumbach 	int queue, ret;
2866cf961e16SLiad Kaufman 	bool alloc_queue = true;
28679f9af3d7SLiad Kaufman 	enum iwl_mvm_queue_status queue_status;
2868e705c121SKalle Valo 	u16 ssn;
2869e705c121SKalle Valo 
2870eea76c36SEmmanuel Grumbach 	struct iwl_trans_txq_scd_cfg cfg = {
2871eea76c36SEmmanuel Grumbach 		.sta_id = mvmsta->sta_id,
2872eea76c36SEmmanuel Grumbach 		.tid = tid,
2873eea76c36SEmmanuel Grumbach 		.frame_limit = buf_size,
2874eea76c36SEmmanuel Grumbach 		.aggregate = true,
2875eea76c36SEmmanuel Grumbach 	};
2876eea76c36SEmmanuel Grumbach 
2877ecaf71deSGregory Greenman 	/*
2878ecaf71deSGregory Greenman 	 * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
2879ecaf71deSGregory Greenman 	 * manager, so this function should never be called in this case.
2880ecaf71deSGregory Greenman 	 */
28814243edb4SEmmanuel Grumbach 	if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
2882ecaf71deSGregory Greenman 		return -EINVAL;
2883ecaf71deSGregory Greenman 
2884e705c121SKalle Valo 	BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2885e705c121SKalle Valo 		     != IWL_MAX_TID_COUNT);
2886e705c121SKalle Valo 
2887e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2888e705c121SKalle Valo 	ssn = tid_data->ssn;
2889e705c121SKalle Valo 	queue = tid_data->txq_id;
2890e705c121SKalle Valo 	tid_data->state = IWL_AGG_ON;
2891e705c121SKalle Valo 	mvmsta->agg_tids |= BIT(tid);
2892e705c121SKalle Valo 	tid_data->ssn = 0xffff;
2893bb81bb68SEmmanuel Grumbach 	tid_data->amsdu_in_ampdu_allowed = amsdu;
2894e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2895e705c121SKalle Valo 
289634e10860SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
289734e10860SSara Sharon 		/*
28980ec9257bSSara Sharon 		 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
28990ec9257bSSara Sharon 		 * would have failed, so if we are here there is no need to
29000ec9257bSSara Sharon 		 * allocate a queue.
29010ec9257bSSara Sharon 		 * However, if aggregation size is different than the default
29020ec9257bSSara Sharon 		 * size, the scheduler should be reconfigured.
29030ec9257bSSara Sharon 		 * We cannot do this with the new TX API, so return unsupported
29040ec9257bSSara Sharon 		 * for now, until it will be offloaded to firmware..
29050ec9257bSSara Sharon 		 * Note that if SCD default value changes - this condition
29060ec9257bSSara Sharon 		 * should be updated as well.
290734e10860SSara Sharon 		 */
29080ec9257bSSara Sharon 		if (buf_size < IWL_FRAME_LIMIT)
290934e10860SSara Sharon 			return -ENOTSUPP;
291034e10860SSara Sharon 
291134e10860SSara Sharon 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
291234e10860SSara Sharon 		if (ret)
291334e10860SSara Sharon 			return -EIO;
291434e10860SSara Sharon 		goto out;
291534e10860SSara Sharon 	}
291634e10860SSara Sharon 
2917eea76c36SEmmanuel Grumbach 	cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
2918e705c121SKalle Valo 
29199f9af3d7SLiad Kaufman 	queue_status = mvm->queue_info[queue].status;
29209f9af3d7SLiad Kaufman 
2921cf961e16SLiad Kaufman 	/* Maybe there is no need to even alloc a queue... */
2922cf961e16SLiad Kaufman 	if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2923cf961e16SLiad Kaufman 		alloc_queue = false;
2924cf961e16SLiad Kaufman 
2925cf961e16SLiad Kaufman 	/*
2926cf961e16SLiad Kaufman 	 * Only reconfig the SCD for the queue if the window size has
2927cf961e16SLiad Kaufman 	 * changed from current (become smaller)
2928cf961e16SLiad Kaufman 	 */
29290ec9257bSSara Sharon 	if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
2930cf961e16SLiad Kaufman 		/*
2931cf961e16SLiad Kaufman 		 * If reconfiguring an existing queue, it first must be
2932cf961e16SLiad Kaufman 		 * drained
2933cf961e16SLiad Kaufman 		 */
2934a1a57877SSara Sharon 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2935cf961e16SLiad Kaufman 						     BIT(queue));
2936cf961e16SLiad Kaufman 		if (ret) {
2937cf961e16SLiad Kaufman 			IWL_ERR(mvm,
2938cf961e16SLiad Kaufman 				"Error draining queue before reconfig\n");
2939cf961e16SLiad Kaufman 			return ret;
2940cf961e16SLiad Kaufman 		}
2941cf961e16SLiad Kaufman 
2942cf961e16SLiad Kaufman 		ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2943cf961e16SLiad Kaufman 					   mvmsta->sta_id, tid,
2944cf961e16SLiad Kaufman 					   buf_size, ssn);
2945cf961e16SLiad Kaufman 		if (ret) {
2946cf961e16SLiad Kaufman 			IWL_ERR(mvm,
2947cf961e16SLiad Kaufman 				"Error reconfiguring TXQ #%d\n", queue);
2948cf961e16SLiad Kaufman 			return ret;
2949cf961e16SLiad Kaufman 		}
2950cf961e16SLiad Kaufman 	}
2951cf961e16SLiad Kaufman 
2952cf961e16SLiad Kaufman 	if (alloc_queue)
2953cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, sta, queue, ssn,
2954cf961e16SLiad Kaufman 				   &cfg, wdg_timeout);
2955e705c121SKalle Valo 
29569f9af3d7SLiad Kaufman 	/* Send ADD_STA command to enable aggs only if the queue isn't shared */
29579f9af3d7SLiad Kaufman 	if (queue_status != IWL_MVM_QUEUE_SHARED) {
2958e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2959e705c121SKalle Valo 		if (ret)
2960e705c121SKalle Valo 			return -EIO;
29619f9af3d7SLiad Kaufman 	}
2962e705c121SKalle Valo 
2963e705c121SKalle Valo 	/* No need to mark as reserved */
2964cf961e16SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
2965e705c121SKalle Valo 
296634e10860SSara Sharon out:
2967e705c121SKalle Valo 	/*
2968e705c121SKalle Valo 	 * Even though in theory the peer could have different
2969e705c121SKalle Valo 	 * aggregation reorder buffer sizes for different sessions,
2970e705c121SKalle Valo 	 * our ucode doesn't allow for that and has a global limit
2971e705c121SKalle Valo 	 * for each station. Therefore, use the minimum of all the
2972e705c121SKalle Valo 	 * aggregation sessions and our default value.
2973e705c121SKalle Valo 	 */
2974e705c121SKalle Valo 	mvmsta->max_agg_bufsize =
2975e705c121SKalle Valo 		min(mvmsta->max_agg_bufsize, buf_size);
2976ecaf71deSGregory Greenman 	mvmsta->lq_sta.rs_drv.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2977e705c121SKalle Valo 
2978e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2979e705c121SKalle Valo 		     sta->addr, tid);
2980e705c121SKalle Valo 
2981cd4d6b0bSGregory Greenman 	return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.rs_drv.lq);
2982e705c121SKalle Valo }
2983e705c121SKalle Valo 
298434e10860SSara Sharon static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
298534e10860SSara Sharon 					struct iwl_mvm_sta *mvmsta,
29864b387906SAvraham Stern 					struct iwl_mvm_tid_data *tid_data)
298734e10860SSara Sharon {
29884b387906SAvraham Stern 	u16 txq_id = tid_data->txq_id;
29894b387906SAvraham Stern 
2990f3f240f9SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
2991f3f240f9SJohannes Berg 
299234e10860SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm))
299334e10860SSara Sharon 		return;
299434e10860SSara Sharon 
299534e10860SSara Sharon 	/*
299634e10860SSara Sharon 	 * The TXQ is marked as reserved only if no traffic came through yet
299734e10860SSara Sharon 	 * This means no traffic has been sent on this TID (agg'd or not), so
299834e10860SSara Sharon 	 * we no longer have use for the queue. Since it hasn't even been
299934e10860SSara Sharon 	 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
300034e10860SSara Sharon 	 * free.
300134e10860SSara Sharon 	 */
30024b387906SAvraham Stern 	if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
300334e10860SSara Sharon 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
30044b387906SAvraham Stern 		tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
30054b387906SAvraham Stern 	}
300634e10860SSara Sharon }
300734e10860SSara Sharon 
3008e705c121SKalle Valo int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3009e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid)
3010e705c121SKalle Valo {
3011e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3012e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3013e705c121SKalle Valo 	u16 txq_id;
3014e705c121SKalle Valo 	int err;
3015e705c121SKalle Valo 
3016e705c121SKalle Valo 	/*
3017e705c121SKalle Valo 	 * If mac80211 is cleaning its state, then say that we finished since
3018e705c121SKalle Valo 	 * our state has been cleared anyway.
3019e705c121SKalle Valo 	 */
3020e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3021e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3022e705c121SKalle Valo 		return 0;
3023e705c121SKalle Valo 	}
3024e705c121SKalle Valo 
3025e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3026e705c121SKalle Valo 
3027e705c121SKalle Valo 	txq_id = tid_data->txq_id;
3028e705c121SKalle Valo 
3029e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
3030e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
3031e705c121SKalle Valo 
3032e705c121SKalle Valo 	mvmsta->agg_tids &= ~BIT(tid);
3033e705c121SKalle Valo 
30344b387906SAvraham Stern 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3035e705c121SKalle Valo 
3036e705c121SKalle Valo 	switch (tid_data->state) {
3037e705c121SKalle Valo 	case IWL_AGG_ON:
3038e705c121SKalle Valo 		tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3039e705c121SKalle Valo 
3040e705c121SKalle Valo 		IWL_DEBUG_TX_QUEUES(mvm,
3041e705c121SKalle Valo 				    "ssn = %d, next_recl = %d\n",
3042e705c121SKalle Valo 				    tid_data->ssn, tid_data->next_reclaimed);
3043e705c121SKalle Valo 
3044e705c121SKalle Valo 		tid_data->ssn = 0xffff;
3045e705c121SKalle Valo 		tid_data->state = IWL_AGG_OFF;
3046e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
3047e705c121SKalle Valo 
3048e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3049e705c121SKalle Valo 
3050e705c121SKalle Valo 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3051e705c121SKalle Valo 		return 0;
3052e705c121SKalle Valo 	case IWL_AGG_STARTING:
3053e705c121SKalle Valo 	case IWL_EMPTYING_HW_QUEUE_ADDBA:
3054e705c121SKalle Valo 		/*
3055e705c121SKalle Valo 		 * The agg session has been stopped before it was set up. This
3056e705c121SKalle Valo 		 * can happen when the AddBA timer times out for example.
3057e705c121SKalle Valo 		 */
3058e705c121SKalle Valo 
3059e705c121SKalle Valo 		/* No barriers since we are under mutex */
3060e705c121SKalle Valo 		lockdep_assert_held(&mvm->mutex);
3061e705c121SKalle Valo 
3062e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3063e705c121SKalle Valo 		tid_data->state = IWL_AGG_OFF;
3064e705c121SKalle Valo 		err = 0;
3065e705c121SKalle Valo 		break;
3066e705c121SKalle Valo 	default:
3067e705c121SKalle Valo 		IWL_ERR(mvm,
3068e705c121SKalle Valo 			"Stopping AGG while state not ON or starting for %d on %d (%d)\n",
3069e705c121SKalle Valo 			mvmsta->sta_id, tid, tid_data->state);
3070e705c121SKalle Valo 		IWL_ERR(mvm,
3071e705c121SKalle Valo 			"\ttid_data->txq_id = %d\n", tid_data->txq_id);
3072e705c121SKalle Valo 		err = -EINVAL;
3073e705c121SKalle Valo 	}
3074e705c121SKalle Valo 
3075e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3076e705c121SKalle Valo 
3077e705c121SKalle Valo 	return err;
3078e705c121SKalle Valo }
3079e705c121SKalle Valo 
3080e705c121SKalle Valo int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3081e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid)
3082e705c121SKalle Valo {
3083e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3084e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3085e705c121SKalle Valo 	u16 txq_id;
3086e705c121SKalle Valo 	enum iwl_mvm_agg_state old_state;
3087e705c121SKalle Valo 
3088e705c121SKalle Valo 	/*
3089e705c121SKalle Valo 	 * First set the agg state to OFF to avoid calling
3090e705c121SKalle Valo 	 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
3091e705c121SKalle Valo 	 */
3092e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3093e705c121SKalle Valo 	txq_id = tid_data->txq_id;
3094e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
3095e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
3096e705c121SKalle Valo 	old_state = tid_data->state;
3097e705c121SKalle Valo 	tid_data->state = IWL_AGG_OFF;
3098e705c121SKalle Valo 	mvmsta->agg_tids &= ~BIT(tid);
3099e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3100e705c121SKalle Valo 
31014b387906SAvraham Stern 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3102e705c121SKalle Valo 
3103e705c121SKalle Valo 	if (old_state >= IWL_AGG_ON) {
3104e705c121SKalle Valo 		iwl_mvm_drain_sta(mvm, mvmsta, true);
3105d167e81aSMordechai Goodstein 
3106d167e81aSMordechai Goodstein 		if (iwl_mvm_has_new_tx_api(mvm)) {
3107d167e81aSMordechai Goodstein 			if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
3108d167e81aSMordechai Goodstein 						   BIT(tid), 0))
3109d167e81aSMordechai Goodstein 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3110d167e81aSMordechai Goodstein 			iwl_trans_wait_txq_empty(mvm->trans, txq_id);
3111d167e81aSMordechai Goodstein 		} else {
3112e705c121SKalle Valo 			if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
3113e705c121SKalle Valo 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
31140b90964aSSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
3115d167e81aSMordechai Goodstein 		}
3116d6d517b7SSara Sharon 
3117e705c121SKalle Valo 		iwl_mvm_drain_sta(mvm, mvmsta, false);
3118e705c121SKalle Valo 
3119e705c121SKalle Valo 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3120e705c121SKalle Valo 	}
3121e705c121SKalle Valo 
3122e705c121SKalle Valo 	return 0;
3123e705c121SKalle Valo }
3124e705c121SKalle Valo 
3125e705c121SKalle Valo static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
3126e705c121SKalle Valo {
3127e705c121SKalle Valo 	int i, max = -1, max_offs = -1;
3128e705c121SKalle Valo 
3129e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3130e705c121SKalle Valo 
3131e705c121SKalle Valo 	/* Pick the unused key offset with the highest 'deleted'
3132e705c121SKalle Valo 	 * counter. Every time a key is deleted, all the counters
3133e705c121SKalle Valo 	 * are incremented and the one that was just deleted is
3134e705c121SKalle Valo 	 * reset to zero. Thus, the highest counter is the one
3135e705c121SKalle Valo 	 * that was deleted longest ago. Pick that one.
3136e705c121SKalle Valo 	 */
3137e705c121SKalle Valo 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3138e705c121SKalle Valo 		if (test_bit(i, mvm->fw_key_table))
3139e705c121SKalle Valo 			continue;
3140e705c121SKalle Valo 		if (mvm->fw_key_deleted[i] > max) {
3141e705c121SKalle Valo 			max = mvm->fw_key_deleted[i];
3142e705c121SKalle Valo 			max_offs = i;
3143e705c121SKalle Valo 		}
3144e705c121SKalle Valo 	}
3145e705c121SKalle Valo 
3146e705c121SKalle Valo 	if (max_offs < 0)
3147e705c121SKalle Valo 		return STA_KEY_IDX_INVALID;
3148e705c121SKalle Valo 
3149e705c121SKalle Valo 	return max_offs;
3150e705c121SKalle Valo }
3151e705c121SKalle Valo 
31525f7a1847SJohannes Berg static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
31534615fd15SEmmanuel Grumbach 					       struct ieee80211_vif *vif,
3154e705c121SKalle Valo 					       struct ieee80211_sta *sta)
3155e705c121SKalle Valo {
3156e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3157e705c121SKalle Valo 
31585f7a1847SJohannes Berg 	if (sta)
31595f7a1847SJohannes Berg 		return iwl_mvm_sta_from_mac80211(sta);
3160e705c121SKalle Valo 
3161e705c121SKalle Valo 	/*
3162e705c121SKalle Valo 	 * The device expects GTKs for station interfaces to be
3163e705c121SKalle Valo 	 * installed as GTKs for the AP station. If we have no
3164e705c121SKalle Valo 	 * station ID, then use AP's station ID.
3165e705c121SKalle Valo 	 */
3166e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
31670ae98812SSara Sharon 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
31684615fd15SEmmanuel Grumbach 		u8 sta_id = mvmvif->ap_sta_id;
31694615fd15SEmmanuel Grumbach 
31707d6a1ab6SEmmanuel Grumbach 		sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
31717d6a1ab6SEmmanuel Grumbach 					    lockdep_is_held(&mvm->mutex));
31727d6a1ab6SEmmanuel Grumbach 
31734615fd15SEmmanuel Grumbach 		/*
31744615fd15SEmmanuel Grumbach 		 * It is possible that the 'sta' parameter is NULL,
31754615fd15SEmmanuel Grumbach 		 * for example when a GTK is removed - the sta_id will then
31764615fd15SEmmanuel Grumbach 		 * be the AP ID, and no station was passed by mac80211.
31774615fd15SEmmanuel Grumbach 		 */
31787d6a1ab6SEmmanuel Grumbach 		if (IS_ERR_OR_NULL(sta))
31797d6a1ab6SEmmanuel Grumbach 			return NULL;
31807d6a1ab6SEmmanuel Grumbach 
31817d6a1ab6SEmmanuel Grumbach 		return iwl_mvm_sta_from_mac80211(sta);
31824615fd15SEmmanuel Grumbach 	}
3183e705c121SKalle Valo 
31845f7a1847SJohannes Berg 	return NULL;
3185e705c121SKalle Valo }
3186e705c121SKalle Valo 
3187e705c121SKalle Valo static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
318885aeb58cSDavid Spinadel 				u32 sta_id,
318945c458b4SSara Sharon 				struct ieee80211_key_conf *key, bool mcast,
31904615fd15SEmmanuel Grumbach 				u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
31914883145aSEmmanuel Grumbach 				u8 key_offset, bool mfp)
3192e705c121SKalle Valo {
319345c458b4SSara Sharon 	union {
319445c458b4SSara Sharon 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
319545c458b4SSara Sharon 		struct iwl_mvm_add_sta_key_cmd cmd;
319645c458b4SSara Sharon 	} u = {};
3197e705c121SKalle Valo 	__le16 key_flags;
3198e705c121SKalle Valo 	int ret;
3199e705c121SKalle Valo 	u32 status;
3200e705c121SKalle Valo 	u16 keyidx;
320145c458b4SSara Sharon 	u64 pn = 0;
320245c458b4SSara Sharon 	int i, size;
320345c458b4SSara Sharon 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
320445c458b4SSara Sharon 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3205e705c121SKalle Valo 
320685aeb58cSDavid Spinadel 	if (sta_id == IWL_MVM_INVALID_STA)
320785aeb58cSDavid Spinadel 		return -EINVAL;
320885aeb58cSDavid Spinadel 
320945c458b4SSara Sharon 	keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
3210e705c121SKalle Valo 		 STA_KEY_FLG_KEYID_MSK;
3211e705c121SKalle Valo 	key_flags = cpu_to_le16(keyidx);
3212e705c121SKalle Valo 	key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3213e705c121SKalle Valo 
321445c458b4SSara Sharon 	switch (key->cipher) {
3215e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
3216e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
321745c458b4SSara Sharon 		if (new_api) {
321845c458b4SSara Sharon 			memcpy((void *)&u.cmd.tx_mic_key,
321945c458b4SSara Sharon 			       &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
322045c458b4SSara Sharon 			       IWL_MIC_KEY_SIZE);
322145c458b4SSara Sharon 
322245c458b4SSara Sharon 			memcpy((void *)&u.cmd.rx_mic_key,
322345c458b4SSara Sharon 			       &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
322445c458b4SSara Sharon 			       IWL_MIC_KEY_SIZE);
322545c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
322645c458b4SSara Sharon 
322745c458b4SSara Sharon 		} else {
322845c458b4SSara Sharon 			u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3229e705c121SKalle Valo 			for (i = 0; i < 5; i++)
323045c458b4SSara Sharon 				u.cmd_v1.tkip_rx_ttak[i] =
323145c458b4SSara Sharon 					cpu_to_le16(tkip_p1k[i]);
323245c458b4SSara Sharon 		}
323345c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3234e705c121SKalle Valo 		break;
3235e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
3236e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
323745c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
323845c458b4SSara Sharon 		if (new_api)
323945c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
3240e705c121SKalle Valo 		break;
3241e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
3242e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
3243e705c121SKalle Valo 		/* fall through */
3244e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
3245e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
324645c458b4SSara Sharon 		memcpy(u.cmd.common.key + 3, key->key, key->keylen);
3247e705c121SKalle Valo 		break;
32482a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
32492a53d166SAyala Beker 		key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
32502a53d166SAyala Beker 		/* fall through */
32512a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
32522a53d166SAyala Beker 		key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
325345c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
325445c458b4SSara Sharon 		if (new_api)
325545c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
32562a53d166SAyala Beker 		break;
3257e705c121SKalle Valo 	default:
3258e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
325945c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3260e705c121SKalle Valo 	}
3261e705c121SKalle Valo 
3262e705c121SKalle Valo 	if (mcast)
3263e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
32644883145aSEmmanuel Grumbach 	if (mfp)
32654883145aSEmmanuel Grumbach 		key_flags |= cpu_to_le16(STA_KEY_MFP);
3266e705c121SKalle Valo 
326745c458b4SSara Sharon 	u.cmd.common.key_offset = key_offset;
326845c458b4SSara Sharon 	u.cmd.common.key_flags = key_flags;
326985aeb58cSDavid Spinadel 	u.cmd.common.sta_id = sta_id;
327045c458b4SSara Sharon 
327145c458b4SSara Sharon 	if (new_api) {
327245c458b4SSara Sharon 		u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
327345c458b4SSara Sharon 		size = sizeof(u.cmd);
327445c458b4SSara Sharon 	} else {
327545c458b4SSara Sharon 		size = sizeof(u.cmd_v1);
327645c458b4SSara Sharon 	}
3277e705c121SKalle Valo 
3278e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
3279e705c121SKalle Valo 	if (cmd_flags & CMD_ASYNC)
328045c458b4SSara Sharon 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
328145c458b4SSara Sharon 					   &u.cmd);
3282e705c121SKalle Valo 	else
328345c458b4SSara Sharon 		ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
328445c458b4SSara Sharon 						  &u.cmd, &status);
3285e705c121SKalle Valo 
3286e705c121SKalle Valo 	switch (status) {
3287e705c121SKalle Valo 	case ADD_STA_SUCCESS:
3288e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3289e705c121SKalle Valo 		break;
3290e705c121SKalle Valo 	default:
3291e705c121SKalle Valo 		ret = -EIO;
3292e705c121SKalle Valo 		IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3293e705c121SKalle Valo 		break;
3294e705c121SKalle Valo 	}
3295e705c121SKalle Valo 
3296e705c121SKalle Valo 	return ret;
3297e705c121SKalle Valo }
3298e705c121SKalle Valo 
3299e705c121SKalle Valo static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3300e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
3301e705c121SKalle Valo 				 u8 sta_id, bool remove_key)
3302e705c121SKalle Valo {
3303e705c121SKalle Valo 	struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3304e705c121SKalle Valo 
3305e705c121SKalle Valo 	/* verify the key details match the required command's expectations */
33068e160ab8SAyala Beker 	if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
33078e160ab8SAyala Beker 		    (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
33088e160ab8SAyala Beker 		    (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
33098e160ab8SAyala Beker 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
33108e160ab8SAyala Beker 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
33118e160ab8SAyala Beker 		return -EINVAL;
33128e160ab8SAyala Beker 
33138e160ab8SAyala Beker 	if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
33148e160ab8SAyala Beker 		    keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3315e705c121SKalle Valo 		return -EINVAL;
3316e705c121SKalle Valo 
3317e705c121SKalle Valo 	igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3318e705c121SKalle Valo 	igtk_cmd.sta_id = cpu_to_le32(sta_id);
3319e705c121SKalle Valo 
3320e705c121SKalle Valo 	if (remove_key) {
3321e705c121SKalle Valo 		igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3322e705c121SKalle Valo 	} else {
3323e705c121SKalle Valo 		struct ieee80211_key_seq seq;
3324e705c121SKalle Valo 		const u8 *pn;
3325e705c121SKalle Valo 
3326aa950524SAyala Beker 		switch (keyconf->cipher) {
3327aa950524SAyala Beker 		case WLAN_CIPHER_SUITE_AES_CMAC:
3328aa950524SAyala Beker 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3329aa950524SAyala Beker 			break;
33308e160ab8SAyala Beker 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
33318e160ab8SAyala Beker 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
33328e160ab8SAyala Beker 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
33338e160ab8SAyala Beker 			break;
3334aa950524SAyala Beker 		default:
3335aa950524SAyala Beker 			return -EINVAL;
3336aa950524SAyala Beker 		}
3337aa950524SAyala Beker 
33388e160ab8SAyala Beker 		memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
33398e160ab8SAyala Beker 		if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
33408e160ab8SAyala Beker 			igtk_cmd.ctrl_flags |=
33418e160ab8SAyala Beker 				cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3342e705c121SKalle Valo 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3343e705c121SKalle Valo 		pn = seq.aes_cmac.pn;
3344e705c121SKalle Valo 		igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3345e705c121SKalle Valo 						       ((u64) pn[4] << 8) |
3346e705c121SKalle Valo 						       ((u64) pn[3] << 16) |
3347e705c121SKalle Valo 						       ((u64) pn[2] << 24) |
3348e705c121SKalle Valo 						       ((u64) pn[1] << 32) |
3349e705c121SKalle Valo 						       ((u64) pn[0] << 40));
3350e705c121SKalle Valo 	}
3351e705c121SKalle Valo 
3352e705c121SKalle Valo 	IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
3353e705c121SKalle Valo 		       remove_key ? "removing" : "installing",
3354e705c121SKalle Valo 		       igtk_cmd.sta_id);
3355e705c121SKalle Valo 
33568e160ab8SAyala Beker 	if (!iwl_mvm_has_new_rx_api(mvm)) {
33578e160ab8SAyala Beker 		struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
33588e160ab8SAyala Beker 			.ctrl_flags = igtk_cmd.ctrl_flags,
33598e160ab8SAyala Beker 			.key_id = igtk_cmd.key_id,
33608e160ab8SAyala Beker 			.sta_id = igtk_cmd.sta_id,
33618e160ab8SAyala Beker 			.receive_seq_cnt = igtk_cmd.receive_seq_cnt
33628e160ab8SAyala Beker 		};
33638e160ab8SAyala Beker 
33648e160ab8SAyala Beker 		memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
33658e160ab8SAyala Beker 		       ARRAY_SIZE(igtk_cmd_v1.igtk));
33668e160ab8SAyala Beker 		return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
33678e160ab8SAyala Beker 					    sizeof(igtk_cmd_v1), &igtk_cmd_v1);
33688e160ab8SAyala Beker 	}
3369e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3370e705c121SKalle Valo 				    sizeof(igtk_cmd), &igtk_cmd);
3371e705c121SKalle Valo }
3372e705c121SKalle Valo 
3373e705c121SKalle Valo 
3374e705c121SKalle Valo static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3375e705c121SKalle Valo 				       struct ieee80211_vif *vif,
3376e705c121SKalle Valo 				       struct ieee80211_sta *sta)
3377e705c121SKalle Valo {
3378e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3379e705c121SKalle Valo 
3380e705c121SKalle Valo 	if (sta)
3381e705c121SKalle Valo 		return sta->addr;
3382e705c121SKalle Valo 
3383e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
33840ae98812SSara Sharon 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3385e705c121SKalle Valo 		u8 sta_id = mvmvif->ap_sta_id;
3386e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3387e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
3388e705c121SKalle Valo 		return sta->addr;
3389e705c121SKalle Valo 	}
3390e705c121SKalle Valo 
3391e705c121SKalle Valo 
3392e705c121SKalle Valo 	return NULL;
3393e705c121SKalle Valo }
3394e705c121SKalle Valo 
3395e705c121SKalle Valo static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3396e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3397e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3398e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
33994615fd15SEmmanuel Grumbach 				 u8 key_offset,
3400e705c121SKalle Valo 				 bool mcast)
3401e705c121SKalle Valo {
3402e705c121SKalle Valo 	int ret;
3403e705c121SKalle Valo 	const u8 *addr;
3404e705c121SKalle Valo 	struct ieee80211_key_seq seq;
3405e705c121SKalle Valo 	u16 p1k[5];
340685aeb58cSDavid Spinadel 	u32 sta_id;
34074883145aSEmmanuel Grumbach 	bool mfp = false;
340885aeb58cSDavid Spinadel 
340985aeb58cSDavid Spinadel 	if (sta) {
341085aeb58cSDavid Spinadel 		struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
341185aeb58cSDavid Spinadel 
341285aeb58cSDavid Spinadel 		sta_id = mvm_sta->sta_id;
34134883145aSEmmanuel Grumbach 		mfp = sta->mfp;
341485aeb58cSDavid Spinadel 	} else if (vif->type == NL80211_IFTYPE_AP &&
341585aeb58cSDavid Spinadel 		   !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
341685aeb58cSDavid Spinadel 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
341785aeb58cSDavid Spinadel 
341885aeb58cSDavid Spinadel 		sta_id = mvmvif->mcast_sta.sta_id;
341985aeb58cSDavid Spinadel 	} else {
342085aeb58cSDavid Spinadel 		IWL_ERR(mvm, "Failed to find station id\n");
342185aeb58cSDavid Spinadel 		return -EINVAL;
342285aeb58cSDavid Spinadel 	}
3423e705c121SKalle Valo 
3424e705c121SKalle Valo 	switch (keyconf->cipher) {
3425e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
3426e705c121SKalle Valo 		addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3427e705c121SKalle Valo 		/* get phase 1 key from mac80211 */
3428e705c121SKalle Valo 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3429e705c121SKalle Valo 		ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
343085aeb58cSDavid Spinadel 		ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
34314883145aSEmmanuel Grumbach 					   seq.tkip.iv32, p1k, 0, key_offset,
34324883145aSEmmanuel Grumbach 					   mfp);
3433e705c121SKalle Valo 		break;
3434e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
3435e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
3436e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
34372a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
34382a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
343985aeb58cSDavid Spinadel 		ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
34404883145aSEmmanuel Grumbach 					   0, NULL, 0, key_offset, mfp);
3441e705c121SKalle Valo 		break;
3442e705c121SKalle Valo 	default:
344385aeb58cSDavid Spinadel 		ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
34444883145aSEmmanuel Grumbach 					   0, NULL, 0, key_offset, mfp);
3445e705c121SKalle Valo 	}
3446e705c121SKalle Valo 
3447e705c121SKalle Valo 	return ret;
3448e705c121SKalle Valo }
3449e705c121SKalle Valo 
3450e705c121SKalle Valo int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3451e705c121SKalle Valo 			struct ieee80211_vif *vif,
3452e705c121SKalle Valo 			struct ieee80211_sta *sta,
3453e705c121SKalle Valo 			struct ieee80211_key_conf *keyconf,
34544615fd15SEmmanuel Grumbach 			u8 key_offset)
3455e705c121SKalle Valo {
3456e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
34575f7a1847SJohannes Berg 	struct iwl_mvm_sta *mvm_sta;
345885aeb58cSDavid Spinadel 	u8 sta_id = IWL_MVM_INVALID_STA;
3459e705c121SKalle Valo 	int ret;
3460e705c121SKalle Valo 	static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3461e705c121SKalle Valo 
3462e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3463e705c121SKalle Valo 
346485aeb58cSDavid Spinadel 	if (vif->type != NL80211_IFTYPE_AP ||
346585aeb58cSDavid Spinadel 	    keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3466e705c121SKalle Valo 		/* Get the station id from the mvm local station table */
34675f7a1847SJohannes Berg 		mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
34685f7a1847SJohannes Berg 		if (!mvm_sta) {
34695f7a1847SJohannes Berg 			IWL_ERR(mvm, "Failed to find station\n");
3470e705c121SKalle Valo 			return -EINVAL;
3471e705c121SKalle Valo 		}
34725f7a1847SJohannes Berg 		sta_id = mvm_sta->sta_id;
3473e705c121SKalle Valo 
3474e705c121SKalle Valo 		/*
3475e705c121SKalle Valo 		 * It is possible that the 'sta' parameter is NULL, and thus
347685aeb58cSDavid Spinadel 		 * there is a need to retrieve the sta from the local station
347785aeb58cSDavid Spinadel 		 * table.
3478e705c121SKalle Valo 		 */
3479e705c121SKalle Valo 		if (!sta) {
348085aeb58cSDavid Spinadel 			sta = rcu_dereference_protected(
348185aeb58cSDavid Spinadel 				mvm->fw_id_to_mac_id[sta_id],
3482e705c121SKalle Valo 				lockdep_is_held(&mvm->mutex));
3483e705c121SKalle Valo 			if (IS_ERR_OR_NULL(sta)) {
3484e705c121SKalle Valo 				IWL_ERR(mvm, "Invalid station id\n");
3485e705c121SKalle Valo 				return -EINVAL;
3486e705c121SKalle Valo 			}
3487e705c121SKalle Valo 		}
3488e705c121SKalle Valo 
3489e705c121SKalle Valo 		if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3490e705c121SKalle Valo 			return -EINVAL;
3491e829b17cSBeni Lev 	} else {
3492e829b17cSBeni Lev 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3493e829b17cSBeni Lev 
3494e829b17cSBeni Lev 		sta_id = mvmvif->mcast_sta.sta_id;
3495e829b17cSBeni Lev 	}
3496e829b17cSBeni Lev 
3497e829b17cSBeni Lev 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3498e829b17cSBeni Lev 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3499e829b17cSBeni Lev 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3500e829b17cSBeni Lev 		ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3501e829b17cSBeni Lev 		goto end;
350285aeb58cSDavid Spinadel 	}
3503e705c121SKalle Valo 
35044615fd15SEmmanuel Grumbach 	/* If the key_offset is not pre-assigned, we need to find a
35054615fd15SEmmanuel Grumbach 	 * new offset to use.  In normal cases, the offset is not
35064615fd15SEmmanuel Grumbach 	 * pre-assigned, but during HW_RESTART we want to reuse the
35074615fd15SEmmanuel Grumbach 	 * same indices, so we pass them when this function is called.
35084615fd15SEmmanuel Grumbach 	 *
35094615fd15SEmmanuel Grumbach 	 * In D3 entry, we need to hardcoded the indices (because the
35104615fd15SEmmanuel Grumbach 	 * firmware hardcodes the PTK offset to 0).  In this case, we
35114615fd15SEmmanuel Grumbach 	 * need to make sure we don't overwrite the hw_key_idx in the
35124615fd15SEmmanuel Grumbach 	 * keyconf structure, because otherwise we cannot configure
35134615fd15SEmmanuel Grumbach 	 * the original ones back when resuming.
3514e705c121SKalle Valo 	 */
35154615fd15SEmmanuel Grumbach 	if (key_offset == STA_KEY_IDX_INVALID) {
35164615fd15SEmmanuel Grumbach 		key_offset  = iwl_mvm_set_fw_key_idx(mvm);
35174615fd15SEmmanuel Grumbach 		if (key_offset == STA_KEY_IDX_INVALID)
3518e705c121SKalle Valo 			return -ENOSPC;
35194615fd15SEmmanuel Grumbach 		keyconf->hw_key_idx = key_offset;
3520e705c121SKalle Valo 	}
3521e705c121SKalle Valo 
35224615fd15SEmmanuel Grumbach 	ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
35239c3deeb5SLuca Coelho 	if (ret)
3524e705c121SKalle Valo 		goto end;
3525e705c121SKalle Valo 
3526e705c121SKalle Valo 	/*
3527e705c121SKalle Valo 	 * For WEP, the same key is used for multicast and unicast. Upload it
3528e705c121SKalle Valo 	 * again, using the same key offset, and now pointing the other one
3529e705c121SKalle Valo 	 * to the same key slot (offset).
3530e705c121SKalle Valo 	 * If this fails, remove the original as well.
3531e705c121SKalle Valo 	 */
353285aeb58cSDavid Spinadel 	if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
353385aeb58cSDavid Spinadel 	     keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
353485aeb58cSDavid Spinadel 	    sta) {
35354615fd15SEmmanuel Grumbach 		ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
35364615fd15SEmmanuel Grumbach 					    key_offset, !mcast);
3537e705c121SKalle Valo 		if (ret) {
3538e705c121SKalle Valo 			__iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
35399c3deeb5SLuca Coelho 			goto end;
3540e705c121SKalle Valo 		}
3541e705c121SKalle Valo 	}
3542e705c121SKalle Valo 
35439c3deeb5SLuca Coelho 	__set_bit(key_offset, mvm->fw_key_table);
35449c3deeb5SLuca Coelho 
3545e705c121SKalle Valo end:
3546e705c121SKalle Valo 	IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3547e705c121SKalle Valo 		      keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3548e705c121SKalle Valo 		      sta ? sta->addr : zero_addr, ret);
3549e705c121SKalle Valo 	return ret;
3550e705c121SKalle Valo }
3551e705c121SKalle Valo 
3552e705c121SKalle Valo int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3553e705c121SKalle Valo 			   struct ieee80211_vif *vif,
3554e705c121SKalle Valo 			   struct ieee80211_sta *sta,
3555e705c121SKalle Valo 			   struct ieee80211_key_conf *keyconf)
3556e705c121SKalle Valo {
3557e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
35585f7a1847SJohannes Berg 	struct iwl_mvm_sta *mvm_sta;
35590ae98812SSara Sharon 	u8 sta_id = IWL_MVM_INVALID_STA;
3560e705c121SKalle Valo 	int ret, i;
3561e705c121SKalle Valo 
3562e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3563e705c121SKalle Valo 
35645f7a1847SJohannes Berg 	/* Get the station from the mvm local station table */
35655f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
356671793b7dSLuca Coelho 	if (mvm_sta)
3567cd4d23c1SIlan Peer 		sta_id = mvm_sta->sta_id;
356885aeb58cSDavid Spinadel 	else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
356985aeb58cSDavid Spinadel 		sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
357085aeb58cSDavid Spinadel 
3571e705c121SKalle Valo 
3572e705c121SKalle Valo 	IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3573e705c121SKalle Valo 		      keyconf->keyidx, sta_id);
3574e705c121SKalle Valo 
357571793b7dSLuca Coelho 	if (mvm_sta && (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
35768e160ab8SAyala Beker 			keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
357771793b7dSLuca Coelho 			keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256))
3578e705c121SKalle Valo 		return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3579e705c121SKalle Valo 
3580e705c121SKalle Valo 	if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3581e705c121SKalle Valo 		IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3582e705c121SKalle Valo 			keyconf->hw_key_idx);
3583e705c121SKalle Valo 		return -ENOENT;
3584e705c121SKalle Valo 	}
3585e705c121SKalle Valo 
3586e705c121SKalle Valo 	/* track which key was deleted last */
3587e705c121SKalle Valo 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3588e705c121SKalle Valo 		if (mvm->fw_key_deleted[i] < U8_MAX)
3589e705c121SKalle Valo 			mvm->fw_key_deleted[i]++;
3590e705c121SKalle Valo 	}
3591e705c121SKalle Valo 	mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3592e705c121SKalle Valo 
359385aeb58cSDavid Spinadel 	if (sta && !mvm_sta) {
3594e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3595e705c121SKalle Valo 		return 0;
3596e705c121SKalle Valo 	}
3597e705c121SKalle Valo 
3598e705c121SKalle Valo 	ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3599e705c121SKalle Valo 	if (ret)
3600e705c121SKalle Valo 		return ret;
3601e705c121SKalle Valo 
3602e705c121SKalle Valo 	/* delete WEP key twice to get rid of (now useless) offset */
3603e705c121SKalle Valo 	if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3604e705c121SKalle Valo 	    keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3605e705c121SKalle Valo 		ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3606e705c121SKalle Valo 
3607e705c121SKalle Valo 	return ret;
3608e705c121SKalle Valo }
3609e705c121SKalle Valo 
3610e705c121SKalle Valo void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3611e705c121SKalle Valo 			     struct ieee80211_vif *vif,
3612e705c121SKalle Valo 			     struct ieee80211_key_conf *keyconf,
3613e705c121SKalle Valo 			     struct ieee80211_sta *sta, u32 iv32,
3614e705c121SKalle Valo 			     u16 *phase1key)
3615e705c121SKalle Valo {
3616e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta;
3617e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
36184883145aSEmmanuel Grumbach 	bool mfp = sta ? sta->mfp : false;
3619e705c121SKalle Valo 
3620e705c121SKalle Valo 	rcu_read_lock();
3621e705c121SKalle Valo 
36225f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
36235f7a1847SJohannes Berg 	if (WARN_ON_ONCE(!mvm_sta))
362412f17211SEmmanuel Grumbach 		goto unlock;
362585aeb58cSDavid Spinadel 	iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
36264883145aSEmmanuel Grumbach 			     iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx,
36274883145aSEmmanuel Grumbach 			     mfp);
362812f17211SEmmanuel Grumbach 
362912f17211SEmmanuel Grumbach  unlock:
3630e705c121SKalle Valo 	rcu_read_unlock();
3631e705c121SKalle Valo }
3632e705c121SKalle Valo 
3633e705c121SKalle Valo void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3634e705c121SKalle Valo 				struct ieee80211_sta *sta)
3635e705c121SKalle Valo {
3636e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3637e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3638e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3639e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3640e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_PS),
3641e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3642e705c121SKalle Valo 	};
3643e705c121SKalle Valo 	int ret;
3644e705c121SKalle Valo 
3645854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3646854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3647e705c121SKalle Valo 	if (ret)
3648e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3649e705c121SKalle Valo }
3650e705c121SKalle Valo 
3651e705c121SKalle Valo void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3652e705c121SKalle Valo 				       struct ieee80211_sta *sta,
3653e705c121SKalle Valo 				       enum ieee80211_frame_release_type reason,
3654e705c121SKalle Valo 				       u16 cnt, u16 tids, bool more_data,
36559a3fcf91SSara Sharon 				       bool single_sta_queue)
3656e705c121SKalle Valo {
3657e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3658e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3659e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3660e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3661e705c121SKalle Valo 		.modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3662e705c121SKalle Valo 		.sleep_tx_count = cpu_to_le16(cnt),
3663e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3664e705c121SKalle Valo 	};
3665e705c121SKalle Valo 	int tid, ret;
3666e705c121SKalle Valo 	unsigned long _tids = tids;
3667e705c121SKalle Valo 
3668e705c121SKalle Valo 	/* convert TIDs to ACs - we don't support TSPEC so that's OK
3669e705c121SKalle Valo 	 * Note that this field is reserved and unused by firmware not
3670e705c121SKalle Valo 	 * supporting GO uAPSD, so it's safe to always do this.
3671e705c121SKalle Valo 	 */
3672e705c121SKalle Valo 	for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3673e705c121SKalle Valo 		cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3674e705c121SKalle Valo 
36759a3fcf91SSara Sharon 	/* If we're releasing frames from aggregation or dqa queues then check
36769a3fcf91SSara Sharon 	 * if all the queues that we're releasing frames from, combined, have:
3677e705c121SKalle Valo 	 *  - more frames than the service period, in which case more_data
3678e705c121SKalle Valo 	 *    needs to be set
3679e705c121SKalle Valo 	 *  - fewer than 'cnt' frames, in which case we need to adjust the
3680e705c121SKalle Valo 	 *    firmware command (but do that unconditionally)
3681e705c121SKalle Valo 	 */
36829a3fcf91SSara Sharon 	if (single_sta_queue) {
3683e705c121SKalle Valo 		int remaining = cnt;
368436be0eb6SEmmanuel Grumbach 		int sleep_tx_count;
3685e705c121SKalle Valo 
3686e705c121SKalle Valo 		spin_lock_bh(&mvmsta->lock);
3687e705c121SKalle Valo 		for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3688e705c121SKalle Valo 			struct iwl_mvm_tid_data *tid_data;
3689e705c121SKalle Valo 			u16 n_queued;
3690e705c121SKalle Valo 
3691e705c121SKalle Valo 			tid_data = &mvmsta->tid_data[tid];
3692e705c121SKalle Valo 
3693dd32162dSLiad Kaufman 			n_queued = iwl_mvm_tid_queued(mvm, tid_data);
3694e705c121SKalle Valo 			if (n_queued > remaining) {
3695e705c121SKalle Valo 				more_data = true;
3696e705c121SKalle Valo 				remaining = 0;
3697e705c121SKalle Valo 				break;
3698e705c121SKalle Valo 			}
3699e705c121SKalle Valo 			remaining -= n_queued;
3700e705c121SKalle Valo 		}
370136be0eb6SEmmanuel Grumbach 		sleep_tx_count = cnt - remaining;
370236be0eb6SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
370336be0eb6SEmmanuel Grumbach 			mvmsta->sleep_tx_count = sleep_tx_count;
3704e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
3705e705c121SKalle Valo 
370636be0eb6SEmmanuel Grumbach 		cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3707e705c121SKalle Valo 		if (WARN_ON(cnt - remaining == 0)) {
3708e705c121SKalle Valo 			ieee80211_sta_eosp(sta);
3709e705c121SKalle Valo 			return;
3710e705c121SKalle Valo 		}
3711e705c121SKalle Valo 	}
3712e705c121SKalle Valo 
3713e705c121SKalle Valo 	/* Note: this is ignored by firmware not supporting GO uAPSD */
3714e705c121SKalle Valo 	if (more_data)
3715ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
3716e705c121SKalle Valo 
3717e705c121SKalle Valo 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3718e705c121SKalle Valo 		mvmsta->next_status_eosp = true;
3719ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
3720e705c121SKalle Valo 	} else {
3721ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
3722e705c121SKalle Valo 	}
3723e705c121SKalle Valo 
3724156f92f2SEmmanuel Grumbach 	/* block the Tx queues until the FW updated the sleep Tx count */
3725156f92f2SEmmanuel Grumbach 	iwl_trans_block_txq_ptrs(mvm->trans, true);
3726156f92f2SEmmanuel Grumbach 
3727156f92f2SEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3728156f92f2SEmmanuel Grumbach 				   CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3729854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3730e705c121SKalle Valo 	if (ret)
3731e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3732e705c121SKalle Valo }
3733e705c121SKalle Valo 
3734e705c121SKalle Valo void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3735e705c121SKalle Valo 			   struct iwl_rx_cmd_buffer *rxb)
3736e705c121SKalle Valo {
3737e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3738e705c121SKalle Valo 	struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3739e705c121SKalle Valo 	struct ieee80211_sta *sta;
3740e705c121SKalle Valo 	u32 sta_id = le32_to_cpu(notif->sta_id);
3741e705c121SKalle Valo 
3742e705c121SKalle Valo 	if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
3743e705c121SKalle Valo 		return;
3744e705c121SKalle Valo 
3745e705c121SKalle Valo 	rcu_read_lock();
3746e705c121SKalle Valo 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3747e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(sta))
3748e705c121SKalle Valo 		ieee80211_sta_eosp(sta);
3749e705c121SKalle Valo 	rcu_read_unlock();
3750e705c121SKalle Valo }
3751e705c121SKalle Valo 
3752e705c121SKalle Valo void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3753e705c121SKalle Valo 				   struct iwl_mvm_sta *mvmsta, bool disable)
3754e705c121SKalle Valo {
3755e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3756e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3757e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3758e705c121SKalle Valo 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3759e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3760e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3761e705c121SKalle Valo 	};
3762e705c121SKalle Valo 	int ret;
3763e705c121SKalle Valo 
3764854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3765854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3766e705c121SKalle Valo 	if (ret)
3767e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3768e705c121SKalle Valo }
3769e705c121SKalle Valo 
3770e705c121SKalle Valo void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3771e705c121SKalle Valo 				      struct ieee80211_sta *sta,
3772e705c121SKalle Valo 				      bool disable)
3773e705c121SKalle Valo {
3774e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3775e705c121SKalle Valo 
3776e705c121SKalle Valo 	spin_lock_bh(&mvm_sta->lock);
3777e705c121SKalle Valo 
3778e705c121SKalle Valo 	if (mvm_sta->disable_tx == disable) {
3779e705c121SKalle Valo 		spin_unlock_bh(&mvm_sta->lock);
3780e705c121SKalle Valo 		return;
3781e705c121SKalle Valo 	}
3782e705c121SKalle Valo 
3783e705c121SKalle Valo 	mvm_sta->disable_tx = disable;
3784e705c121SKalle Valo 
3785c8f54701SJohannes Berg 	/* Tell mac80211 to start/stop queuing tx for this station */
3786e705c121SKalle Valo 	ieee80211_sta_block_awake(mvm->hw, sta, disable);
3787e705c121SKalle Valo 
3788e705c121SKalle Valo 	iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3789e705c121SKalle Valo 
3790e705c121SKalle Valo 	spin_unlock_bh(&mvm_sta->lock);
3791e705c121SKalle Valo }
3792e705c121SKalle Valo 
3793ced19f26SSara Sharon static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
3794ced19f26SSara Sharon 					      struct iwl_mvm_vif *mvmvif,
3795ced19f26SSara Sharon 					      struct iwl_mvm_int_sta *sta,
3796ced19f26SSara Sharon 					      bool disable)
3797ced19f26SSara Sharon {
3798ced19f26SSara Sharon 	u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
3799ced19f26SSara Sharon 	struct iwl_mvm_add_sta_cmd cmd = {
3800ced19f26SSara Sharon 		.add_modify = STA_MODE_MODIFY,
3801ced19f26SSara Sharon 		.sta_id = sta->sta_id,
3802ced19f26SSara Sharon 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3803ced19f26SSara Sharon 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3804ced19f26SSara Sharon 		.mac_id_n_color = cpu_to_le32(id),
3805ced19f26SSara Sharon 	};
3806ced19f26SSara Sharon 	int ret;
3807ced19f26SSara Sharon 
3808ced19f26SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, 0,
3809ced19f26SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3810ced19f26SSara Sharon 	if (ret)
3811ced19f26SSara Sharon 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3812ced19f26SSara Sharon }
3813ced19f26SSara Sharon 
3814e705c121SKalle Valo void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3815e705c121SKalle Valo 				       struct iwl_mvm_vif *mvmvif,
3816e705c121SKalle Valo 				       bool disable)
3817e705c121SKalle Valo {
3818e705c121SKalle Valo 	struct ieee80211_sta *sta;
3819e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta;
3820e705c121SKalle Valo 	int i;
3821e705c121SKalle Valo 
3822e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3823e705c121SKalle Valo 
3824e705c121SKalle Valo 	/* Block/unblock all the stations of the given mvmvif */
38250ae98812SSara Sharon 	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
3826e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3827e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
3828e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
3829e705c121SKalle Valo 			continue;
3830e705c121SKalle Valo 
3831e705c121SKalle Valo 		mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3832e705c121SKalle Valo 		if (mvm_sta->mac_id_n_color !=
3833e705c121SKalle Valo 		    FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3834e705c121SKalle Valo 			continue;
3835e705c121SKalle Valo 
3836e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3837e705c121SKalle Valo 	}
3838ced19f26SSara Sharon 
3839ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3840ced19f26SSara Sharon 		return;
3841ced19f26SSara Sharon 
3842ced19f26SSara Sharon 	/* Need to block/unblock also multicast station */
3843ced19f26SSara Sharon 	if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
3844ced19f26SSara Sharon 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3845ced19f26SSara Sharon 						  &mvmvif->mcast_sta, disable);
3846ced19f26SSara Sharon 
3847ced19f26SSara Sharon 	/*
3848ced19f26SSara Sharon 	 * Only unblock the broadcast station (FW blocks it for immediate
3849ced19f26SSara Sharon 	 * quiet, not the driver)
3850ced19f26SSara Sharon 	 */
3851ced19f26SSara Sharon 	if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
3852ced19f26SSara Sharon 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3853ced19f26SSara Sharon 						  &mvmvif->bcast_sta, disable);
3854e705c121SKalle Valo }
3855e705c121SKalle Valo 
3856e705c121SKalle Valo void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3857e705c121SKalle Valo {
3858e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3859e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
3860e705c121SKalle Valo 
3861e705c121SKalle Valo 	rcu_read_lock();
3862e705c121SKalle Valo 
3863e705c121SKalle Valo 	mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3864e705c121SKalle Valo 
3865e705c121SKalle Valo 	if (!WARN_ON(!mvmsta))
3866e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3867e705c121SKalle Valo 
3868e705c121SKalle Valo 	rcu_read_unlock();
3869e705c121SKalle Valo }
3870dd32162dSLiad Kaufman 
3871dd32162dSLiad Kaufman u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
3872dd32162dSLiad Kaufman {
3873dd32162dSLiad Kaufman 	u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3874dd32162dSLiad Kaufman 
3875dd32162dSLiad Kaufman 	/*
38762f7a3863SLuca Coelho 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
3877dd32162dSLiad Kaufman 	 * to align the wrap around of ssn so we compare relevant values.
3878dd32162dSLiad Kaufman 	 */
3879dd32162dSLiad Kaufman 	if (mvm->trans->cfg->gen2)
3880dd32162dSLiad Kaufman 		sn &= 0xff;
3881dd32162dSLiad Kaufman 
3882dd32162dSLiad Kaufman 	return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
3883dd32162dSLiad Kaufman }
3884