18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
33538c809SLuca Coelho  * Copyright (C) 2012-2015, 2018-2022 Intel Corporation
48e99ea8dSJohannes Berg  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
58e99ea8dSJohannes Berg  * Copyright (C) 2016-2017 Intel Deutschland GmbH
68e99ea8dSJohannes Berg  */
7e705c121SKalle Valo #include <net/mac80211.h>
8e705c121SKalle Valo 
9e705c121SKalle Valo #include "mvm.h"
10e705c121SKalle Valo #include "sta.h"
11e705c121SKalle Valo #include "rs.h"
12*c7eca79dSAvraham Stern #include "time-sync.h"
13e705c121SKalle Valo 
14854c5705SSara Sharon /*
15854c5705SSara Sharon  * New version of ADD_STA_sta command added new fields at the end of the
16854c5705SSara Sharon  * structure, so sending the size of the relevant API's structure is enough to
17854c5705SSara Sharon  * support both API versions.
18854c5705SSara Sharon  */
19854c5705SSara Sharon static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
20854c5705SSara Sharon {
21ced19f26SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) ||
22ced19f26SSara Sharon 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
23ced19f26SSara Sharon 		return sizeof(struct iwl_mvm_add_sta_cmd);
24ced19f26SSara Sharon 	else
25ced19f26SSara Sharon 		return sizeof(struct iwl_mvm_add_sta_cmd_v7);
26854c5705SSara Sharon }
27854c5705SSara Sharon 
28e705c121SKalle Valo static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
29e705c121SKalle Valo 				    enum nl80211_iftype iftype)
30e705c121SKalle Valo {
31e705c121SKalle Valo 	int sta_id;
32e705c121SKalle Valo 	u32 reserved_ids = 0;
33e705c121SKalle Valo 
34be9ae34eSNathan Errera 	BUILD_BUG_ON(IWL_MVM_STATION_COUNT_MAX > 32);
35e705c121SKalle Valo 	WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
36e705c121SKalle Valo 
37e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
38e705c121SKalle Valo 
39e705c121SKalle Valo 	/* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
40e705c121SKalle Valo 	if (iftype != NL80211_IFTYPE_STATION)
41e705c121SKalle Valo 		reserved_ids = BIT(0);
42e705c121SKalle Valo 
43e705c121SKalle Valo 	/* Don't take rcu_read_lock() since we are protected by mvm->mutex */
44be9ae34eSNathan Errera 	for (sta_id = 0; sta_id < mvm->fw->ucode_capa.num_stations; sta_id++) {
45e705c121SKalle Valo 		if (BIT(sta_id) & reserved_ids)
46e705c121SKalle Valo 			continue;
47e705c121SKalle Valo 
48e705c121SKalle Valo 		if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
49e705c121SKalle Valo 					       lockdep_is_held(&mvm->mutex)))
50e705c121SKalle Valo 			return sta_id;
51e705c121SKalle Valo 	}
520ae98812SSara Sharon 	return IWL_MVM_INVALID_STA;
53e705c121SKalle Valo }
54e705c121SKalle Valo 
55e705c121SKalle Valo /* send station add/update command to firmware */
56e705c121SKalle Valo int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
5724afba76SLiad Kaufman 			   bool update, unsigned int flags)
58e705c121SKalle Valo {
59e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
60e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd add_sta_cmd = {
61e705c121SKalle Valo 		.sta_id = mvm_sta->sta_id,
62e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
63e705c121SKalle Valo 		.add_modify = update ? 1 : 0,
64e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
658addabf8SNaftali Goldstein 						 STA_FLG_MIMO_EN_MSK |
668addabf8SNaftali Goldstein 						 STA_FLG_RTS_MIMO_PROT),
67cf0cda19SLiad Kaufman 		.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
68e705c121SKalle Valo 	};
69e705c121SKalle Valo 	int ret;
70e705c121SKalle Valo 	u32 status;
71e705c121SKalle Valo 	u32 agg_size = 0, mpdu_dens = 0;
72e705c121SKalle Valo 
73ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
74ced19f26SSara Sharon 		add_sta_cmd.station_type = mvm_sta->sta_type;
75ced19f26SSara Sharon 
7624afba76SLiad Kaufman 	if (!update || (flags & STA_MODIFY_QUEUES)) {
77e705c121SKalle Valo 		memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
7824afba76SLiad Kaufman 
79bb49701bSSara Sharon 		if (!iwl_mvm_has_new_tx_api(mvm)) {
80bb49701bSSara Sharon 			add_sta_cmd.tfd_queue_msk =
81bb49701bSSara Sharon 				cpu_to_le32(mvm_sta->tfd_queue_msk);
82bb49701bSSara Sharon 
8324afba76SLiad Kaufman 			if (flags & STA_MODIFY_QUEUES)
8424afba76SLiad Kaufman 				add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
85bb49701bSSara Sharon 		} else {
86bb49701bSSara Sharon 			WARN_ON(flags & STA_MODIFY_QUEUES);
87bb49701bSSara Sharon 		}
88e705c121SKalle Valo 	}
89e705c121SKalle Valo 
90046d2e7cSSriram R 	switch (sta->deflink.bandwidth) {
915dca295dSIlan Peer 	case IEEE80211_STA_RX_BW_320:
92e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_160:
93e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
945a2abdcaSGustavo A. R. Silva 		fallthrough;
95e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_80:
96e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
975a2abdcaSGustavo A. R. Silva 		fallthrough;
98e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_40:
99e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
1005a2abdcaSGustavo A. R. Silva 		fallthrough;
101e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_20:
102046d2e7cSSriram R 		if (sta->deflink.ht_cap.ht_supported)
103e705c121SKalle Valo 			add_sta_cmd.station_flags |=
104e705c121SKalle Valo 				cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
105e705c121SKalle Valo 		break;
106e705c121SKalle Valo 	}
107e705c121SKalle Valo 
108046d2e7cSSriram R 	switch (sta->deflink.rx_nss) {
109e705c121SKalle Valo 	case 1:
110e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
111e705c121SKalle Valo 		break;
112e705c121SKalle Valo 	case 2:
113e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
114e705c121SKalle Valo 		break;
115e705c121SKalle Valo 	case 3 ... 8:
116e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
117e705c121SKalle Valo 		break;
118e705c121SKalle Valo 	}
119e705c121SKalle Valo 
120261ce887SBenjamin Berg 	switch (sta->deflink.smps_mode) {
121e705c121SKalle Valo 	case IEEE80211_SMPS_AUTOMATIC:
122e705c121SKalle Valo 	case IEEE80211_SMPS_NUM_MODES:
123e705c121SKalle Valo 		WARN_ON(1);
124e705c121SKalle Valo 		break;
125e705c121SKalle Valo 	case IEEE80211_SMPS_STATIC:
126e705c121SKalle Valo 		/* override NSS */
127e705c121SKalle Valo 		add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
128e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
129e705c121SKalle Valo 		break;
130e705c121SKalle Valo 	case IEEE80211_SMPS_DYNAMIC:
131e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
132e705c121SKalle Valo 		break;
133e705c121SKalle Valo 	case IEEE80211_SMPS_OFF:
134e705c121SKalle Valo 		/* nothing */
135e705c121SKalle Valo 		break;
136e705c121SKalle Valo 	}
137e705c121SKalle Valo 
138046d2e7cSSriram R 	if (sta->deflink.ht_cap.ht_supported) {
139e705c121SKalle Valo 		add_sta_cmd.station_flags_msk |=
140e705c121SKalle Valo 			cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
141e705c121SKalle Valo 				    STA_FLG_AGG_MPDU_DENS_MSK);
142e705c121SKalle Valo 
143046d2e7cSSriram R 		mpdu_dens = sta->deflink.ht_cap.ampdu_density;
144e705c121SKalle Valo 	}
145e705c121SKalle Valo 
146eae94cf8SLuca Coelho 	if (mvm_sta->vif->bss_conf.chandef.chan->band == NL80211_BAND_6GHZ) {
147eae94cf8SLuca Coelho 		add_sta_cmd.station_flags_msk |=
148eae94cf8SLuca Coelho 			cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
149eae94cf8SLuca Coelho 				    STA_FLG_AGG_MPDU_DENS_MSK);
150c8a2e7a2SMordechay Goodstein 
151046d2e7cSSriram R 		mpdu_dens = le16_get_bits(sta->deflink.he_6ghz_capa.capa,
152eae94cf8SLuca Coelho 					  IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
153046d2e7cSSriram R 		agg_size = le16_get_bits(sta->deflink.he_6ghz_capa.capa,
154eae94cf8SLuca Coelho 					 IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
155046d2e7cSSriram R 	} else if (sta->deflink.vht_cap.vht_supported) {
156046d2e7cSSriram R 		agg_size = sta->deflink.vht_cap.cap &
157e705c121SKalle Valo 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
158e705c121SKalle Valo 		agg_size >>=
159e705c121SKalle Valo 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
160046d2e7cSSriram R 	} else if (sta->deflink.ht_cap.ht_supported) {
161046d2e7cSSriram R 		agg_size = sta->deflink.ht_cap.ampdu_factor;
162e705c121SKalle Valo 	}
163e705c121SKalle Valo 
164c8a2e7a2SMordechay Goodstein 	/* D6.0 10.12.2 A-MPDU length limit rules
165c8a2e7a2SMordechay Goodstein 	 * A STA indicates the maximum length of the A-MPDU preEOF padding
166c8a2e7a2SMordechay Goodstein 	 * that it can receive in an HE PPDU in the Maximum A-MPDU Length
167c8a2e7a2SMordechay Goodstein 	 * Exponent field in its HT Capabilities, VHT Capabilities,
168c8a2e7a2SMordechay Goodstein 	 * and HE 6 GHz Band Capabilities elements (if present) and the
169c8a2e7a2SMordechay Goodstein 	 * Maximum AMPDU Length Exponent Extension field in its HE
170c8a2e7a2SMordechay Goodstein 	 * Capabilities element
171c8a2e7a2SMordechay Goodstein 	 */
172046d2e7cSSriram R 	if (sta->deflink.he_cap.has_he)
173046d2e7cSSriram R 		agg_size += u8_get_bits(sta->deflink.he_cap.he_cap_elem.mac_cap_info[3],
174c8a2e7a2SMordechay Goodstein 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
175c8a2e7a2SMordechay Goodstein 
176c8a2e7a2SMordechay Goodstein 	/* Limit to max A-MPDU supported by FW */
177c8a2e7a2SMordechay Goodstein 	if (agg_size > (STA_FLG_MAX_AGG_SIZE_4M >> STA_FLG_MAX_AGG_SIZE_SHIFT))
178c8a2e7a2SMordechay Goodstein 		agg_size = (STA_FLG_MAX_AGG_SIZE_4M >>
179c8a2e7a2SMordechay Goodstein 			    STA_FLG_MAX_AGG_SIZE_SHIFT);
180c8a2e7a2SMordechay Goodstein 
181e705c121SKalle Valo 	add_sta_cmd.station_flags |=
182e705c121SKalle Valo 		cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
183e705c121SKalle Valo 	add_sta_cmd.station_flags |=
184e705c121SKalle Valo 		cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
185d94c5a82SGregory Greenman 	if (mvm_sta->sta_state >= IEEE80211_STA_ASSOC)
18619c52f46SEmmanuel Grumbach 		add_sta_cmd.assoc_id = cpu_to_le16(sta->aid);
187e705c121SKalle Valo 
18865e25482SJohannes Berg 	if (sta->wme) {
18965e25482SJohannes Berg 		add_sta_cmd.modify_mask |= STA_MODIFY_UAPSD_ACS;
19065e25482SJohannes Berg 
19165e25482SJohannes Berg 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
192c80eb570SEmmanuel Grumbach 			add_sta_cmd.uapsd_acs |= BIT(AC_BK);
19365e25482SJohannes Berg 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
194c80eb570SEmmanuel Grumbach 			add_sta_cmd.uapsd_acs |= BIT(AC_BE);
19565e25482SJohannes Berg 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
196c80eb570SEmmanuel Grumbach 			add_sta_cmd.uapsd_acs |= BIT(AC_VI);
19765e25482SJohannes Berg 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
198c80eb570SEmmanuel Grumbach 			add_sta_cmd.uapsd_acs |= BIT(AC_VO);
199c80eb570SEmmanuel Grumbach 		add_sta_cmd.uapsd_acs |= add_sta_cmd.uapsd_acs << 4;
200e71ca5eaSEmmanuel Grumbach 		add_sta_cmd.sp_length = sta->max_sp ? sta->max_sp * 2 : 128;
20165e25482SJohannes Berg 	}
20265e25482SJohannes Berg 
203e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
204854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
205854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
206e705c121SKalle Valo 					  &add_sta_cmd, &status);
207e705c121SKalle Valo 	if (ret)
208e705c121SKalle Valo 		return ret;
209e705c121SKalle Valo 
210837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
211e705c121SKalle Valo 	case ADD_STA_SUCCESS:
212e705c121SKalle Valo 		IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
213e705c121SKalle Valo 		break;
214e705c121SKalle Valo 	default:
215e705c121SKalle Valo 		ret = -EIO;
216e705c121SKalle Valo 		IWL_ERR(mvm, "ADD_STA failed\n");
217e705c121SKalle Valo 		break;
218e705c121SKalle Valo 	}
219e705c121SKalle Valo 
220e705c121SKalle Valo 	return ret;
221e705c121SKalle Valo }
222e705c121SKalle Valo 
2238cef5344SKees Cook static void iwl_mvm_rx_agg_session_expired(struct timer_list *t)
22410b2b201SSara Sharon {
2258cef5344SKees Cook 	struct iwl_mvm_baid_data *data =
2268cef5344SKees Cook 		from_timer(data, t, session_timer);
2278cef5344SKees Cook 	struct iwl_mvm_baid_data __rcu **rcu_ptr = data->rcu_ptr;
22810b2b201SSara Sharon 	struct iwl_mvm_baid_data *ba_data;
22910b2b201SSara Sharon 	struct ieee80211_sta *sta;
23010b2b201SSara Sharon 	struct iwl_mvm_sta *mvm_sta;
23110b2b201SSara Sharon 	unsigned long timeout;
23210b2b201SSara Sharon 
23310b2b201SSara Sharon 	rcu_read_lock();
23410b2b201SSara Sharon 
23510b2b201SSara Sharon 	ba_data = rcu_dereference(*rcu_ptr);
23610b2b201SSara Sharon 
23710b2b201SSara Sharon 	if (WARN_ON(!ba_data))
23810b2b201SSara Sharon 		goto unlock;
23910b2b201SSara Sharon 
24010b2b201SSara Sharon 	if (!ba_data->timeout)
24110b2b201SSara Sharon 		goto unlock;
24210b2b201SSara Sharon 
24310b2b201SSara Sharon 	timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
24410b2b201SSara Sharon 	if (time_is_after_jiffies(timeout)) {
24510b2b201SSara Sharon 		mod_timer(&ba_data->session_timer, timeout);
24610b2b201SSara Sharon 		goto unlock;
24710b2b201SSara Sharon 	}
24810b2b201SSara Sharon 
24910b2b201SSara Sharon 	/* Timer expired */
25010b2b201SSara Sharon 	sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
25161dd8a8aSEmmanuel Grumbach 
25261dd8a8aSEmmanuel Grumbach 	/*
25361dd8a8aSEmmanuel Grumbach 	 * sta should be valid unless the following happens:
25461dd8a8aSEmmanuel Grumbach 	 * The firmware asserts which triggers a reconfig flow, but
25561dd8a8aSEmmanuel Grumbach 	 * the reconfig fails before we set the pointer to sta into
25661dd8a8aSEmmanuel Grumbach 	 * the fw_id_to_mac_id pointer table. Mac80211 can't stop
25761dd8a8aSEmmanuel Grumbach 	 * A-MDPU and hence the timer continues to run. Then, the
25861dd8a8aSEmmanuel Grumbach 	 * timer expires and sta is NULL.
25961dd8a8aSEmmanuel Grumbach 	 */
26061dd8a8aSEmmanuel Grumbach 	if (!sta)
26161dd8a8aSEmmanuel Grumbach 		goto unlock;
26261dd8a8aSEmmanuel Grumbach 
26310b2b201SSara Sharon 	mvm_sta = iwl_mvm_sta_from_mac80211(sta);
26420fc690fSNaftali Goldstein 	ieee80211_rx_ba_timer_expired(mvm_sta->vif,
26510b2b201SSara Sharon 				      sta->addr, ba_data->tid);
26610b2b201SSara Sharon unlock:
26710b2b201SSara Sharon 	rcu_read_unlock();
26810b2b201SSara Sharon }
26910b2b201SSara Sharon 
2709794c64fSLiad Kaufman /* Disable aggregations for a bitmap of TIDs for a given station */
2719794c64fSLiad Kaufman static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
2729794c64fSLiad Kaufman 					unsigned long disable_agg_tids,
2739794c64fSLiad Kaufman 					bool remove_queue)
2749794c64fSLiad Kaufman {
2759794c64fSLiad Kaufman 	struct iwl_mvm_add_sta_cmd cmd = {};
2769794c64fSLiad Kaufman 	struct ieee80211_sta *sta;
2779794c64fSLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
2789794c64fSLiad Kaufman 	u32 status;
2799794c64fSLiad Kaufman 	u8 sta_id;
2809794c64fSLiad Kaufman 
281bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
282bb49701bSSara Sharon 		return -EINVAL;
283bb49701bSSara Sharon 
2849794c64fSLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
2859794c64fSLiad Kaufman 
2869794c64fSLiad Kaufman 	rcu_read_lock();
2879794c64fSLiad Kaufman 
2889794c64fSLiad Kaufman 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
2899794c64fSLiad Kaufman 
2909794c64fSLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
2919794c64fSLiad Kaufman 		rcu_read_unlock();
2929794c64fSLiad Kaufman 		return -EINVAL;
2939794c64fSLiad Kaufman 	}
2949794c64fSLiad Kaufman 
2959794c64fSLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
2969794c64fSLiad Kaufman 
2979794c64fSLiad Kaufman 	mvmsta->tid_disable_agg |= disable_agg_tids;
2989794c64fSLiad Kaufman 
2999794c64fSLiad Kaufman 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
3009794c64fSLiad Kaufman 	cmd.sta_id = mvmsta->sta_id;
3019794c64fSLiad Kaufman 	cmd.add_modify = STA_MODE_MODIFY;
3029794c64fSLiad Kaufman 	cmd.modify_mask = STA_MODIFY_QUEUES;
3039794c64fSLiad Kaufman 	if (disable_agg_tids)
3049794c64fSLiad Kaufman 		cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
3059794c64fSLiad Kaufman 	if (remove_queue)
3069794c64fSLiad Kaufman 		cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
3079794c64fSLiad Kaufman 	cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
3089794c64fSLiad Kaufman 	cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
3099794c64fSLiad Kaufman 
3109794c64fSLiad Kaufman 	rcu_read_unlock();
3119794c64fSLiad Kaufman 
3129794c64fSLiad Kaufman 	/* Notify FW of queue removal from the STA queues */
3139794c64fSLiad Kaufman 	status = ADD_STA_SUCCESS;
314b2c1bf59SSara Sharon 	return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
3159794c64fSLiad Kaufman 					   iwl_mvm_add_sta_cmd_size(mvm),
3169794c64fSLiad Kaufman 					   &cmd, &status);
3179794c64fSLiad Kaufman }
3189794c64fSLiad Kaufman 
319cfbc6c4cSSara Sharon static int iwl_mvm_disable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
320c5a976cfSJohannes Berg 			       int sta_id, u16 *queueptr, u8 tid)
32199448a8cSJohannes Berg {
322c6ce1c74SJohannes Berg 	int queue = *queueptr;
32399448a8cSJohannes Berg 	struct iwl_scd_txq_cfg_cmd cmd = {
32499448a8cSJohannes Berg 		.scd_queue = queue,
32599448a8cSJohannes Berg 		.action = SCD_CFG_DISABLE_QUEUE,
32699448a8cSJohannes Berg 	};
32799448a8cSJohannes Berg 	int ret;
32899448a8cSJohannes Berg 
329227f2597SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
330227f2597SJohannes Berg 
33199448a8cSJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm)) {
332227f2597SJohannes Berg 		if (mvm->sta_remove_requires_queue_remove) {
333227f2597SJohannes Berg 			u32 cmd_id = WIDE_ID(DATA_PATH_GROUP,
334227f2597SJohannes Berg 					     SCD_QUEUE_CONFIG_CMD);
335227f2597SJohannes Berg 			struct iwl_scd_queue_cfg_cmd remove_cmd = {
336227f2597SJohannes Berg 				.operation = cpu_to_le32(IWL_SCD_QUEUE_REMOVE),
337c5a976cfSJohannes Berg 				.u.remove.tid = cpu_to_le32(tid),
338c5a976cfSJohannes Berg 				.u.remove.sta_mask = cpu_to_le32(BIT(sta_id)),
339227f2597SJohannes Berg 			};
340227f2597SJohannes Berg 
341227f2597SJohannes Berg 			ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0,
342227f2597SJohannes Berg 						   sizeof(remove_cmd),
343227f2597SJohannes Berg 						   &remove_cmd);
344227f2597SJohannes Berg 		} else {
345227f2597SJohannes Berg 			ret = 0;
346227f2597SJohannes Berg 		}
347227f2597SJohannes Berg 
34899448a8cSJohannes Berg 		iwl_trans_txq_free(mvm->trans, queue);
349c6ce1c74SJohannes Berg 		*queueptr = IWL_MVM_INVALID_QUEUE;
35099448a8cSJohannes Berg 
351227f2597SJohannes Berg 		return ret;
35299448a8cSJohannes Berg 	}
35399448a8cSJohannes Berg 
354f3f240f9SJohannes Berg 	if (WARN_ON(mvm->queue_info[queue].tid_bitmap == 0))
35599448a8cSJohannes Berg 		return 0;
35699448a8cSJohannes Berg 
35799448a8cSJohannes Berg 	mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
35899448a8cSJohannes Berg 
3591c14089eSJohannes Berg 	cmd.action = mvm->queue_info[queue].tid_bitmap ?
36099448a8cSJohannes Berg 		SCD_CFG_ENABLE_QUEUE : SCD_CFG_DISABLE_QUEUE;
36199448a8cSJohannes Berg 	if (cmd.action == SCD_CFG_DISABLE_QUEUE)
36299448a8cSJohannes Berg 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_FREE;
36399448a8cSJohannes Berg 
36499448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
365cfbc6c4cSSara Sharon 			    "Disabling TXQ #%d tids=0x%x\n",
36699448a8cSJohannes Berg 			    queue,
367cfbc6c4cSSara Sharon 			    mvm->queue_info[queue].tid_bitmap);
36899448a8cSJohannes Berg 
36999448a8cSJohannes Berg 	/* If the queue is still enabled - nothing left to do in this func */
370f3f240f9SJohannes Berg 	if (cmd.action == SCD_CFG_ENABLE_QUEUE)
37199448a8cSJohannes Berg 		return 0;
37299448a8cSJohannes Berg 
37399448a8cSJohannes Berg 	cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
37499448a8cSJohannes Berg 	cmd.tid = mvm->queue_info[queue].txq_tid;
37599448a8cSJohannes Berg 
37699448a8cSJohannes Berg 	/* Make sure queue info is correct even though we overwrite it */
377cfbc6c4cSSara Sharon 	WARN(mvm->queue_info[queue].tid_bitmap,
378cfbc6c4cSSara Sharon 	     "TXQ #%d info out-of-sync - tids=0x%x\n",
379cfbc6c4cSSara Sharon 	     queue, mvm->queue_info[queue].tid_bitmap);
38099448a8cSJohannes Berg 
38199448a8cSJohannes Berg 	/* If we are here - the queue is freed and we can zero out these vals */
38299448a8cSJohannes Berg 	mvm->queue_info[queue].tid_bitmap = 0;
383cfbc6c4cSSara Sharon 
384cfbc6c4cSSara Sharon 	if (sta) {
385cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
386cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_tid(sta, tid);
387cfbc6c4cSSara Sharon 
388cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
38995b0f666SJose Ignacio Tornos Martinez 		list_del_init(&mvmtxq->list);
390cfbc6c4cSSara Sharon 	}
39199448a8cSJohannes Berg 
39299448a8cSJohannes Berg 	/* Regardless if this is a reserved TXQ for a STA - mark it as false */
39399448a8cSJohannes Berg 	mvm->queue_info[queue].reserved = false;
39499448a8cSJohannes Berg 
39599448a8cSJohannes Berg 	iwl_trans_txq_disable(mvm->trans, queue, false);
39664ff7eb0SJohannes Berg 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0,
39799448a8cSJohannes Berg 				   sizeof(struct iwl_scd_txq_cfg_cmd), &cmd);
39899448a8cSJohannes Berg 
39999448a8cSJohannes Berg 	if (ret)
40099448a8cSJohannes Berg 		IWL_ERR(mvm, "Failed to disable queue %d (ret=%d)\n",
40199448a8cSJohannes Berg 			queue, ret);
40299448a8cSJohannes Berg 	return ret;
40399448a8cSJohannes Berg }
40499448a8cSJohannes Berg 
40542db09c1SLiad Kaufman static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
40642db09c1SLiad Kaufman {
40742db09c1SLiad Kaufman 	struct ieee80211_sta *sta;
40842db09c1SLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
40942db09c1SLiad Kaufman 	unsigned long tid_bitmap;
41042db09c1SLiad Kaufman 	unsigned long agg_tids = 0;
411806911daSSharon Dvir 	u8 sta_id;
41242db09c1SLiad Kaufman 	int tid;
41342db09c1SLiad Kaufman 
41442db09c1SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
41542db09c1SLiad Kaufman 
416bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
417bb49701bSSara Sharon 		return -EINVAL;
418bb49701bSSara Sharon 
41942db09c1SLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
42042db09c1SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
42142db09c1SLiad Kaufman 
42242db09c1SLiad Kaufman 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
42342db09c1SLiad Kaufman 					lockdep_is_held(&mvm->mutex));
42442db09c1SLiad Kaufman 
42542db09c1SLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
42642db09c1SLiad Kaufman 		return -EINVAL;
42742db09c1SLiad Kaufman 
42842db09c1SLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
42942db09c1SLiad Kaufman 
43042db09c1SLiad Kaufman 	spin_lock_bh(&mvmsta->lock);
43142db09c1SLiad Kaufman 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
43242db09c1SLiad Kaufman 		if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
43342db09c1SLiad Kaufman 			agg_tids |= BIT(tid);
43442db09c1SLiad Kaufman 	}
43542db09c1SLiad Kaufman 	spin_unlock_bh(&mvmsta->lock);
43642db09c1SLiad Kaufman 
43742db09c1SLiad Kaufman 	return agg_tids;
43842db09c1SLiad Kaufman }
43942db09c1SLiad Kaufman 
4409794c64fSLiad Kaufman /*
4419794c64fSLiad Kaufman  * Remove a queue from a station's resources.
4429794c64fSLiad Kaufman  * Note that this only marks as free. It DOESN'T delete a BA agreement, and
4439794c64fSLiad Kaufman  * doesn't disable the queue
4449794c64fSLiad Kaufman  */
4459794c64fSLiad Kaufman static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
4469794c64fSLiad Kaufman {
4479794c64fSLiad Kaufman 	struct ieee80211_sta *sta;
4489794c64fSLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
4499794c64fSLiad Kaufman 	unsigned long tid_bitmap;
4509794c64fSLiad Kaufman 	unsigned long disable_agg_tids = 0;
4519794c64fSLiad Kaufman 	u8 sta_id;
4529794c64fSLiad Kaufman 	int tid;
4539794c64fSLiad Kaufman 
4549794c64fSLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
4559794c64fSLiad Kaufman 
456bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
457bb49701bSSara Sharon 		return -EINVAL;
458bb49701bSSara Sharon 
4599794c64fSLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
4609794c64fSLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
4619794c64fSLiad Kaufman 
4629794c64fSLiad Kaufman 	rcu_read_lock();
4639794c64fSLiad Kaufman 
4649794c64fSLiad Kaufman 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
4659794c64fSLiad Kaufman 
4669794c64fSLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
4679794c64fSLiad Kaufman 		rcu_read_unlock();
4689794c64fSLiad Kaufman 		return 0;
4699794c64fSLiad Kaufman 	}
4709794c64fSLiad Kaufman 
4719794c64fSLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
4729794c64fSLiad Kaufman 
4739794c64fSLiad Kaufman 	spin_lock_bh(&mvmsta->lock);
47442db09c1SLiad Kaufman 	/* Unmap MAC queues and TIDs from this queue */
4759794c64fSLiad Kaufman 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
476cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
477cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_tid(sta, tid);
478cfbc6c4cSSara Sharon 
4799794c64fSLiad Kaufman 		if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
4809794c64fSLiad Kaufman 			disable_agg_tids |= BIT(tid);
4816862fceeSSara Sharon 		mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
482cfbc6c4cSSara Sharon 
483cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
48495b0f666SJose Ignacio Tornos Martinez 		list_del_init(&mvmtxq->list);
4859794c64fSLiad Kaufman 	}
4869794c64fSLiad Kaufman 
48742db09c1SLiad Kaufman 	mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
4889794c64fSLiad Kaufman 	spin_unlock_bh(&mvmsta->lock);
4899794c64fSLiad Kaufman 
4909794c64fSLiad Kaufman 	rcu_read_unlock();
4919794c64fSLiad Kaufman 
49206bc6f6eSJohannes Berg 	/*
49306bc6f6eSJohannes Berg 	 * The TX path may have been using this TXQ_ID from the tid_data,
49406bc6f6eSJohannes Berg 	 * so make sure it's no longer running so that we can safely reuse
49506bc6f6eSJohannes Berg 	 * this TXQ later. We've set all the TIDs to IWL_MVM_INVALID_QUEUE
49606bc6f6eSJohannes Berg 	 * above, but nothing guarantees we've stopped using them. Thus,
49706bc6f6eSJohannes Berg 	 * without this, we could get to iwl_mvm_disable_txq() and remove
49806bc6f6eSJohannes Berg 	 * the queue while still sending frames to it.
49906bc6f6eSJohannes Berg 	 */
50006bc6f6eSJohannes Berg 	synchronize_net();
50106bc6f6eSJohannes Berg 
5029794c64fSLiad Kaufman 	return disable_agg_tids;
5039794c64fSLiad Kaufman }
5049794c64fSLiad Kaufman 
50501796ff2SSara Sharon static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
506cfbc6c4cSSara Sharon 				       struct ieee80211_sta *old_sta,
507724fe771SJohannes Berg 				       u8 new_sta_id)
50801796ff2SSara Sharon {
50901796ff2SSara Sharon 	struct iwl_mvm_sta *mvmsta;
510cfbc6c4cSSara Sharon 	u8 sta_id, tid;
51101796ff2SSara Sharon 	unsigned long disable_agg_tids = 0;
512724fe771SJohannes Berg 	bool same_sta;
513c6ce1c74SJohannes Berg 	u16 queue_tmp = queue;
51401796ff2SSara Sharon 	int ret;
51501796ff2SSara Sharon 
51601796ff2SSara Sharon 	lockdep_assert_held(&mvm->mutex);
51701796ff2SSara Sharon 
518bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
519bb49701bSSara Sharon 		return -EINVAL;
520bb49701bSSara Sharon 
52101796ff2SSara Sharon 	sta_id = mvm->queue_info[queue].ra_sta_id;
52201796ff2SSara Sharon 	tid = mvm->queue_info[queue].txq_tid;
52301796ff2SSara Sharon 
524724fe771SJohannes Berg 	same_sta = sta_id == new_sta_id;
525724fe771SJohannes Berg 
52601796ff2SSara Sharon 	mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
527e3df1e4bSSharon Dvir 	if (WARN_ON(!mvmsta))
528e3df1e4bSSharon Dvir 		return -EINVAL;
52901796ff2SSara Sharon 
53001796ff2SSara Sharon 	disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
53101796ff2SSara Sharon 	/* Disable the queue */
53201796ff2SSara Sharon 	if (disable_agg_tids)
53301796ff2SSara Sharon 		iwl_mvm_invalidate_sta_queue(mvm, queue,
53401796ff2SSara Sharon 					     disable_agg_tids, false);
53501796ff2SSara Sharon 
536c5a976cfSJohannes Berg 	ret = iwl_mvm_disable_txq(mvm, old_sta, sta_id, &queue_tmp, tid);
53701796ff2SSara Sharon 	if (ret) {
53801796ff2SSara Sharon 		IWL_ERR(mvm,
53901796ff2SSara Sharon 			"Failed to free inactive queue %d (ret=%d)\n",
54001796ff2SSara Sharon 			queue, ret);
54101796ff2SSara Sharon 
54201796ff2SSara Sharon 		return ret;
54301796ff2SSara Sharon 	}
54401796ff2SSara Sharon 
54501796ff2SSara Sharon 	/* If TXQ is allocated to another STA, update removal in FW */
54601796ff2SSara Sharon 	if (!same_sta)
54701796ff2SSara Sharon 		iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
54801796ff2SSara Sharon 
54901796ff2SSara Sharon 	return 0;
55001796ff2SSara Sharon }
55101796ff2SSara Sharon 
55242db09c1SLiad Kaufman static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
55342db09c1SLiad Kaufman 				    unsigned long tfd_queue_mask, u8 ac)
55442db09c1SLiad Kaufman {
55542db09c1SLiad Kaufman 	int queue = 0;
55642db09c1SLiad Kaufman 	u8 ac_to_queue[IEEE80211_NUM_ACS];
55742db09c1SLiad Kaufman 	int i;
55842db09c1SLiad Kaufman 
55990d2d94cSJohannes Berg 	/*
56090d2d94cSJohannes Berg 	 * This protects us against grabbing a queue that's being reconfigured
56190d2d94cSJohannes Berg 	 * by the inactivity checker.
56290d2d94cSJohannes Berg 	 */
56390d2d94cSJohannes Berg 	lockdep_assert_held(&mvm->mutex);
56490d2d94cSJohannes Berg 
565bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
566bb49701bSSara Sharon 		return -EINVAL;
56742db09c1SLiad Kaufman 
56842db09c1SLiad Kaufman 	memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
56942db09c1SLiad Kaufman 
57042db09c1SLiad Kaufman 	/* See what ACs the existing queues for this STA have */
57142db09c1SLiad Kaufman 	for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
57242db09c1SLiad Kaufman 		/* Only DATA queues can be shared */
57342db09c1SLiad Kaufman 		if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
57442db09c1SLiad Kaufman 		    i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
57542db09c1SLiad Kaufman 			continue;
57642db09c1SLiad Kaufman 
57742db09c1SLiad Kaufman 		ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
57842db09c1SLiad Kaufman 	}
57942db09c1SLiad Kaufman 
58042db09c1SLiad Kaufman 	/*
58142db09c1SLiad Kaufman 	 * The queue to share is chosen only from DATA queues as follows (in
58242db09c1SLiad Kaufman 	 * descending priority):
58342db09c1SLiad Kaufman 	 * 1. An AC_BE queue
58442db09c1SLiad Kaufman 	 * 2. Same AC queue
58542db09c1SLiad Kaufman 	 * 3. Highest AC queue that is lower than new AC
58642db09c1SLiad Kaufman 	 * 4. Any existing AC (there always is at least 1 DATA queue)
58742db09c1SLiad Kaufman 	 */
58842db09c1SLiad Kaufman 
58942db09c1SLiad Kaufman 	/* Priority 1: An AC_BE queue */
59042db09c1SLiad Kaufman 	if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
59142db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_BE];
59242db09c1SLiad Kaufman 	/* Priority 2: Same AC queue */
59342db09c1SLiad Kaufman 	else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
59442db09c1SLiad Kaufman 		queue = ac_to_queue[ac];
59542db09c1SLiad Kaufman 	/* Priority 3a: If new AC is VO and VI exists - use VI */
59642db09c1SLiad Kaufman 	else if (ac == IEEE80211_AC_VO &&
59742db09c1SLiad Kaufman 		 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
59842db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VI];
59942db09c1SLiad Kaufman 	/* Priority 3b: No BE so only AC less than the new one is BK */
60042db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
60142db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_BK];
60242db09c1SLiad Kaufman 	/* Priority 4a: No BE nor BK - use VI if exists */
60342db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
60442db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VI];
60542db09c1SLiad Kaufman 	/* Priority 4b: No BE, BK nor VI - use VO if exists */
60642db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
60742db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VO];
60842db09c1SLiad Kaufman 
60942db09c1SLiad Kaufman 	/* Make sure queue found (or not) is legal */
6109f9af3d7SLiad Kaufman 	if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
6119f9af3d7SLiad Kaufman 	    !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
6129f9af3d7SLiad Kaufman 	    (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
61342db09c1SLiad Kaufman 		IWL_ERR(mvm, "No DATA queues available to share\n");
6149f9af3d7SLiad Kaufman 		return -ENOSPC;
6159f9af3d7SLiad Kaufman 	}
6169f9af3d7SLiad Kaufman 
61742db09c1SLiad Kaufman 	return queue;
61842db09c1SLiad Kaufman }
61942db09c1SLiad Kaufman 
620a54844d4SJohannes Berg /* Re-configure the SCD for a queue that has already been configured */
621a54844d4SJohannes Berg static int iwl_mvm_reconfig_scd(struct iwl_mvm *mvm, int queue, int fifo,
622a54844d4SJohannes Berg 				int sta_id, int tid, int frame_limit, u16 ssn)
623a54844d4SJohannes Berg {
624a54844d4SJohannes Berg 	struct iwl_scd_txq_cfg_cmd cmd = {
625a54844d4SJohannes Berg 		.scd_queue = queue,
626a54844d4SJohannes Berg 		.action = SCD_CFG_ENABLE_QUEUE,
627a54844d4SJohannes Berg 		.window = frame_limit,
628a54844d4SJohannes Berg 		.sta_id = sta_id,
629a54844d4SJohannes Berg 		.ssn = cpu_to_le16(ssn),
630a54844d4SJohannes Berg 		.tx_fifo = fifo,
631a54844d4SJohannes Berg 		.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
632a54844d4SJohannes Berg 			      queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE),
633a54844d4SJohannes Berg 		.tid = tid,
634a54844d4SJohannes Berg 	};
635a54844d4SJohannes Berg 	int ret;
636a54844d4SJohannes Berg 
637a54844d4SJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
638a54844d4SJohannes Berg 		return -EINVAL;
639a54844d4SJohannes Berg 
640a54844d4SJohannes Berg 	if (WARN(mvm->queue_info[queue].tid_bitmap == 0,
641a54844d4SJohannes Berg 		 "Trying to reconfig unallocated queue %d\n", queue))
642a54844d4SJohannes Berg 		return -ENXIO;
643a54844d4SJohannes Berg 
644a54844d4SJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm, "Reconfig SCD for TXQ #%d\n", queue);
645a54844d4SJohannes Berg 
646a54844d4SJohannes Berg 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
647a54844d4SJohannes Berg 	WARN_ONCE(ret, "Failed to re-configure queue %d on FIFO %d, ret=%d\n",
648a54844d4SJohannes Berg 		  queue, fifo, ret);
649a54844d4SJohannes Berg 
650a54844d4SJohannes Berg 	return ret;
651a54844d4SJohannes Berg }
652a54844d4SJohannes Berg 
65358f2cc57SLiad Kaufman /*
6549f9af3d7SLiad Kaufman  * If a given queue has a higher AC than the TID stream that is being compared
6559f9af3d7SLiad Kaufman  * to, the queue needs to be redirected to the lower AC. This function does that
65658f2cc57SLiad Kaufman  * in such a case, otherwise - if no redirection required - it does nothing,
65758f2cc57SLiad Kaufman  * unless the %force param is true.
65858f2cc57SLiad Kaufman  */
659cfbc6c4cSSara Sharon static int iwl_mvm_redirect_queue(struct iwl_mvm *mvm, int queue, int tid,
66058f2cc57SLiad Kaufman 				  int ac, int ssn, unsigned int wdg_timeout,
661cfbc6c4cSSara Sharon 				  bool force, struct iwl_mvm_txq *txq)
66258f2cc57SLiad Kaufman {
66358f2cc57SLiad Kaufman 	struct iwl_scd_txq_cfg_cmd cmd = {
66458f2cc57SLiad Kaufman 		.scd_queue = queue,
665f7c692deSLiad Kaufman 		.action = SCD_CFG_DISABLE_QUEUE,
66658f2cc57SLiad Kaufman 	};
66758f2cc57SLiad Kaufman 	bool shared_queue;
66858f2cc57SLiad Kaufman 	int ret;
66958f2cc57SLiad Kaufman 
670bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
671bb49701bSSara Sharon 		return -EINVAL;
672bb49701bSSara Sharon 
67358f2cc57SLiad Kaufman 	/*
67458f2cc57SLiad Kaufman 	 * If the AC is lower than current one - FIFO needs to be redirected to
67558f2cc57SLiad Kaufman 	 * the lowest one of the streams in the queue. Check if this is needed
67658f2cc57SLiad Kaufman 	 * here.
67758f2cc57SLiad Kaufman 	 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
67858f2cc57SLiad Kaufman 	 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
67958f2cc57SLiad Kaufman 	 * we need to check if the numerical value of X is LARGER than of Y.
68058f2cc57SLiad Kaufman 	 */
68158f2cc57SLiad Kaufman 	if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
68258f2cc57SLiad Kaufman 		IWL_DEBUG_TX_QUEUES(mvm,
68358f2cc57SLiad Kaufman 				    "No redirection needed on TXQ #%d\n",
68458f2cc57SLiad Kaufman 				    queue);
68558f2cc57SLiad Kaufman 		return 0;
68658f2cc57SLiad Kaufman 	}
68758f2cc57SLiad Kaufman 
68858f2cc57SLiad Kaufman 	cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
68958f2cc57SLiad Kaufman 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
690edbe961cSLiad Kaufman 	cmd.tid = mvm->queue_info[queue].txq_tid;
6911c14089eSJohannes Berg 	shared_queue = hweight16(mvm->queue_info[queue].tid_bitmap) > 1;
69258f2cc57SLiad Kaufman 
6939f9af3d7SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
69458f2cc57SLiad Kaufman 			    queue, iwl_mvm_ac_to_tx_fifo[ac]);
69558f2cc57SLiad Kaufman 
696cfbc6c4cSSara Sharon 	/* Stop the queue and wait for it to empty */
697cfbc6c4cSSara Sharon 	txq->stopped = true;
698cfbc6c4cSSara Sharon 
699a1a57877SSara Sharon 	ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue));
70058f2cc57SLiad Kaufman 	if (ret) {
70158f2cc57SLiad Kaufman 		IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
70258f2cc57SLiad Kaufman 			queue);
70358f2cc57SLiad Kaufman 		ret = -EIO;
70458f2cc57SLiad Kaufman 		goto out;
70558f2cc57SLiad Kaufman 	}
70658f2cc57SLiad Kaufman 
70758f2cc57SLiad Kaufman 	/* Before redirecting the queue we need to de-activate it */
70858f2cc57SLiad Kaufman 	iwl_trans_txq_disable(mvm->trans, queue, false);
70958f2cc57SLiad Kaufman 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
71058f2cc57SLiad Kaufman 	if (ret)
71158f2cc57SLiad Kaufman 		IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
71258f2cc57SLiad Kaufman 			ret);
71358f2cc57SLiad Kaufman 
71458f2cc57SLiad Kaufman 	/* Make sure the SCD wrptr is correctly set before reconfiguring */
715ca3b9c6bSSara Sharon 	iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
71658f2cc57SLiad Kaufman 
717edbe961cSLiad Kaufman 	/* Update the TID "owner" of the queue */
718edbe961cSLiad Kaufman 	mvm->queue_info[queue].txq_tid = tid;
719edbe961cSLiad Kaufman 
72058f2cc57SLiad Kaufman 	/* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
72158f2cc57SLiad Kaufman 
72258f2cc57SLiad Kaufman 	/* Redirect to lower AC */
72358f2cc57SLiad Kaufman 	iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
7240ec9257bSSara Sharon 			     cmd.sta_id, tid, IWL_FRAME_LIMIT, ssn);
72558f2cc57SLiad Kaufman 
72658f2cc57SLiad Kaufman 	/* Update AC marking of the queue */
72758f2cc57SLiad Kaufman 	mvm->queue_info[queue].mac80211_ac = ac;
72858f2cc57SLiad Kaufman 
72958f2cc57SLiad Kaufman 	/*
73058f2cc57SLiad Kaufman 	 * Mark queue as shared in transport if shared
73158f2cc57SLiad Kaufman 	 * Note this has to be done after queue enablement because enablement
73258f2cc57SLiad Kaufman 	 * can also set this value, and there is no indication there to shared
73358f2cc57SLiad Kaufman 	 * queues
73458f2cc57SLiad Kaufman 	 */
73558f2cc57SLiad Kaufman 	if (shared_queue)
73658f2cc57SLiad Kaufman 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
73758f2cc57SLiad Kaufman 
73858f2cc57SLiad Kaufman out:
739cfbc6c4cSSara Sharon 	/* Continue using the queue */
740cfbc6c4cSSara Sharon 	txq->stopped = false;
74158f2cc57SLiad Kaufman 
74258f2cc57SLiad Kaufman 	return ret;
74358f2cc57SLiad Kaufman }
74458f2cc57SLiad Kaufman 
74599448a8cSJohannes Berg static int iwl_mvm_find_free_queue(struct iwl_mvm *mvm, u8 sta_id,
74699448a8cSJohannes Berg 				   u8 minq, u8 maxq)
74799448a8cSJohannes Berg {
74899448a8cSJohannes Berg 	int i;
74999448a8cSJohannes Berg 
750f3f240f9SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
75199448a8cSJohannes Berg 
752e5b72e3bSJohannes Berg 	if (WARN(maxq >= mvm->trans->trans_cfg->base_params->num_of_queues,
753e5b72e3bSJohannes Berg 		 "max queue %d >= num_of_queues (%d)", maxq,
754e5b72e3bSJohannes Berg 		 mvm->trans->trans_cfg->base_params->num_of_queues))
755e5b72e3bSJohannes Berg 		maxq = mvm->trans->trans_cfg->base_params->num_of_queues - 1;
756e5b72e3bSJohannes Berg 
75799448a8cSJohannes Berg 	/* This should not be hit with new TX path */
75899448a8cSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
75999448a8cSJohannes Berg 		return -ENOSPC;
76099448a8cSJohannes Berg 
76199448a8cSJohannes Berg 	/* Start by looking for a free queue */
76299448a8cSJohannes Berg 	for (i = minq; i <= maxq; i++)
7631c14089eSJohannes Berg 		if (mvm->queue_info[i].tid_bitmap == 0 &&
76499448a8cSJohannes Berg 		    mvm->queue_info[i].status == IWL_MVM_QUEUE_FREE)
76599448a8cSJohannes Berg 			return i;
76699448a8cSJohannes Berg 
76799448a8cSJohannes Berg 	return -ENOSPC;
76899448a8cSJohannes Berg }
76999448a8cSJohannes Berg 
77009369983SJohannes Berg static int iwl_mvm_get_queue_size(struct ieee80211_sta *sta)
77109369983SJohannes Berg {
77209369983SJohannes Berg 	/* this queue isn't used for traffic (cab_queue) */
77309369983SJohannes Berg 	if (!sta)
77409369983SJohannes Berg 		return IWL_MGMT_QUEUE_SIZE;
77509369983SJohannes Berg 
77609369983SJohannes Berg 	/* support for 1k ba size */
77709369983SJohannes Berg 	if (sta->deflink.eht_cap.has_eht)
77809369983SJohannes Berg 		return IWL_DEFAULT_QUEUE_SIZE_EHT;
77909369983SJohannes Berg 
78009369983SJohannes Berg 	/* support for 256 ba size */
78109369983SJohannes Berg 	if (sta->deflink.he_cap.has_he)
78209369983SJohannes Berg 		return IWL_DEFAULT_QUEUE_SIZE_HE;
78309369983SJohannes Berg 
78409369983SJohannes Berg 	return IWL_DEFAULT_QUEUE_SIZE;
78509369983SJohannes Berg }
78609369983SJohannes Berg 
787006c152aSMiri Korenblit int iwl_mvm_tvqm_enable_txq(struct iwl_mvm *mvm,
78809369983SJohannes Berg 			    struct ieee80211_sta *sta,
78999448a8cSJohannes Berg 			    u8 sta_id, u8 tid, unsigned int timeout)
79099448a8cSJohannes Berg {
791d5399f11SMordechay Goodstein 	int queue, size;
79299448a8cSJohannes Berg 
79399448a8cSJohannes Berg 	if (tid == IWL_MAX_TID_COUNT) {
79499448a8cSJohannes Berg 		tid = IWL_MGMT_TID;
795ff911dcaSShaul Triebitz 		size = max_t(u32, IWL_MGMT_QUEUE_SIZE,
796ff911dcaSShaul Triebitz 			     mvm->trans->cfg->min_txq_size);
797d5399f11SMordechay Goodstein 	} else {
79809369983SJohannes Berg 		size = iwl_mvm_get_queue_size(sta);
799d5399f11SMordechay Goodstein 	}
800d5399f11SMordechay Goodstein 
801d5399f11SMordechay Goodstein 	/* take the min with bc tbl entries allowed */
802d5399f11SMordechay Goodstein 	size = min_t(u32, size, mvm->trans->txqs.bc_tbl_size / sizeof(u16));
803d5399f11SMordechay Goodstein 
804d5399f11SMordechay Goodstein 	/* size needs to be power of 2 values for calculating read/write pointers */
805d5399f11SMordechay Goodstein 	size = rounddown_pow_of_two(size);
806d5399f11SMordechay Goodstein 
80792f78d4bSJohannes Berg 	do {
808227f2597SJohannes Berg 		queue = iwl_trans_txq_alloc(mvm->trans, 0, BIT(sta_id),
80985b17a33SJohannes Berg 					    tid, size, timeout);
81092f78d4bSJohannes Berg 
81192f78d4bSJohannes Berg 		if (queue < 0)
81299448a8cSJohannes Berg 			IWL_DEBUG_TX_QUEUES(mvm,
81392f78d4bSJohannes Berg 					    "Failed allocating TXQ of size %d for sta %d tid %d, ret: %d\n",
81492f78d4bSJohannes Berg 					    size, sta_id, tid, queue);
81592f78d4bSJohannes Berg 		size /= 2;
81692f78d4bSJohannes Berg 	} while (queue < 0 && size >= 16);
81792f78d4bSJohannes Berg 
81892f78d4bSJohannes Berg 	if (queue < 0)
81999448a8cSJohannes Berg 		return queue;
82099448a8cSJohannes Berg 
82199448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d for sta %d tid %d\n",
82299448a8cSJohannes Berg 			    queue, sta_id, tid);
82399448a8cSJohannes Berg 
82499448a8cSJohannes Berg 	return queue;
82599448a8cSJohannes Berg }
82699448a8cSJohannes Berg 
827310181ecSSara Sharon static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
828310181ecSSara Sharon 					struct ieee80211_sta *sta, u8 ac,
829310181ecSSara Sharon 					int tid)
830310181ecSSara Sharon {
831310181ecSSara Sharon 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
832cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq =
833cfbc6c4cSSara Sharon 		iwl_mvm_txq_from_tid(sta, tid);
834310181ecSSara Sharon 	unsigned int wdg_timeout =
835310181ecSSara Sharon 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
836310181ecSSara Sharon 	int queue = -1;
837310181ecSSara Sharon 
838310181ecSSara Sharon 	lockdep_assert_held(&mvm->mutex);
839310181ecSSara Sharon 
840310181ecSSara Sharon 	IWL_DEBUG_TX_QUEUES(mvm,
841310181ecSSara Sharon 			    "Allocating queue for sta %d on tid %d\n",
842310181ecSSara Sharon 			    mvmsta->sta_id, tid);
84309369983SJohannes Berg 	queue = iwl_mvm_tvqm_enable_txq(mvm, sta, mvmsta->sta_id,
84409369983SJohannes Berg 					tid, wdg_timeout);
845310181ecSSara Sharon 	if (queue < 0)
846310181ecSSara Sharon 		return queue;
847310181ecSSara Sharon 
848cfbc6c4cSSara Sharon 	mvmtxq->txq_id = queue;
849cfbc6c4cSSara Sharon 	mvm->tvqm_info[queue].txq_tid = tid;
850cfbc6c4cSSara Sharon 	mvm->tvqm_info[queue].sta_id = mvmsta->sta_id;
851cfbc6c4cSSara Sharon 
852310181ecSSara Sharon 	IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
853310181ecSSara Sharon 
854310181ecSSara Sharon 	spin_lock_bh(&mvmsta->lock);
855310181ecSSara Sharon 	mvmsta->tid_data[tid].txq_id = queue;
856310181ecSSara Sharon 	spin_unlock_bh(&mvmsta->lock);
857310181ecSSara Sharon 
858310181ecSSara Sharon 	return 0;
859310181ecSSara Sharon }
860310181ecSSara Sharon 
861cfbc6c4cSSara Sharon static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm,
862cfbc6c4cSSara Sharon 				       struct ieee80211_sta *sta,
863cfbc6c4cSSara Sharon 				       int queue, u8 sta_id, u8 tid)
86499448a8cSJohannes Berg {
86599448a8cSJohannes Berg 	bool enable_queue = true;
86699448a8cSJohannes Berg 
86799448a8cSJohannes Berg 	/* Make sure this TID isn't already enabled */
86899448a8cSJohannes Berg 	if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) {
86999448a8cSJohannes Berg 		IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n",
87099448a8cSJohannes Berg 			queue, tid);
87199448a8cSJohannes Berg 		return false;
87299448a8cSJohannes Berg 	}
87399448a8cSJohannes Berg 
87499448a8cSJohannes Berg 	/* Update mappings and refcounts */
8751c14089eSJohannes Berg 	if (mvm->queue_info[queue].tid_bitmap)
87699448a8cSJohannes Berg 		enable_queue = false;
87799448a8cSJohannes Berg 
87899448a8cSJohannes Berg 	mvm->queue_info[queue].tid_bitmap |= BIT(tid);
87999448a8cSJohannes Berg 	mvm->queue_info[queue].ra_sta_id = sta_id;
88099448a8cSJohannes Berg 
88199448a8cSJohannes Berg 	if (enable_queue) {
88299448a8cSJohannes Berg 		if (tid != IWL_MAX_TID_COUNT)
88399448a8cSJohannes Berg 			mvm->queue_info[queue].mac80211_ac =
88499448a8cSJohannes Berg 				tid_to_mac80211_ac[tid];
88599448a8cSJohannes Berg 		else
88699448a8cSJohannes Berg 			mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO;
88799448a8cSJohannes Berg 
88899448a8cSJohannes Berg 		mvm->queue_info[queue].txq_tid = tid;
88999448a8cSJohannes Berg 	}
89099448a8cSJohannes Berg 
891cfbc6c4cSSara Sharon 	if (sta) {
892cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
893cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_tid(sta, tid);
894cfbc6c4cSSara Sharon 
895cfbc6c4cSSara Sharon 		mvmtxq->txq_id = queue;
896cfbc6c4cSSara Sharon 	}
897cfbc6c4cSSara Sharon 
89899448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
899cfbc6c4cSSara Sharon 			    "Enabling TXQ #%d tids=0x%x\n",
900cfbc6c4cSSara Sharon 			    queue, mvm->queue_info[queue].tid_bitmap);
90199448a8cSJohannes Berg 
90299448a8cSJohannes Berg 	return enable_queue;
90399448a8cSJohannes Berg }
90499448a8cSJohannes Berg 
905cfbc6c4cSSara Sharon static bool iwl_mvm_enable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
906cfbc6c4cSSara Sharon 			       int queue, u16 ssn,
90799448a8cSJohannes Berg 			       const struct iwl_trans_txq_scd_cfg *cfg,
90899448a8cSJohannes Berg 			       unsigned int wdg_timeout)
90999448a8cSJohannes Berg {
91099448a8cSJohannes Berg 	struct iwl_scd_txq_cfg_cmd cmd = {
91199448a8cSJohannes Berg 		.scd_queue = queue,
91299448a8cSJohannes Berg 		.action = SCD_CFG_ENABLE_QUEUE,
91399448a8cSJohannes Berg 		.window = cfg->frame_limit,
91499448a8cSJohannes Berg 		.sta_id = cfg->sta_id,
91599448a8cSJohannes Berg 		.ssn = cpu_to_le16(ssn),
91699448a8cSJohannes Berg 		.tx_fifo = cfg->fifo,
91799448a8cSJohannes Berg 		.aggregate = cfg->aggregate,
91899448a8cSJohannes Berg 		.tid = cfg->tid,
91999448a8cSJohannes Berg 	};
92099448a8cSJohannes Berg 	bool inc_ssn;
92199448a8cSJohannes Berg 
92299448a8cSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
92399448a8cSJohannes Berg 		return false;
92499448a8cSJohannes Berg 
92599448a8cSJohannes Berg 	/* Send the enabling command if we need to */
926cfbc6c4cSSara Sharon 	if (!iwl_mvm_update_txq_mapping(mvm, sta, queue, cfg->sta_id, cfg->tid))
92799448a8cSJohannes Berg 		return false;
92899448a8cSJohannes Berg 
92999448a8cSJohannes Berg 	inc_ssn = iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn,
93099448a8cSJohannes Berg 					   NULL, wdg_timeout);
93199448a8cSJohannes Berg 	if (inc_ssn)
93299448a8cSJohannes Berg 		le16_add_cpu(&cmd.ssn, 1);
93399448a8cSJohannes Berg 
93499448a8cSJohannes Berg 	WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd),
93599448a8cSJohannes Berg 	     "Failed to configure queue %d on FIFO %d\n", queue, cfg->fifo);
93699448a8cSJohannes Berg 
93799448a8cSJohannes Berg 	return inc_ssn;
93899448a8cSJohannes Berg }
93999448a8cSJohannes Berg 
940b3a87f11SJohannes Berg static void iwl_mvm_change_queue_tid(struct iwl_mvm *mvm, int queue)
94119aefa45SLiad Kaufman {
94219aefa45SLiad Kaufman 	struct iwl_scd_txq_cfg_cmd cmd = {
94319aefa45SLiad Kaufman 		.scd_queue = queue,
94419aefa45SLiad Kaufman 		.action = SCD_CFG_UPDATE_QUEUE_TID,
94519aefa45SLiad Kaufman 	};
94619aefa45SLiad Kaufman 	int tid;
94719aefa45SLiad Kaufman 	unsigned long tid_bitmap;
94819aefa45SLiad Kaufman 	int ret;
94919aefa45SLiad Kaufman 
95019aefa45SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
95119aefa45SLiad Kaufman 
952bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
953bb49701bSSara Sharon 		return;
954bb49701bSSara Sharon 
95519aefa45SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
95619aefa45SLiad Kaufman 
95719aefa45SLiad Kaufman 	if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
95819aefa45SLiad Kaufman 		return;
95919aefa45SLiad Kaufman 
96019aefa45SLiad Kaufman 	/* Find any TID for queue */
96119aefa45SLiad Kaufman 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
96219aefa45SLiad Kaufman 	cmd.tid = tid;
96319aefa45SLiad Kaufman 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
96419aefa45SLiad Kaufman 
96519aefa45SLiad Kaufman 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
966341ca402SLiad Kaufman 	if (ret) {
96719aefa45SLiad Kaufman 		IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
96819aefa45SLiad Kaufman 			queue, ret);
969341ca402SLiad Kaufman 		return;
970341ca402SLiad Kaufman 	}
971341ca402SLiad Kaufman 
972341ca402SLiad Kaufman 	mvm->queue_info[queue].txq_tid = tid;
97319aefa45SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
97419aefa45SLiad Kaufman 			    queue, tid);
97519aefa45SLiad Kaufman }
97619aefa45SLiad Kaufman 
9779f9af3d7SLiad Kaufman static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
9789f9af3d7SLiad Kaufman {
9799f9af3d7SLiad Kaufman 	struct ieee80211_sta *sta;
9809f9af3d7SLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
981806911daSSharon Dvir 	u8 sta_id;
9829f9af3d7SLiad Kaufman 	int tid = -1;
9839f9af3d7SLiad Kaufman 	unsigned long tid_bitmap;
9849f9af3d7SLiad Kaufman 	unsigned int wdg_timeout;
9859f9af3d7SLiad Kaufman 	int ssn;
9869f9af3d7SLiad Kaufman 	int ret = true;
9879f9af3d7SLiad Kaufman 
988bb49701bSSara Sharon 	/* queue sharing is disabled on new TX path */
989bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
990bb49701bSSara Sharon 		return;
991bb49701bSSara Sharon 
9929f9af3d7SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
9939f9af3d7SLiad Kaufman 
9949f9af3d7SLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
9959f9af3d7SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
9969f9af3d7SLiad Kaufman 
9979f9af3d7SLiad Kaufman 	/* Find TID for queue, and make sure it is the only one on the queue */
9989f9af3d7SLiad Kaufman 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
9999f9af3d7SLiad Kaufman 	if (tid_bitmap != BIT(tid)) {
10009f9af3d7SLiad Kaufman 		IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
10019f9af3d7SLiad Kaufman 			queue, tid_bitmap);
10029f9af3d7SLiad Kaufman 		return;
10039f9af3d7SLiad Kaufman 	}
10049f9af3d7SLiad Kaufman 
10059f9af3d7SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
10069f9af3d7SLiad Kaufman 			    tid);
10079f9af3d7SLiad Kaufman 
10089f9af3d7SLiad Kaufman 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
10099f9af3d7SLiad Kaufman 					lockdep_is_held(&mvm->mutex));
10109f9af3d7SLiad Kaufman 
10119f9af3d7SLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
10129f9af3d7SLiad Kaufman 		return;
10139f9af3d7SLiad Kaufman 
10149f9af3d7SLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
10159f9af3d7SLiad Kaufman 	wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
10169f9af3d7SLiad Kaufman 
10179f9af3d7SLiad Kaufman 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
10189f9af3d7SLiad Kaufman 
1019cfbc6c4cSSara Sharon 	ret = iwl_mvm_redirect_queue(mvm, queue, tid,
10209f9af3d7SLiad Kaufman 				     tid_to_mac80211_ac[tid], ssn,
1021cfbc6c4cSSara Sharon 				     wdg_timeout, true,
1022cfbc6c4cSSara Sharon 				     iwl_mvm_txq_from_tid(sta, tid));
10239f9af3d7SLiad Kaufman 	if (ret) {
10249f9af3d7SLiad Kaufman 		IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
10259f9af3d7SLiad Kaufman 		return;
10269f9af3d7SLiad Kaufman 	}
10279f9af3d7SLiad Kaufman 
10289f9af3d7SLiad Kaufman 	/* If aggs should be turned back on - do it */
10299f9af3d7SLiad Kaufman 	if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
10309cd70e80SEmmanuel Grumbach 		struct iwl_mvm_add_sta_cmd cmd = {0};
10319f9af3d7SLiad Kaufman 
10329f9af3d7SLiad Kaufman 		mvmsta->tid_disable_agg &= ~BIT(tid);
10339f9af3d7SLiad Kaufman 
10349f9af3d7SLiad Kaufman 		cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
10359f9af3d7SLiad Kaufman 		cmd.sta_id = mvmsta->sta_id;
10369f9af3d7SLiad Kaufman 		cmd.add_modify = STA_MODE_MODIFY;
10379f9af3d7SLiad Kaufman 		cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
10389f9af3d7SLiad Kaufman 		cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
10399f9af3d7SLiad Kaufman 		cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
10409f9af3d7SLiad Kaufman 
10419f9af3d7SLiad Kaufman 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
10429f9af3d7SLiad Kaufman 					   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
10439f9af3d7SLiad Kaufman 		if (!ret) {
10449f9af3d7SLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm,
10459f9af3d7SLiad Kaufman 					    "TXQ #%d is now aggregated again\n",
10469f9af3d7SLiad Kaufman 					    queue);
10479f9af3d7SLiad Kaufman 
10489f9af3d7SLiad Kaufman 			/* Mark queue intenally as aggregating again */
10499f9af3d7SLiad Kaufman 			iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
10509f9af3d7SLiad Kaufman 		}
10519f9af3d7SLiad Kaufman 	}
10529f9af3d7SLiad Kaufman 
10539f9af3d7SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
10549f9af3d7SLiad Kaufman }
10559f9af3d7SLiad Kaufman 
105699448a8cSJohannes Berg /*
105799448a8cSJohannes Berg  * Remove inactive TIDs of a given queue.
105899448a8cSJohannes Berg  * If all queue TIDs are inactive - mark the queue as inactive
105999448a8cSJohannes Berg  * If only some the queue TIDs are inactive - unmap them from the queue
1060724fe771SJohannes Berg  *
1061724fe771SJohannes Berg  * Returns %true if all TIDs were removed and the queue could be reused.
106299448a8cSJohannes Berg  */
1063724fe771SJohannes Berg static bool iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
106499448a8cSJohannes Berg 					 struct iwl_mvm_sta *mvmsta, int queue,
106590d2d94cSJohannes Berg 					 unsigned long tid_bitmap,
1066b3a87f11SJohannes Berg 					 unsigned long *unshare_queues,
1067b3a87f11SJohannes Berg 					 unsigned long *changetid_queues)
106899448a8cSJohannes Berg {
1069af3cdfd3SJakub Kicinski 	unsigned int tid;
107099448a8cSJohannes Berg 
107199448a8cSJohannes Berg 	lockdep_assert_held(&mvmsta->lock);
1072f3f240f9SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
107399448a8cSJohannes Berg 
107499448a8cSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1075724fe771SJohannes Berg 		return false;
107699448a8cSJohannes Berg 
107799448a8cSJohannes Berg 	/* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
107899448a8cSJohannes Berg 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
107999448a8cSJohannes Berg 		/* If some TFDs are still queued - don't mark TID as inactive */
108099448a8cSJohannes Berg 		if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid]))
108199448a8cSJohannes Berg 			tid_bitmap &= ~BIT(tid);
108299448a8cSJohannes Berg 
108399448a8cSJohannes Berg 		/* Don't mark as inactive any TID that has an active BA */
108499448a8cSJohannes Berg 		if (mvmsta->tid_data[tid].state != IWL_AGG_OFF)
108599448a8cSJohannes Berg 			tid_bitmap &= ~BIT(tid);
108699448a8cSJohannes Berg 	}
108799448a8cSJohannes Berg 
1088724fe771SJohannes Berg 	/* If all TIDs in the queue are inactive - return it can be reused */
108999448a8cSJohannes Berg 	if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
1090724fe771SJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Queue %d is inactive\n", queue);
1091724fe771SJohannes Berg 		return true;
109299448a8cSJohannes Berg 	}
109399448a8cSJohannes Berg 
109499448a8cSJohannes Berg 	/*
109599448a8cSJohannes Berg 	 * If we are here, this is a shared queue and not all TIDs timed-out.
109699448a8cSJohannes Berg 	 * Remove the ones that did.
109799448a8cSJohannes Berg 	 */
109899448a8cSJohannes Berg 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1099d4530f63SJohannes Berg 		u16 q_tid_bitmap;
110099448a8cSJohannes Berg 
110199448a8cSJohannes Berg 		mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
110299448a8cSJohannes Berg 		mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
110399448a8cSJohannes Berg 
1104d4530f63SJohannes Berg 		q_tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1105b3a87f11SJohannes Berg 
1106b3a87f11SJohannes Berg 		/*
1107b3a87f11SJohannes Berg 		 * We need to take into account a situation in which a TXQ was
1108b3a87f11SJohannes Berg 		 * allocated to TID x, and then turned shared by adding TIDs y
1109b3a87f11SJohannes Berg 		 * and z. If TID x becomes inactive and is removed from the TXQ,
1110b3a87f11SJohannes Berg 		 * ownership must be given to one of the remaining TIDs.
1111b3a87f11SJohannes Berg 		 * This is mainly because if TID x continues - a new queue can't
1112b3a87f11SJohannes Berg 		 * be allocated for it as long as it is an owner of another TXQ.
1113b3a87f11SJohannes Berg 		 *
1114b3a87f11SJohannes Berg 		 * Mark this queue in the right bitmap, we'll send the command
1115b3a87f11SJohannes Berg 		 * to the firmware later.
1116b3a87f11SJohannes Berg 		 */
1117d4530f63SJohannes Berg 		if (!(q_tid_bitmap & BIT(mvm->queue_info[queue].txq_tid)))
1118b3a87f11SJohannes Berg 			set_bit(queue, changetid_queues);
1119b3a87f11SJohannes Berg 
112099448a8cSJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm,
112199448a8cSJohannes Berg 				    "Removing inactive TID %d from shared Q:%d\n",
112299448a8cSJohannes Berg 				    tid, queue);
112399448a8cSJohannes Berg 	}
112499448a8cSJohannes Berg 
112599448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
112699448a8cSJohannes Berg 			    "TXQ #%d left with tid bitmap 0x%x\n", queue,
112799448a8cSJohannes Berg 			    mvm->queue_info[queue].tid_bitmap);
112899448a8cSJohannes Berg 
112999448a8cSJohannes Berg 	/*
113099448a8cSJohannes Berg 	 * There may be different TIDs with the same mac queues, so make
113199448a8cSJohannes Berg 	 * sure all TIDs have existing corresponding mac queues enabled
113299448a8cSJohannes Berg 	 */
113399448a8cSJohannes Berg 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
113499448a8cSJohannes Berg 
113599448a8cSJohannes Berg 	/* If the queue is marked as shared - "unshare" it */
11361c14089eSJohannes Berg 	if (hweight16(mvm->queue_info[queue].tid_bitmap) == 1 &&
113799448a8cSJohannes Berg 	    mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
113899448a8cSJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
113999448a8cSJohannes Berg 				    queue);
114090d2d94cSJohannes Berg 		set_bit(queue, unshare_queues);
114199448a8cSJohannes Berg 	}
1142724fe771SJohannes Berg 
1143724fe771SJohannes Berg 	return false;
114499448a8cSJohannes Berg }
114599448a8cSJohannes Berg 
1146724fe771SJohannes Berg /*
1147724fe771SJohannes Berg  * Check for inactivity - this includes checking if any queue
1148724fe771SJohannes Berg  * can be unshared and finding one (and only one) that can be
1149724fe771SJohannes Berg  * reused.
1150724fe771SJohannes Berg  * This function is also invoked as a sort of clean-up task,
1151724fe771SJohannes Berg  * in which case @alloc_for_sta is IWL_MVM_INVALID_STA.
1152724fe771SJohannes Berg  *
1153724fe771SJohannes Berg  * Returns the queue number, or -ENOSPC.
1154724fe771SJohannes Berg  */
1155724fe771SJohannes Berg static int iwl_mvm_inactivity_check(struct iwl_mvm *mvm, u8 alloc_for_sta)
115699448a8cSJohannes Berg {
115799448a8cSJohannes Berg 	unsigned long now = jiffies;
115890d2d94cSJohannes Berg 	unsigned long unshare_queues = 0;
1159b3a87f11SJohannes Berg 	unsigned long changetid_queues = 0;
1160724fe771SJohannes Berg 	int i, ret, free_queue = -ENOSPC;
1161cfbc6c4cSSara Sharon 	struct ieee80211_sta *queue_owner  = NULL;
116299448a8cSJohannes Berg 
1163df2a2245SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
1164df2a2245SJohannes Berg 
116599448a8cSJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm))
1166724fe771SJohannes Berg 		return -ENOSPC;
116799448a8cSJohannes Berg 
116899448a8cSJohannes Berg 	rcu_read_lock();
116999448a8cSJohannes Berg 
1170459ab045SJohannes Berg 	/* we skip the CMD queue below by starting at 1 */
1171459ab045SJohannes Berg 	BUILD_BUG_ON(IWL_MVM_DQA_CMD_QUEUE != 0);
1172459ab045SJohannes Berg 
1173459ab045SJohannes Berg 	for (i = 1; i < IWL_MAX_HW_QUEUES; i++) {
117499448a8cSJohannes Berg 		struct ieee80211_sta *sta;
117599448a8cSJohannes Berg 		struct iwl_mvm_sta *mvmsta;
117699448a8cSJohannes Berg 		u8 sta_id;
117799448a8cSJohannes Berg 		int tid;
117899448a8cSJohannes Berg 		unsigned long inactive_tid_bitmap = 0;
117999448a8cSJohannes Berg 		unsigned long queue_tid_bitmap;
118099448a8cSJohannes Berg 
118199448a8cSJohannes Berg 		queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
1182459ab045SJohannes Berg 		if (!queue_tid_bitmap)
1183459ab045SJohannes Berg 			continue;
118499448a8cSJohannes Berg 
118599448a8cSJohannes Berg 		/* If TXQ isn't in active use anyway - nothing to do here... */
118699448a8cSJohannes Berg 		if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
1187459ab045SJohannes Berg 		    mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED)
118899448a8cSJohannes Berg 			continue;
118999448a8cSJohannes Berg 
119099448a8cSJohannes Berg 		/* Check to see if there are inactive TIDs on this queue */
119199448a8cSJohannes Berg 		for_each_set_bit(tid, &queue_tid_bitmap,
119299448a8cSJohannes Berg 				 IWL_MAX_TID_COUNT + 1) {
119399448a8cSJohannes Berg 			if (time_after(mvm->queue_info[i].last_frame_time[tid] +
119499448a8cSJohannes Berg 				       IWL_MVM_DQA_QUEUE_TIMEOUT, now))
119599448a8cSJohannes Berg 				continue;
119699448a8cSJohannes Berg 
119799448a8cSJohannes Berg 			inactive_tid_bitmap |= BIT(tid);
119899448a8cSJohannes Berg 		}
119999448a8cSJohannes Berg 
120099448a8cSJohannes Berg 		/* If all TIDs are active - finish check on this queue */
120199448a8cSJohannes Berg 		if (!inactive_tid_bitmap)
120299448a8cSJohannes Berg 			continue;
120399448a8cSJohannes Berg 
120499448a8cSJohannes Berg 		/*
120599448a8cSJohannes Berg 		 * If we are here - the queue hadn't been served recently and is
120699448a8cSJohannes Berg 		 * in use
120799448a8cSJohannes Berg 		 */
120899448a8cSJohannes Berg 
120999448a8cSJohannes Berg 		sta_id = mvm->queue_info[i].ra_sta_id;
121099448a8cSJohannes Berg 		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
121199448a8cSJohannes Berg 
121299448a8cSJohannes Berg 		/*
121399448a8cSJohannes Berg 		 * If the STA doesn't exist anymore, it isn't an error. It could
121499448a8cSJohannes Berg 		 * be that it was removed since getting the queues, and in this
121599448a8cSJohannes Berg 		 * case it should've inactivated its queues anyway.
121699448a8cSJohannes Berg 		 */
121799448a8cSJohannes Berg 		if (IS_ERR_OR_NULL(sta))
121899448a8cSJohannes Berg 			continue;
121999448a8cSJohannes Berg 
122099448a8cSJohannes Berg 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
122199448a8cSJohannes Berg 
1222f3f240f9SJohannes Berg 		spin_lock_bh(&mvmsta->lock);
1223724fe771SJohannes Berg 		ret = iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
122490d2d94cSJohannes Berg 						   inactive_tid_bitmap,
1225b3a87f11SJohannes Berg 						   &unshare_queues,
1226b3a87f11SJohannes Berg 						   &changetid_queues);
1227e6d419f9SJohannes Berg 		if (ret && free_queue < 0) {
1228cfbc6c4cSSara Sharon 			queue_owner = sta;
1229e6d419f9SJohannes Berg 			free_queue = i;
1230cfbc6c4cSSara Sharon 		}
1231459ab045SJohannes Berg 		/* only unlock sta lock - we still need the queue info lock */
1232f3f240f9SJohannes Berg 		spin_unlock_bh(&mvmsta->lock);
123399448a8cSJohannes Berg 	}
123499448a8cSJohannes Berg 
1235df2a2245SJohannes Berg 
1236df2a2245SJohannes Berg 	/* Reconfigure queues requiring reconfiguation */
123790d2d94cSJohannes Berg 	for_each_set_bit(i, &unshare_queues, IWL_MAX_HW_QUEUES)
123890d2d94cSJohannes Berg 		iwl_mvm_unshare_queue(mvm, i);
1239b3a87f11SJohannes Berg 	for_each_set_bit(i, &changetid_queues, IWL_MAX_HW_QUEUES)
1240b3a87f11SJohannes Berg 		iwl_mvm_change_queue_tid(mvm, i);
1241724fe771SJohannes Berg 
1242fbb1461aSJohannes Berg 	rcu_read_unlock();
1243fbb1461aSJohannes Berg 
1244724fe771SJohannes Berg 	if (free_queue >= 0 && alloc_for_sta != IWL_MVM_INVALID_STA) {
1245cfbc6c4cSSara Sharon 		ret = iwl_mvm_free_inactive_queue(mvm, free_queue, queue_owner,
1246724fe771SJohannes Berg 						  alloc_for_sta);
1247fbb1461aSJohannes Berg 		if (ret)
1248724fe771SJohannes Berg 			return ret;
1249724fe771SJohannes Berg 	}
1250724fe771SJohannes Berg 
1251724fe771SJohannes Berg 	return free_queue;
125299448a8cSJohannes Berg }
125399448a8cSJohannes Berg 
1254c20e08b0SJohannes Berg static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
1255cfbc6c4cSSara Sharon 				   struct ieee80211_sta *sta, u8 ac, int tid)
1256c20e08b0SJohannes Berg {
1257c20e08b0SJohannes Berg 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1258c20e08b0SJohannes Berg 	struct iwl_trans_txq_scd_cfg cfg = {
1259c20e08b0SJohannes Berg 		.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
1260c20e08b0SJohannes Berg 		.sta_id = mvmsta->sta_id,
1261c20e08b0SJohannes Berg 		.tid = tid,
1262c20e08b0SJohannes Berg 		.frame_limit = IWL_FRAME_LIMIT,
1263c20e08b0SJohannes Berg 	};
1264c20e08b0SJohannes Berg 	unsigned int wdg_timeout =
1265c20e08b0SJohannes Berg 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1266c20e08b0SJohannes Berg 	int queue = -1;
1267c6ce1c74SJohannes Berg 	u16 queue_tmp;
1268c20e08b0SJohannes Berg 	unsigned long disable_agg_tids = 0;
1269c20e08b0SJohannes Berg 	enum iwl_mvm_agg_state queue_state;
1270c20e08b0SJohannes Berg 	bool shared_queue = false, inc_ssn;
1271c20e08b0SJohannes Berg 	int ssn;
1272c20e08b0SJohannes Berg 	unsigned long tfd_queue_mask;
1273c20e08b0SJohannes Berg 	int ret;
1274c20e08b0SJohannes Berg 
1275c20e08b0SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
1276c20e08b0SJohannes Berg 
1277c20e08b0SJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm))
1278c20e08b0SJohannes Berg 		return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
1279c20e08b0SJohannes Berg 
1280c20e08b0SJohannes Berg 	spin_lock_bh(&mvmsta->lock);
1281c20e08b0SJohannes Berg 	tfd_queue_mask = mvmsta->tfd_queue_msk;
128235739348SSara Sharon 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1283c20e08b0SJohannes Berg 	spin_unlock_bh(&mvmsta->lock);
1284c20e08b0SJohannes Berg 
1285cfbc6c4cSSara Sharon 	if (tid == IWL_MAX_TID_COUNT) {
1286c20e08b0SJohannes Berg 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1287c20e08b0SJohannes Berg 						IWL_MVM_DQA_MIN_MGMT_QUEUE,
1288c20e08b0SJohannes Berg 						IWL_MVM_DQA_MAX_MGMT_QUEUE);
1289c20e08b0SJohannes Berg 		if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
1290c20e08b0SJohannes Berg 			IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
1291c20e08b0SJohannes Berg 					    queue);
1292c20e08b0SJohannes Berg 
1293c20e08b0SJohannes Berg 		/* If no such queue is found, we'll use a DATA queue instead */
1294c20e08b0SJohannes Berg 	}
1295c20e08b0SJohannes Berg 
1296c20e08b0SJohannes Berg 	if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
1297c20e08b0SJohannes Berg 	    (mvm->queue_info[mvmsta->reserved_queue].status ==
1298724fe771SJohannes Berg 			IWL_MVM_QUEUE_RESERVED)) {
1299c20e08b0SJohannes Berg 		queue = mvmsta->reserved_queue;
1300c20e08b0SJohannes Berg 		mvm->queue_info[queue].reserved = true;
1301c20e08b0SJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
1302c20e08b0SJohannes Berg 	}
1303c20e08b0SJohannes Berg 
1304c20e08b0SJohannes Berg 	if (queue < 0)
1305c20e08b0SJohannes Berg 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1306c20e08b0SJohannes Berg 						IWL_MVM_DQA_MIN_DATA_QUEUE,
1307c20e08b0SJohannes Berg 						IWL_MVM_DQA_MAX_DATA_QUEUE);
1308724fe771SJohannes Berg 	if (queue < 0) {
1309724fe771SJohannes Berg 		/* try harder - perhaps kill an inactive queue */
1310724fe771SJohannes Berg 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1311c20e08b0SJohannes Berg 	}
1312c20e08b0SJohannes Berg 
1313c20e08b0SJohannes Berg 	/* No free queue - we'll have to share */
1314c20e08b0SJohannes Berg 	if (queue <= 0) {
1315c20e08b0SJohannes Berg 		queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
1316c20e08b0SJohannes Berg 		if (queue > 0) {
1317c20e08b0SJohannes Berg 			shared_queue = true;
1318c20e08b0SJohannes Berg 			mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
1319c20e08b0SJohannes Berg 		}
1320c20e08b0SJohannes Berg 	}
1321c20e08b0SJohannes Berg 
1322c20e08b0SJohannes Berg 	/*
1323c20e08b0SJohannes Berg 	 * Mark TXQ as ready, even though it hasn't been fully configured yet,
1324c20e08b0SJohannes Berg 	 * to make sure no one else takes it.
1325c20e08b0SJohannes Berg 	 * This will allow avoiding re-acquiring the lock at the end of the
1326c20e08b0SJohannes Berg 	 * configuration. On error we'll mark it back as free.
1327c20e08b0SJohannes Berg 	 */
1328c20e08b0SJohannes Berg 	if (queue > 0 && !shared_queue)
1329c20e08b0SJohannes Berg 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1330c20e08b0SJohannes Berg 
1331c20e08b0SJohannes Berg 	/* This shouldn't happen - out of queues */
1332c20e08b0SJohannes Berg 	if (WARN_ON(queue <= 0)) {
1333c20e08b0SJohannes Berg 		IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
1334c20e08b0SJohannes Berg 			tid, cfg.sta_id);
1335c20e08b0SJohannes Berg 		return queue;
1336c20e08b0SJohannes Berg 	}
1337c20e08b0SJohannes Berg 
1338c20e08b0SJohannes Berg 	/*
1339c20e08b0SJohannes Berg 	 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
1340c20e08b0SJohannes Berg 	 * but for configuring the SCD to send A-MPDUs we need to mark the queue
1341c20e08b0SJohannes Berg 	 * as aggregatable.
1342c20e08b0SJohannes Berg 	 * Mark all DATA queues as allowing to be aggregated at some point
1343c20e08b0SJohannes Berg 	 */
1344c20e08b0SJohannes Berg 	cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1345c20e08b0SJohannes Berg 			 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1346c20e08b0SJohannes Berg 
1347c20e08b0SJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
1348c20e08b0SJohannes Berg 			    "Allocating %squeue #%d to sta %d on tid %d\n",
1349c20e08b0SJohannes Berg 			    shared_queue ? "shared " : "", queue,
1350c20e08b0SJohannes Berg 			    mvmsta->sta_id, tid);
1351c20e08b0SJohannes Berg 
1352c20e08b0SJohannes Berg 	if (shared_queue) {
1353c20e08b0SJohannes Berg 		/* Disable any open aggs on this queue */
1354c20e08b0SJohannes Berg 		disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
1355c20e08b0SJohannes Berg 
1356c20e08b0SJohannes Berg 		if (disable_agg_tids) {
1357c20e08b0SJohannes Berg 			IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
1358c20e08b0SJohannes Berg 					    queue);
1359c20e08b0SJohannes Berg 			iwl_mvm_invalidate_sta_queue(mvm, queue,
1360c20e08b0SJohannes Berg 						     disable_agg_tids, false);
1361c20e08b0SJohannes Berg 		}
1362c20e08b0SJohannes Berg 	}
1363c20e08b0SJohannes Berg 
1364cfbc6c4cSSara Sharon 	inc_ssn = iwl_mvm_enable_txq(mvm, sta, queue, ssn, &cfg, wdg_timeout);
1365c20e08b0SJohannes Berg 
1366c20e08b0SJohannes Berg 	/*
1367c20e08b0SJohannes Berg 	 * Mark queue as shared in transport if shared
1368c20e08b0SJohannes Berg 	 * Note this has to be done after queue enablement because enablement
1369c20e08b0SJohannes Berg 	 * can also set this value, and there is no indication there to shared
1370c20e08b0SJohannes Berg 	 * queues
1371c20e08b0SJohannes Berg 	 */
1372c20e08b0SJohannes Berg 	if (shared_queue)
1373c20e08b0SJohannes Berg 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
1374c20e08b0SJohannes Berg 
1375c20e08b0SJohannes Berg 	spin_lock_bh(&mvmsta->lock);
1376c20e08b0SJohannes Berg 	/*
1377c20e08b0SJohannes Berg 	 * This looks racy, but it is not. We have only one packet for
1378c20e08b0SJohannes Berg 	 * this ra/tid in our Tx path since we stop the Qdisc when we
1379c20e08b0SJohannes Berg 	 * need to allocate a new TFD queue.
1380c20e08b0SJohannes Berg 	 */
138135739348SSara Sharon 	if (inc_ssn) {
1382c20e08b0SJohannes Berg 		mvmsta->tid_data[tid].seq_number += 0x10;
138335739348SSara Sharon 		ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
138435739348SSara Sharon 	}
1385c20e08b0SJohannes Berg 	mvmsta->tid_data[tid].txq_id = queue;
1386c20e08b0SJohannes Berg 	mvmsta->tfd_queue_msk |= BIT(queue);
1387c20e08b0SJohannes Berg 	queue_state = mvmsta->tid_data[tid].state;
1388c20e08b0SJohannes Berg 
1389c20e08b0SJohannes Berg 	if (mvmsta->reserved_queue == queue)
1390c20e08b0SJohannes Berg 		mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
1391c20e08b0SJohannes Berg 	spin_unlock_bh(&mvmsta->lock);
1392c20e08b0SJohannes Berg 
1393c20e08b0SJohannes Berg 	if (!shared_queue) {
1394c20e08b0SJohannes Berg 		ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
1395c20e08b0SJohannes Berg 		if (ret)
1396c20e08b0SJohannes Berg 			goto out_err;
1397c20e08b0SJohannes Berg 
1398c20e08b0SJohannes Berg 		/* If we need to re-enable aggregations... */
1399c20e08b0SJohannes Berg 		if (queue_state == IWL_AGG_ON) {
1400c20e08b0SJohannes Berg 			ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1401c20e08b0SJohannes Berg 			if (ret)
1402c20e08b0SJohannes Berg 				goto out_err;
1403c20e08b0SJohannes Berg 		}
1404c20e08b0SJohannes Berg 	} else {
1405c20e08b0SJohannes Berg 		/* Redirect queue, if needed */
1406cfbc6c4cSSara Sharon 		ret = iwl_mvm_redirect_queue(mvm, queue, tid, ac, ssn,
1407cfbc6c4cSSara Sharon 					     wdg_timeout, false,
1408cfbc6c4cSSara Sharon 					     iwl_mvm_txq_from_tid(sta, tid));
1409c20e08b0SJohannes Berg 		if (ret)
1410c20e08b0SJohannes Berg 			goto out_err;
1411c20e08b0SJohannes Berg 	}
1412c20e08b0SJohannes Berg 
1413c20e08b0SJohannes Berg 	return 0;
1414c20e08b0SJohannes Berg 
1415c20e08b0SJohannes Berg out_err:
1416c6ce1c74SJohannes Berg 	queue_tmp = queue;
1417c5a976cfSJohannes Berg 	iwl_mvm_disable_txq(mvm, sta, mvmsta->sta_id, &queue_tmp, tid);
1418c20e08b0SJohannes Berg 
1419c20e08b0SJohannes Berg 	return ret;
1420c20e08b0SJohannes Berg }
1421c20e08b0SJohannes Berg 
142224afba76SLiad Kaufman void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
142324afba76SLiad Kaufman {
142424afba76SLiad Kaufman 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
142524afba76SLiad Kaufman 					   add_stream_wk);
142624afba76SLiad Kaufman 
142724afba76SLiad Kaufman 	mutex_lock(&mvm->mutex);
142824afba76SLiad Kaufman 
1429724fe771SJohannes Berg 	iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
14309f9af3d7SLiad Kaufman 
1431cfbc6c4cSSara Sharon 	while (!list_empty(&mvm->add_stream_txqs)) {
1432cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq;
1433cfbc6c4cSSara Sharon 		struct ieee80211_txq *txq;
1434cfbc6c4cSSara Sharon 		u8 tid;
143524afba76SLiad Kaufman 
1436cfbc6c4cSSara Sharon 		mvmtxq = list_first_entry(&mvm->add_stream_txqs,
1437cfbc6c4cSSara Sharon 					  struct iwl_mvm_txq, list);
143824afba76SLiad Kaufman 
1439cfbc6c4cSSara Sharon 		txq = container_of((void *)mvmtxq, struct ieee80211_txq,
1440cfbc6c4cSSara Sharon 				   drv_priv);
1441cfbc6c4cSSara Sharon 		tid = txq->tid;
1442cfbc6c4cSSara Sharon 		if (tid == IEEE80211_NUM_TIDS)
1443cfbc6c4cSSara Sharon 			tid = IWL_MAX_TID_COUNT;
1444cfbc6c4cSSara Sharon 
14453d1d87abSJohannes Berg 		/*
14463d1d87abSJohannes Berg 		 * We can't really do much here, but if this fails we can't
14473d1d87abSJohannes Berg 		 * transmit anyway - so just don't transmit the frame etc.
14483d1d87abSJohannes Berg 		 * and let them back up ... we've tried our best to allocate
14493d1d87abSJohannes Berg 		 * a queue in the function itself.
14503d1d87abSJohannes Berg 		 */
14513d1d87abSJohannes Berg 		if (iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, tid)) {
14523d1d87abSJohannes Berg 			list_del_init(&mvmtxq->list);
14533d1d87abSJohannes Berg 			continue;
14543d1d87abSJohannes Berg 		}
14553d1d87abSJohannes Berg 
1456cfbc6c4cSSara Sharon 		list_del_init(&mvmtxq->list);
1457f5ae2f93SJohannes Berg 		local_bh_disable();
1458cfbc6c4cSSara Sharon 		iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1459f5ae2f93SJohannes Berg 		local_bh_enable();
146024afba76SLiad Kaufman 	}
146124afba76SLiad Kaufman 
146224afba76SLiad Kaufman 	mutex_unlock(&mvm->mutex);
146324afba76SLiad Kaufman }
146424afba76SLiad Kaufman 
146524afba76SLiad Kaufman static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1466d5216a28SLiad Kaufman 				      struct ieee80211_sta *sta,
1467d5216a28SLiad Kaufman 				      enum nl80211_iftype vif_type)
146824afba76SLiad Kaufman {
146924afba76SLiad Kaufman 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
147024afba76SLiad Kaufman 	int queue;
147124afba76SLiad Kaufman 
1472396952eeSSara Sharon 	/* queue reserving is disabled on new TX path */
1473396952eeSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1474396952eeSSara Sharon 		return 0;
1475396952eeSSara Sharon 
1476724fe771SJohannes Berg 	/* run the general cleanup/unsharing of queues */
1477724fe771SJohannes Berg 	iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
14789794c64fSLiad Kaufman 
147924afba76SLiad Kaufman 	/* Make sure we have free resources for this STA */
1480d5216a28SLiad Kaufman 	if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
14811c14089eSJohannes Berg 	    !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].tid_bitmap &&
1482cf961e16SLiad Kaufman 	    (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1483cf961e16SLiad Kaufman 	     IWL_MVM_QUEUE_FREE))
1484d5216a28SLiad Kaufman 		queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1485d5216a28SLiad Kaufman 	else
14869794c64fSLiad Kaufman 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
14879794c64fSLiad Kaufman 						IWL_MVM_DQA_MIN_DATA_QUEUE,
148824afba76SLiad Kaufman 						IWL_MVM_DQA_MAX_DATA_QUEUE);
148924afba76SLiad Kaufman 	if (queue < 0) {
1490724fe771SJohannes Berg 		/* try again - this time kick out a queue if needed */
1491724fe771SJohannes Berg 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1492724fe771SJohannes Berg 		if (queue < 0) {
149324afba76SLiad Kaufman 			IWL_ERR(mvm, "No available queues for new station\n");
149424afba76SLiad Kaufman 			return -ENOSPC;
1495724fe771SJohannes Berg 		}
149624afba76SLiad Kaufman 	}
1497cf961e16SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
149824afba76SLiad Kaufman 
149924afba76SLiad Kaufman 	mvmsta->reserved_queue = queue;
150024afba76SLiad Kaufman 
150124afba76SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
150224afba76SLiad Kaufman 			    queue, mvmsta->sta_id);
150324afba76SLiad Kaufman 
150424afba76SLiad Kaufman 	return 0;
150524afba76SLiad Kaufman }
150624afba76SLiad Kaufman 
15078d98ae6eSLiad Kaufman /*
15088d98ae6eSLiad Kaufman  * In DQA mode, after a HW restart the queues should be allocated as before, in
15098d98ae6eSLiad Kaufman  * order to avoid race conditions when there are shared queues. This function
15108d98ae6eSLiad Kaufman  * does the re-mapping and queue allocation.
15118d98ae6eSLiad Kaufman  *
15128d98ae6eSLiad Kaufman  * Note that re-enabling aggregations isn't done in this function.
15138d98ae6eSLiad Kaufman  */
15148d98ae6eSLiad Kaufman static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1515cfbc6c4cSSara Sharon 						 struct ieee80211_sta *sta)
15168d98ae6eSLiad Kaufman {
1517cfbc6c4cSSara Sharon 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1518cfbc6c4cSSara Sharon 	unsigned int wdg =
15198d98ae6eSLiad Kaufman 		iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
15208d98ae6eSLiad Kaufman 	int i;
15218d98ae6eSLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
15228d98ae6eSLiad Kaufman 		.sta_id = mvm_sta->sta_id,
15238d98ae6eSLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
15248d98ae6eSLiad Kaufman 	};
15258d98ae6eSLiad Kaufman 
152603c902bfSJohannes Berg 	/* Make sure reserved queue is still marked as such (if allocated) */
152703c902bfSJohannes Berg 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
15288d98ae6eSLiad Kaufman 		mvm->queue_info[mvm_sta->reserved_queue].status =
15298d98ae6eSLiad Kaufman 			IWL_MVM_QUEUE_RESERVED;
15308d98ae6eSLiad Kaufman 
15318d98ae6eSLiad Kaufman 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
15328d98ae6eSLiad Kaufman 		struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
15338d98ae6eSLiad Kaufman 		int txq_id = tid_data->txq_id;
15348d98ae6eSLiad Kaufman 		int ac;
15358d98ae6eSLiad Kaufman 
15366862fceeSSara Sharon 		if (txq_id == IWL_MVM_INVALID_QUEUE)
15378d98ae6eSLiad Kaufman 			continue;
15388d98ae6eSLiad Kaufman 
15398d98ae6eSLiad Kaufman 		ac = tid_to_mac80211_ac[i];
15408d98ae6eSLiad Kaufman 
1541310181ecSSara Sharon 		if (iwl_mvm_has_new_tx_api(mvm)) {
1542310181ecSSara Sharon 			IWL_DEBUG_TX_QUEUES(mvm,
1543310181ecSSara Sharon 					    "Re-mapping sta %d tid %d\n",
1544310181ecSSara Sharon 					    mvm_sta->sta_id, i);
154509369983SJohannes Berg 			txq_id = iwl_mvm_tvqm_enable_txq(mvm, sta,
154609369983SJohannes Berg 							 mvm_sta->sta_id,
1547cfbc6c4cSSara Sharon 							 i, wdg);
154891cf5dedSJohannes Berg 			/*
154991cf5dedSJohannes Berg 			 * on failures, just set it to IWL_MVM_INVALID_QUEUE
155091cf5dedSJohannes Berg 			 * to try again later, we have no other good way of
155191cf5dedSJohannes Berg 			 * failing here
155291cf5dedSJohannes Berg 			 */
155391cf5dedSJohannes Berg 			if (txq_id < 0)
155491cf5dedSJohannes Berg 				txq_id = IWL_MVM_INVALID_QUEUE;
1555310181ecSSara Sharon 			tid_data->txq_id = txq_id;
15565d39051aSLiad Kaufman 
15575d39051aSLiad Kaufman 			/*
15585d39051aSLiad Kaufman 			 * Since we don't set the seq number after reset, and HW
15595d39051aSLiad Kaufman 			 * sets it now, FW reset will cause the seq num to start
15605d39051aSLiad Kaufman 			 * at 0 again, so driver will need to update it
15615d39051aSLiad Kaufman 			 * internally as well, so it keeps in sync with real val
15625d39051aSLiad Kaufman 			 */
15635d39051aSLiad Kaufman 			tid_data->seq_number = 0;
1564310181ecSSara Sharon 		} else {
1565310181ecSSara Sharon 			u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1566310181ecSSara Sharon 
15678d98ae6eSLiad Kaufman 			cfg.tid = i;
1568cf6c6ea3SEmmanuel Grumbach 			cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
15698d98ae6eSLiad Kaufman 			cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1570310181ecSSara Sharon 					 txq_id ==
1571310181ecSSara Sharon 					 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
15728d98ae6eSLiad Kaufman 
15738d98ae6eSLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm,
15748d98ae6eSLiad Kaufman 					    "Re-mapping sta %d tid %d to queue %d\n",
15758d98ae6eSLiad Kaufman 					    mvm_sta->sta_id, i, txq_id);
15768d98ae6eSLiad Kaufman 
1577cfbc6c4cSSara Sharon 			iwl_mvm_enable_txq(mvm, sta, txq_id, seq, &cfg, wdg);
15788d98ae6eSLiad Kaufman 			mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
15798d98ae6eSLiad Kaufman 		}
158034e10860SSara Sharon 	}
15818d98ae6eSLiad Kaufman }
15828d98ae6eSLiad Kaufman 
1583732d06e9SShaul Triebitz static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1584732d06e9SShaul Triebitz 				      struct iwl_mvm_int_sta *sta,
1585732d06e9SShaul Triebitz 				      const u8 *addr,
1586732d06e9SShaul Triebitz 				      u16 mac_id, u16 color)
1587732d06e9SShaul Triebitz {
1588732d06e9SShaul Triebitz 	struct iwl_mvm_add_sta_cmd cmd;
1589732d06e9SShaul Triebitz 	int ret;
15903f497de9SLuca Coelho 	u32 status = ADD_STA_SUCCESS;
1591732d06e9SShaul Triebitz 
1592732d06e9SShaul Triebitz 	lockdep_assert_held(&mvm->mutex);
1593732d06e9SShaul Triebitz 
1594732d06e9SShaul Triebitz 	memset(&cmd, 0, sizeof(cmd));
1595732d06e9SShaul Triebitz 	cmd.sta_id = sta->sta_id;
15962c2c3647SNathan Errera 
1597971cbe50SJohannes Berg 	if (iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA, 0) >= 12 &&
15982c2c3647SNathan Errera 	    sta->type == IWL_STA_AUX_ACTIVITY)
15992c2c3647SNathan Errera 		cmd.mac_id_n_color = cpu_to_le32(mac_id);
16002c2c3647SNathan Errera 	else
1601732d06e9SShaul Triebitz 		cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1602732d06e9SShaul Triebitz 								     color));
16032c2c3647SNathan Errera 
1604732d06e9SShaul Triebitz 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1605732d06e9SShaul Triebitz 		cmd.station_type = sta->type;
1606732d06e9SShaul Triebitz 
1607732d06e9SShaul Triebitz 	if (!iwl_mvm_has_new_tx_api(mvm))
1608732d06e9SShaul Triebitz 		cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1609732d06e9SShaul Triebitz 	cmd.tid_disable_tx = cpu_to_le16(0xffff);
1610732d06e9SShaul Triebitz 
1611732d06e9SShaul Triebitz 	if (addr)
1612732d06e9SShaul Triebitz 		memcpy(cmd.addr, addr, ETH_ALEN);
1613732d06e9SShaul Triebitz 
1614732d06e9SShaul Triebitz 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1615732d06e9SShaul Triebitz 					  iwl_mvm_add_sta_cmd_size(mvm),
1616732d06e9SShaul Triebitz 					  &cmd, &status);
1617732d06e9SShaul Triebitz 	if (ret)
1618732d06e9SShaul Triebitz 		return ret;
1619732d06e9SShaul Triebitz 
1620732d06e9SShaul Triebitz 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1621732d06e9SShaul Triebitz 	case ADD_STA_SUCCESS:
1622732d06e9SShaul Triebitz 		IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1623732d06e9SShaul Triebitz 		return 0;
1624732d06e9SShaul Triebitz 	default:
1625732d06e9SShaul Triebitz 		ret = -EIO;
1626732d06e9SShaul Triebitz 		IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1627732d06e9SShaul Triebitz 			status);
1628732d06e9SShaul Triebitz 		break;
1629732d06e9SShaul Triebitz 	}
1630732d06e9SShaul Triebitz 	return ret;
1631732d06e9SShaul Triebitz }
1632732d06e9SShaul Triebitz 
1633e705c121SKalle Valo int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1634e705c121SKalle Valo 		    struct ieee80211_vif *vif,
1635e705c121SKalle Valo 		    struct ieee80211_sta *sta)
1636e705c121SKalle Valo {
1637e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1638e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1639a571f5f6SSara Sharon 	struct iwl_mvm_rxq_dup_data *dup_data;
1640e705c121SKalle Valo 	int i, ret, sta_id;
1641732d06e9SShaul Triebitz 	bool sta_update = false;
1642732d06e9SShaul Triebitz 	unsigned int sta_flags = 0;
1643e705c121SKalle Valo 
1644e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1645e705c121SKalle Valo 
1646e705c121SKalle Valo 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1647e705c121SKalle Valo 		sta_id = iwl_mvm_find_free_sta_id(mvm,
1648e705c121SKalle Valo 						  ieee80211_vif_type_p2p(vif));
1649e705c121SKalle Valo 	else
1650e705c121SKalle Valo 		sta_id = mvm_sta->sta_id;
1651e705c121SKalle Valo 
16520ae98812SSara Sharon 	if (sta_id == IWL_MVM_INVALID_STA)
1653e705c121SKalle Valo 		return -ENOSPC;
1654e705c121SKalle Valo 
1655e705c121SKalle Valo 	spin_lock_init(&mvm_sta->lock);
1656e705c121SKalle Valo 
1657c8f54701SJohannes Berg 	/* if this is a HW restart re-alloc existing queues */
1658c8f54701SJohannes Berg 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1659732d06e9SShaul Triebitz 		struct iwl_mvm_int_sta tmp_sta = {
1660732d06e9SShaul Triebitz 			.sta_id = sta_id,
1661732d06e9SShaul Triebitz 			.type = mvm_sta->sta_type,
1662732d06e9SShaul Triebitz 		};
1663732d06e9SShaul Triebitz 
1664732d06e9SShaul Triebitz 		/*
1665732d06e9SShaul Triebitz 		 * First add an empty station since allocating
1666732d06e9SShaul Triebitz 		 * a queue requires a valid station
1667732d06e9SShaul Triebitz 		 */
1668732d06e9SShaul Triebitz 		ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1669732d06e9SShaul Triebitz 						 mvmvif->id, mvmvif->color);
1670732d06e9SShaul Triebitz 		if (ret)
1671732d06e9SShaul Triebitz 			goto err;
1672732d06e9SShaul Triebitz 
1673cfbc6c4cSSara Sharon 		iwl_mvm_realloc_queues_after_restart(mvm, sta);
1674732d06e9SShaul Triebitz 		sta_update = true;
1675732d06e9SShaul Triebitz 		sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
16768d98ae6eSLiad Kaufman 		goto update_fw;
16778d98ae6eSLiad Kaufman 	}
16788d98ae6eSLiad Kaufman 
1679e705c121SKalle Valo 	mvm_sta->sta_id = sta_id;
1680e705c121SKalle Valo 	mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1681e705c121SKalle Valo 						      mvmvif->color);
1682e705c121SKalle Valo 	mvm_sta->vif = vif;
1683286ca8ebSLuca Coelho 	if (!mvm->trans->trans_cfg->gen2)
1684e705c121SKalle Valo 		mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1685a58bb468SLiad Kaufman 	else
1686a58bb468SLiad Kaufman 		mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
1687e705c121SKalle Valo 	mvm_sta->tx_protection = 0;
1688e705c121SKalle Valo 	mvm_sta->tt_tx_protection = false;
1689ced19f26SSara Sharon 	mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
1690e705c121SKalle Valo 
1691e705c121SKalle Valo 	/* HW restart, don't assume the memory has been zeroed */
1692e705c121SKalle Valo 	mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1693e705c121SKalle Valo 	mvm_sta->tfd_queue_msk = 0;
1694e705c121SKalle Valo 
1695e705c121SKalle Valo 	/* for HW restart - reset everything but the sequence number */
169624afba76SLiad Kaufman 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1697e705c121SKalle Valo 		u16 seq = mvm_sta->tid_data[i].seq_number;
1698e705c121SKalle Valo 		memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1699e705c121SKalle Valo 		mvm_sta->tid_data[i].seq_number = seq;
170024afba76SLiad Kaufman 
170124afba76SLiad Kaufman 		/*
170224afba76SLiad Kaufman 		 * Mark all queues for this STA as unallocated and defer TX
170324afba76SLiad Kaufman 		 * frames until the queue is allocated
170424afba76SLiad Kaufman 		 */
17056862fceeSSara Sharon 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1706e705c121SKalle Valo 	}
1707cfbc6c4cSSara Sharon 
1708cfbc6c4cSSara Sharon 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1709cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
1710cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1711cfbc6c4cSSara Sharon 
1712cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1713cfbc6c4cSSara Sharon 		INIT_LIST_HEAD(&mvmtxq->list);
1714fba8248eSSara Sharon 		atomic_set(&mvmtxq->tx_request, 0);
1715cfbc6c4cSSara Sharon 	}
1716cfbc6c4cSSara Sharon 
1717e705c121SKalle Valo 	mvm_sta->agg_tids = 0;
1718e705c121SKalle Valo 
1719a571f5f6SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) &&
1720a571f5f6SSara Sharon 	    !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
172192c4dca6SJohannes Berg 		int q;
172292c4dca6SJohannes Berg 
1723a571f5f6SSara Sharon 		dup_data = kcalloc(mvm->trans->num_rx_queues,
172492c4dca6SJohannes Berg 				   sizeof(*dup_data), GFP_KERNEL);
1725a571f5f6SSara Sharon 		if (!dup_data)
1726a571f5f6SSara Sharon 			return -ENOMEM;
172792c4dca6SJohannes Berg 		/*
172892c4dca6SJohannes Berg 		 * Initialize all the last_seq values to 0xffff which can never
172992c4dca6SJohannes Berg 		 * compare equal to the frame's seq_ctrl in the check in
173092c4dca6SJohannes Berg 		 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
173192c4dca6SJohannes Berg 		 * number and fragmented packets don't reach that function.
173292c4dca6SJohannes Berg 		 *
173392c4dca6SJohannes Berg 		 * This thus allows receiving a packet with seqno 0 and the
173492c4dca6SJohannes Berg 		 * retry bit set as the very first packet on a new TID.
173592c4dca6SJohannes Berg 		 */
173692c4dca6SJohannes Berg 		for (q = 0; q < mvm->trans->num_rx_queues; q++)
173792c4dca6SJohannes Berg 			memset(dup_data[q].last_seq, 0xff,
173892c4dca6SJohannes Berg 			       sizeof(dup_data[q].last_seq));
1739a571f5f6SSara Sharon 		mvm_sta->dup_data = dup_data;
1740a571f5f6SSara Sharon 	}
1741a571f5f6SSara Sharon 
1742c8f54701SJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm)) {
1743d5216a28SLiad Kaufman 		ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1744d5216a28SLiad Kaufman 						 ieee80211_vif_type_p2p(vif));
174524afba76SLiad Kaufman 		if (ret)
174624afba76SLiad Kaufman 			goto err;
174724afba76SLiad Kaufman 	}
174824afba76SLiad Kaufman 
17499f66a397SGregory Greenman 	/*
17509f66a397SGregory Greenman 	 * if rs is registered with mac80211, then "add station" will be handled
17519f66a397SGregory Greenman 	 * via the corresponding ops, otherwise need to notify rate scaling here
17529f66a397SGregory Greenman 	 */
17534243edb4SEmmanuel Grumbach 	if (iwl_mvm_has_tlc_offload(mvm))
17549f66a397SGregory Greenman 		iwl_mvm_rs_add_sta(mvm, mvm_sta);
17550f8084cdSMordechay Goodstein 	else
1756f5d88fa3SGregory Greenman 		spin_lock_init(&mvm_sta->lq_sta.rs_drv.pers.lock);
17579f66a397SGregory Greenman 
17580dde2440SAvraham Stern 	iwl_mvm_toggle_tx_ant(mvm, &mvm_sta->tx_ant);
17590dde2440SAvraham Stern 
17608d98ae6eSLiad Kaufman update_fw:
1761732d06e9SShaul Triebitz 	ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
1762e705c121SKalle Valo 	if (ret)
1763e705c121SKalle Valo 		goto err;
1764e705c121SKalle Valo 
1765e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION) {
1766e705c121SKalle Valo 		if (!sta->tdls) {
17670ae98812SSara Sharon 			WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
1768e705c121SKalle Valo 			mvmvif->ap_sta_id = sta_id;
1769e705c121SKalle Valo 		} else {
17700ae98812SSara Sharon 			WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
1771e705c121SKalle Valo 		}
1772e705c121SKalle Valo 	}
1773e705c121SKalle Valo 
1774*c7eca79dSAvraham Stern 	if (!sta->tdls)
1775*c7eca79dSAvraham Stern 		iwl_mvm_time_sync_config(mvm, sta->addr,
1776*c7eca79dSAvraham Stern 					 IWL_TIME_SYNC_PROTOCOL_TM |
1777*c7eca79dSAvraham Stern 					 IWL_TIME_SYNC_PROTOCOL_FTM);
1778*c7eca79dSAvraham Stern 
1779e705c121SKalle Valo 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1780e705c121SKalle Valo 
1781e705c121SKalle Valo 	return 0;
1782e705c121SKalle Valo 
1783e705c121SKalle Valo err:
1784e705c121SKalle Valo 	return ret;
1785e705c121SKalle Valo }
1786e705c121SKalle Valo 
1787e705c121SKalle Valo int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1788e705c121SKalle Valo 		      bool drain)
1789e705c121SKalle Valo {
1790e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
1791e705c121SKalle Valo 	int ret;
1792e705c121SKalle Valo 	u32 status;
1793e705c121SKalle Valo 
1794e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1795e705c121SKalle Valo 
1796e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1797e705c121SKalle Valo 	cmd.sta_id = mvmsta->sta_id;
1798e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
1799e705c121SKalle Valo 	cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1800e705c121SKalle Valo 	cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1801e705c121SKalle Valo 
1802e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
1803854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1804854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
1805e705c121SKalle Valo 					  &cmd, &status);
1806e705c121SKalle Valo 	if (ret)
1807e705c121SKalle Valo 		return ret;
1808e705c121SKalle Valo 
1809837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1810e705c121SKalle Valo 	case ADD_STA_SUCCESS:
1811e705c121SKalle Valo 		IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1812e705c121SKalle Valo 			       mvmsta->sta_id);
1813e705c121SKalle Valo 		break;
1814e705c121SKalle Valo 	default:
1815e705c121SKalle Valo 		ret = -EIO;
1816e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1817e705c121SKalle Valo 			mvmsta->sta_id);
1818e705c121SKalle Valo 		break;
1819e705c121SKalle Valo 	}
1820e705c121SKalle Valo 
1821e705c121SKalle Valo 	return ret;
1822e705c121SKalle Valo }
1823e705c121SKalle Valo 
1824e705c121SKalle Valo /*
1825e705c121SKalle Valo  * Remove a station from the FW table. Before sending the command to remove
1826e705c121SKalle Valo  * the station validate that the station is indeed known to the driver (sanity
1827e705c121SKalle Valo  * only).
1828e705c121SKalle Valo  */
1829e705c121SKalle Valo static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1830e705c121SKalle Valo {
1831e705c121SKalle Valo 	struct ieee80211_sta *sta;
1832e705c121SKalle Valo 	struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1833e705c121SKalle Valo 		.sta_id = sta_id,
1834e705c121SKalle Valo 	};
1835e705c121SKalle Valo 	int ret;
1836e705c121SKalle Valo 
1837e705c121SKalle Valo 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1838e705c121SKalle Valo 					lockdep_is_held(&mvm->mutex));
1839e705c121SKalle Valo 
1840e705c121SKalle Valo 	/* Note: internal stations are marked as error values */
1841e705c121SKalle Valo 	if (!sta) {
1842e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid station id\n");
1843e705c121SKalle Valo 		return -EINVAL;
1844e705c121SKalle Valo 	}
1845e705c121SKalle Valo 
1846e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1847e705c121SKalle Valo 				   sizeof(rm_sta_cmd), &rm_sta_cmd);
1848e705c121SKalle Valo 	if (ret) {
1849e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1850e705c121SKalle Valo 		return ret;
1851e705c121SKalle Valo 	}
1852e705c121SKalle Valo 
1853e705c121SKalle Valo 	return 0;
1854e705c121SKalle Valo }
1855e705c121SKalle Valo 
185624afba76SLiad Kaufman static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
185724afba76SLiad Kaufman 				       struct ieee80211_vif *vif,
1858cfbc6c4cSSara Sharon 				       struct ieee80211_sta *sta)
185924afba76SLiad Kaufman {
1860cfbc6c4cSSara Sharon 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
186124afba76SLiad Kaufman 	int i;
186224afba76SLiad Kaufman 
186324afba76SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
186424afba76SLiad Kaufman 
186524afba76SLiad Kaufman 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
18666862fceeSSara Sharon 		if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
186724afba76SLiad Kaufman 			continue;
186824afba76SLiad Kaufman 
1869c5a976cfSJohannes Berg 		iwl_mvm_disable_txq(mvm, sta, mvm_sta->sta_id,
1870c5a976cfSJohannes Berg 				    &mvm_sta->tid_data[i].txq_id, i);
18716862fceeSSara Sharon 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
187224afba76SLiad Kaufman 	}
1873cfbc6c4cSSara Sharon 
1874cfbc6c4cSSara Sharon 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1875cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
1876cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1877cfbc6c4cSSara Sharon 
1878cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
187914a3aacfSJose Ignacio Tornos Martinez 		list_del_init(&mvmtxq->list);
1880cfbc6c4cSSara Sharon 	}
188124afba76SLiad Kaufman }
188224afba76SLiad Kaufman 
1883d6d517b7SSara Sharon int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1884d6d517b7SSara Sharon 				  struct iwl_mvm_sta *mvm_sta)
1885d6d517b7SSara Sharon {
1886bec9522aSSharon Dvir 	int i;
1887d6d517b7SSara Sharon 
1888d6d517b7SSara Sharon 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1889d6d517b7SSara Sharon 		u16 txq_id;
1890bec9522aSSharon Dvir 		int ret;
1891d6d517b7SSara Sharon 
1892d6d517b7SSara Sharon 		spin_lock_bh(&mvm_sta->lock);
1893d6d517b7SSara Sharon 		txq_id = mvm_sta->tid_data[i].txq_id;
1894d6d517b7SSara Sharon 		spin_unlock_bh(&mvm_sta->lock);
1895d6d517b7SSara Sharon 
1896d6d517b7SSara Sharon 		if (txq_id == IWL_MVM_INVALID_QUEUE)
1897d6d517b7SSara Sharon 			continue;
1898d6d517b7SSara Sharon 
1899d6d517b7SSara Sharon 		ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1900d6d517b7SSara Sharon 		if (ret)
1901bec9522aSSharon Dvir 			return ret;
1902d6d517b7SSara Sharon 	}
1903d6d517b7SSara Sharon 
1904bec9522aSSharon Dvir 	return 0;
1905d6d517b7SSara Sharon }
1906d6d517b7SSara Sharon 
1907e705c121SKalle Valo int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1908e705c121SKalle Valo 		   struct ieee80211_vif *vif,
1909e705c121SKalle Valo 		   struct ieee80211_sta *sta)
1910e705c121SKalle Valo {
1911e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1912e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
191394c3e614SSara Sharon 	u8 sta_id = mvm_sta->sta_id;
1914e705c121SKalle Valo 	int ret;
1915e705c121SKalle Valo 
1916e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1917e705c121SKalle Valo 
1918a571f5f6SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
1919a571f5f6SSara Sharon 		kfree(mvm_sta->dup_data);
1920a571f5f6SSara Sharon 
1921e705c121SKalle Valo 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1922e705c121SKalle Valo 	if (ret)
1923e705c121SKalle Valo 		return ret;
1924c8f54701SJohannes Berg 
1925e705c121SKalle Valo 	/* flush its queues here since we are freeing mvm_sta */
1926f9084775SNathan Errera 	ret = iwl_mvm_flush_sta(mvm, mvm_sta, false);
1927e705c121SKalle Valo 	if (ret)
1928e705c121SKalle Valo 		return ret;
1929d6d517b7SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
1930d6d517b7SSara Sharon 		ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1931d6d517b7SSara Sharon 	} else {
1932d6d517b7SSara Sharon 		u32 q_mask = mvm_sta->tfd_queue_msk;
1933d6d517b7SSara Sharon 
1934a1a57877SSara Sharon 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1935d6d517b7SSara Sharon 						     q_mask);
1936d6d517b7SSara Sharon 	}
1937e705c121SKalle Valo 	if (ret)
1938e705c121SKalle Valo 		return ret;
1939c8f54701SJohannes Berg 
1940e705c121SKalle Valo 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1941e705c121SKalle Valo 
1942cfbc6c4cSSara Sharon 	iwl_mvm_disable_sta_queues(mvm, vif, sta);
194356214749SLiad Kaufman 
194456214749SLiad Kaufman 	/* If there is a TXQ still marked as reserved - free it */
1945c8f54701SJohannes Berg 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1946a0315deaSLiad Kaufman 		u8 reserved_txq = mvm_sta->reserved_queue;
1947a0315deaSLiad Kaufman 		enum iwl_mvm_queue_status *status;
1948a0315deaSLiad Kaufman 
1949a0315deaSLiad Kaufman 		/*
1950a0315deaSLiad Kaufman 		 * If no traffic has gone through the reserved TXQ - it
1951a0315deaSLiad Kaufman 		 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1952a0315deaSLiad Kaufman 		 * should be manually marked as free again
1953a0315deaSLiad Kaufman 		 */
1954a0315deaSLiad Kaufman 		status = &mvm->queue_info[reserved_txq].status;
1955a0315deaSLiad Kaufman 		if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1956a0315deaSLiad Kaufman 			 (*status != IWL_MVM_QUEUE_FREE),
1957a0315deaSLiad Kaufman 			 "sta_id %d reserved txq %d status %d",
1958f3f240f9SJohannes Berg 			 sta_id, reserved_txq, *status))
1959a0315deaSLiad Kaufman 			return -EINVAL;
1960a0315deaSLiad Kaufman 
1961a0315deaSLiad Kaufman 		*status = IWL_MVM_QUEUE_FREE;
1962a0315deaSLiad Kaufman 	}
1963a0315deaSLiad Kaufman 
1964e3118ad7SLiad Kaufman 	if (vif->type == NL80211_IFTYPE_STATION &&
196594c3e614SSara Sharon 	    mvmvif->ap_sta_id == sta_id) {
1966e3118ad7SLiad Kaufman 		/* if associated - we can't remove the AP STA now */
1967f276e20bSJohannes Berg 		if (vif->cfg.assoc)
1968e705c121SKalle Valo 			return ret;
1969e705c121SKalle Valo 
19705c75a208SJohannes Berg 		/* first remove remaining keys */
19715c75a208SJohannes Berg 		iwl_mvm_sec_key_remove_ap(mvm, vif);
19725c75a208SJohannes Berg 
1973e705c121SKalle Valo 		/* unassoc - go ahead - remove the AP STA now */
19740ae98812SSara Sharon 		mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1975e705c121SKalle Valo 	}
1976e705c121SKalle Valo 
1977e705c121SKalle Valo 	/*
1978e705c121SKalle Valo 	 * This shouldn't happen - the TDLS channel switch should be canceled
1979e705c121SKalle Valo 	 * before the STA is removed.
1980e705c121SKalle Valo 	 */
198194c3e614SSara Sharon 	if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
19820ae98812SSara Sharon 		mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1983e705c121SKalle Valo 		cancel_delayed_work(&mvm->tdls_cs.dwork);
1984e705c121SKalle Valo 	}
1985e705c121SKalle Valo 
1986e705c121SKalle Valo 	/*
1987e705c121SKalle Valo 	 * Make sure that the tx response code sees the station as -EBUSY and
1988e705c121SKalle Valo 	 * calls the drain worker.
1989e705c121SKalle Valo 	 */
1990e705c121SKalle Valo 	spin_lock_bh(&mvm_sta->lock);
1991e705c121SKalle Valo 	spin_unlock_bh(&mvm_sta->lock);
1992e705c121SKalle Valo 
1993*c7eca79dSAvraham Stern 	iwl_mvm_time_sync_sta_rm(mvm, sta);
1994*c7eca79dSAvraham Stern 
1995e705c121SKalle Valo 	ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1996e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1997e705c121SKalle Valo 
1998e705c121SKalle Valo 	return ret;
1999e705c121SKalle Valo }
2000e705c121SKalle Valo 
2001e705c121SKalle Valo int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
2002e705c121SKalle Valo 		      struct ieee80211_vif *vif,
2003e705c121SKalle Valo 		      u8 sta_id)
2004e705c121SKalle Valo {
2005e705c121SKalle Valo 	int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
2006e705c121SKalle Valo 
2007e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2008e705c121SKalle Valo 
2009e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
2010e705c121SKalle Valo 	return ret;
2011e705c121SKalle Valo }
2012e705c121SKalle Valo 
20130e39eb03SChaya Rachel Ivgi int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
2014e705c121SKalle Valo 			     struct iwl_mvm_int_sta *sta,
2015ced19f26SSara Sharon 			     u32 qmask, enum nl80211_iftype iftype,
2016006c152aSMiri Korenblit 			     u8 type)
2017e705c121SKalle Valo {
2018df65c8d1SAvraham Stern 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
2019df65c8d1SAvraham Stern 	    sta->sta_id == IWL_MVM_INVALID_STA) {
2020e705c121SKalle Valo 		sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
20210ae98812SSara Sharon 		if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
2022e705c121SKalle Valo 			return -ENOSPC;
2023e705c121SKalle Valo 	}
2024e705c121SKalle Valo 
2025e705c121SKalle Valo 	sta->tfd_queue_msk = qmask;
2026ced19f26SSara Sharon 	sta->type = type;
2027e705c121SKalle Valo 
2028e705c121SKalle Valo 	/* put a non-NULL value so iterating over the stations won't stop */
2029e705c121SKalle Valo 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
2030e705c121SKalle Valo 	return 0;
2031e705c121SKalle Valo }
2032e705c121SKalle Valo 
203326d6c16bSSara Sharon void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
2034e705c121SKalle Valo {
2035e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
2036e705c121SKalle Valo 	memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
20370ae98812SSara Sharon 	sta->sta_id = IWL_MVM_INVALID_STA;
2038e705c121SKalle Valo }
2039e705c121SKalle Valo 
204091cf5dedSJohannes Berg static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 queue,
2041b13f43a4SEmmanuel Grumbach 					  u8 sta_id, u8 fifo)
2042e705c121SKalle Valo {
20439617040eSEmmanuel Grumbach 	unsigned int wdg_timeout =
20449617040eSEmmanuel Grumbach 		mvm->trans->trans_cfg->base_params->wd_timeout;
204528d0793eSLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
2046b13f43a4SEmmanuel Grumbach 		.fifo = fifo,
2047b13f43a4SEmmanuel Grumbach 		.sta_id = sta_id,
204828d0793eSLiad Kaufman 		.tid = IWL_MAX_TID_COUNT,
204928d0793eSLiad Kaufman 		.aggregate = false,
205028d0793eSLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
205128d0793eSLiad Kaufman 	};
205228d0793eSLiad Kaufman 
205391cf5dedSJohannes Berg 	WARN_ON(iwl_mvm_has_new_tx_api(mvm));
205491cf5dedSJohannes Berg 
205591cf5dedSJohannes Berg 	iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
205628d0793eSLiad Kaufman }
205791cf5dedSJohannes Berg 
205891cf5dedSJohannes Berg static int iwl_mvm_enable_aux_snif_queue_tvqm(struct iwl_mvm *mvm, u8 sta_id)
205991cf5dedSJohannes Berg {
20609617040eSEmmanuel Grumbach 	unsigned int wdg_timeout =
20619617040eSEmmanuel Grumbach 		mvm->trans->trans_cfg->base_params->wd_timeout;
206291cf5dedSJohannes Berg 
206391cf5dedSJohannes Berg 	WARN_ON(!iwl_mvm_has_new_tx_api(mvm));
206491cf5dedSJohannes Berg 
206509369983SJohannes Berg 	return iwl_mvm_tvqm_enable_txq(mvm, NULL, sta_id, IWL_MAX_TID_COUNT,
206691cf5dedSJohannes Berg 				       wdg_timeout);
206791cf5dedSJohannes Berg }
206891cf5dedSJohannes Berg 
206991cf5dedSJohannes Berg static int iwl_mvm_add_int_sta_with_queue(struct iwl_mvm *mvm, int macidx,
2070be82ecd3SAvraham Stern 					  int maccolor, u8 *addr,
207191cf5dedSJohannes Berg 					  struct iwl_mvm_int_sta *sta,
207291cf5dedSJohannes Berg 					  u16 *queue, int fifo)
207391cf5dedSJohannes Berg {
207491cf5dedSJohannes Berg 	int ret;
207591cf5dedSJohannes Berg 
207691cf5dedSJohannes Berg 	/* Map queue to fifo - needs to happen before adding station */
207791cf5dedSJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm))
207891cf5dedSJohannes Berg 		iwl_mvm_enable_aux_snif_queue(mvm, *queue, sta->sta_id, fifo);
207991cf5dedSJohannes Berg 
2080be82ecd3SAvraham Stern 	ret = iwl_mvm_add_int_sta_common(mvm, sta, addr, macidx, maccolor);
208191cf5dedSJohannes Berg 	if (ret) {
208291cf5dedSJohannes Berg 		if (!iwl_mvm_has_new_tx_api(mvm))
2083c5a976cfSJohannes Berg 			iwl_mvm_disable_txq(mvm, NULL, sta->sta_id, queue,
208464ff7eb0SJohannes Berg 					    IWL_MAX_TID_COUNT);
208591cf5dedSJohannes Berg 		return ret;
208691cf5dedSJohannes Berg 	}
208791cf5dedSJohannes Berg 
208891cf5dedSJohannes Berg 	/*
208991cf5dedSJohannes Berg 	 * For 22000 firmware and on we cannot add queue to a station unknown
209091cf5dedSJohannes Berg 	 * to firmware so enable queue here - after the station was added
209191cf5dedSJohannes Berg 	 */
209291cf5dedSJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm)) {
209391cf5dedSJohannes Berg 		int txq;
209491cf5dedSJohannes Berg 
209591cf5dedSJohannes Berg 		txq = iwl_mvm_enable_aux_snif_queue_tvqm(mvm, sta->sta_id);
209691cf5dedSJohannes Berg 		if (txq < 0) {
209791cf5dedSJohannes Berg 			iwl_mvm_rm_sta_common(mvm, sta->sta_id);
209891cf5dedSJohannes Berg 			return txq;
209991cf5dedSJohannes Berg 		}
210091cf5dedSJohannes Berg 
210191cf5dedSJohannes Berg 		*queue = txq;
210291cf5dedSJohannes Berg 	}
210391cf5dedSJohannes Berg 
210491cf5dedSJohannes Berg 	return 0;
2105c5a719eeSSara Sharon }
2106c5a719eeSSara Sharon 
21072c2c3647SNathan Errera int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm, u32 lmac_id)
2108c5a719eeSSara Sharon {
2109c5a719eeSSara Sharon 	int ret;
2110c5a719eeSSara Sharon 
2111c5a719eeSSara Sharon 	lockdep_assert_held(&mvm->mutex);
2112c5a719eeSSara Sharon 
2113c5a719eeSSara Sharon 	/* Allocate aux station and assign to it the aux queue */
2114c5a719eeSSara Sharon 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
2115ced19f26SSara Sharon 				       NL80211_IFTYPE_UNSPECIFIED,
2116ced19f26SSara Sharon 				       IWL_STA_AUX_ACTIVITY);
2117c5a719eeSSara Sharon 	if (ret)
2118c5a719eeSSara Sharon 		return ret;
2119c5a719eeSSara Sharon 
21202c2c3647SNathan Errera 	/*
21212c2c3647SNathan Errera 	 * In CDB NICs we need to specify which lmac to use for aux activity
21222c2c3647SNathan Errera 	 * using the mac_id argument place to send lmac_id to the function
21232c2c3647SNathan Errera 	 */
21242c2c3647SNathan Errera 	ret = iwl_mvm_add_int_sta_with_queue(mvm, lmac_id, 0, NULL,
212591cf5dedSJohannes Berg 					     &mvm->aux_sta, &mvm->aux_queue,
2126b13f43a4SEmmanuel Grumbach 					     IWL_MVM_TX_FIFO_MCAST);
2127c5a719eeSSara Sharon 	if (ret) {
2128e705c121SKalle Valo 		iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2129e705c121SKalle Valo 		return ret;
2130e705c121SKalle Valo 	}
2131e705c121SKalle Valo 
2132c5a719eeSSara Sharon 	return 0;
2133c5a719eeSSara Sharon }
2134c5a719eeSSara Sharon 
21350e39eb03SChaya Rachel Ivgi int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
21360e39eb03SChaya Rachel Ivgi {
21370e39eb03SChaya Rachel Ivgi 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
21380e39eb03SChaya Rachel Ivgi 
21390e39eb03SChaya Rachel Ivgi 	lockdep_assert_held(&mvm->mutex);
2140b13f43a4SEmmanuel Grumbach 
214191cf5dedSJohannes Berg 	return iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
2142be82ecd3SAvraham Stern 					      NULL, &mvm->snif_sta,
2143be82ecd3SAvraham Stern 					      &mvm->snif_queue,
2144b13f43a4SEmmanuel Grumbach 					      IWL_MVM_TX_FIFO_BE);
21450e39eb03SChaya Rachel Ivgi }
21460e39eb03SChaya Rachel Ivgi 
21470e39eb03SChaya Rachel Ivgi int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
21480e39eb03SChaya Rachel Ivgi {
21490e39eb03SChaya Rachel Ivgi 	int ret;
21500e39eb03SChaya Rachel Ivgi 
21510e39eb03SChaya Rachel Ivgi 	lockdep_assert_held(&mvm->mutex);
21520e39eb03SChaya Rachel Ivgi 
2153e223e42aSGregory Greenman 	if (WARN_ON_ONCE(mvm->snif_sta.sta_id == IWL_MVM_INVALID_STA))
2154e223e42aSGregory Greenman 		return -EINVAL;
2155e223e42aSGregory Greenman 
2156c5a976cfSJohannes Berg 	iwl_mvm_disable_txq(mvm, NULL, mvm->snif_sta.sta_id,
2157c5a976cfSJohannes Berg 			    &mvm->snif_queue, IWL_MAX_TID_COUNT);
21580e39eb03SChaya Rachel Ivgi 	ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
21590e39eb03SChaya Rachel Ivgi 	if (ret)
21600e39eb03SChaya Rachel Ivgi 		IWL_WARN(mvm, "Failed sending remove station\n");
21610e39eb03SChaya Rachel Ivgi 
21620e39eb03SChaya Rachel Ivgi 	return ret;
21630e39eb03SChaya Rachel Ivgi }
21640e39eb03SChaya Rachel Ivgi 
2165f327236dSSharon int iwl_mvm_rm_aux_sta(struct iwl_mvm *mvm)
2166f327236dSSharon {
2167f327236dSSharon 	int ret;
2168f327236dSSharon 
2169f327236dSSharon 	lockdep_assert_held(&mvm->mutex);
2170f327236dSSharon 
2171e223e42aSGregory Greenman 	if (WARN_ON_ONCE(mvm->aux_sta.sta_id == IWL_MVM_INVALID_STA))
2172e223e42aSGregory Greenman 		return -EINVAL;
2173e223e42aSGregory Greenman 
2174c5a976cfSJohannes Berg 	iwl_mvm_disable_txq(mvm, NULL, mvm->aux_sta.sta_id,
2175c5a976cfSJohannes Berg 			    &mvm->aux_queue, IWL_MAX_TID_COUNT);
2176f327236dSSharon 	ret = iwl_mvm_rm_sta_common(mvm, mvm->aux_sta.sta_id);
2177f327236dSSharon 	if (ret)
2178f327236dSSharon 		IWL_WARN(mvm, "Failed sending remove station\n");
2179f327236dSSharon 	iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2180f327236dSSharon 
2181f327236dSSharon 	return ret;
2182f327236dSSharon }
2183f327236dSSharon 
21840e39eb03SChaya Rachel Ivgi void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
21850e39eb03SChaya Rachel Ivgi {
21860e39eb03SChaya Rachel Ivgi 	iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
21870e39eb03SChaya Rachel Ivgi }
21880e39eb03SChaya Rachel Ivgi 
2189e705c121SKalle Valo /*
2190e705c121SKalle Valo  * Send the add station command for the vif's broadcast station.
2191e705c121SKalle Valo  * Assumes that the station was already allocated.
2192e705c121SKalle Valo  *
2193e705c121SKalle Valo  * @mvm: the mvm component
2194e705c121SKalle Valo  * @vif: the interface to which the broadcast station is added
2195e705c121SKalle Valo  * @bsta: the broadcast station to add.
2196e705c121SKalle Valo  */
2197e705c121SKalle Valo int iwl_mvm_send_add_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 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2201e705c121SKalle Valo 	static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2202e705c121SKalle Valo 	const u8 *baddr = _baddr;
22037daa7624SJohannes Berg 	int queue;
2204df88c08dSLiad Kaufman 	int ret;
2205c5a719eeSSara Sharon 	unsigned int wdg_timeout =
2206c5a719eeSSara Sharon 		iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2207de24f638SLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
2208de24f638SLiad Kaufman 		.fifo = IWL_MVM_TX_FIFO_VO,
2209de24f638SLiad Kaufman 		.sta_id = mvmvif->bcast_sta.sta_id,
2210de24f638SLiad Kaufman 		.tid = IWL_MAX_TID_COUNT,
2211de24f638SLiad Kaufman 		.aggregate = false,
2212de24f638SLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
2213de24f638SLiad Kaufman 	};
2214de24f638SLiad Kaufman 
2215c5a719eeSSara Sharon 	lockdep_assert_held(&mvm->mutex);
2216c5a719eeSSara Sharon 
2217c8f54701SJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm)) {
22184d339989SLiad Kaufman 		if (vif->type == NL80211_IFTYPE_AP ||
2219f8510d67SNathan Chancellor 		    vif->type == NL80211_IFTYPE_ADHOC) {
222049f71713SSara Sharon 			queue = mvm->probe_queue;
2221f8510d67SNathan Chancellor 		} else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
222249f71713SSara Sharon 			queue = mvm->p2p_dev_queue;
2223f8510d67SNathan Chancellor 		} else {
2224f8510d67SNathan Chancellor 			WARN(1, "Missing required TXQ for adding bcast STA\n");
2225de24f638SLiad Kaufman 			return -EINVAL;
2226f8510d67SNathan Chancellor 		}
2227de24f638SLiad Kaufman 
2228df88c08dSLiad Kaufman 		bsta->tfd_queue_msk |= BIT(queue);
2229c5a719eeSSara Sharon 
2230cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2231de24f638SLiad Kaufman 	}
2232de24f638SLiad Kaufman 
2233e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_ADHOC)
2234e705c121SKalle Valo 		baddr = vif->bss_conf.bssid;
2235e705c121SKalle Valo 
22360ae98812SSara Sharon 	if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
2237e705c121SKalle Valo 		return -ENOSPC;
2238e705c121SKalle Valo 
2239df88c08dSLiad Kaufman 	ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2240e705c121SKalle Valo 					 mvmvif->id, mvmvif->color);
2241df88c08dSLiad Kaufman 	if (ret)
2242df88c08dSLiad Kaufman 		return ret;
2243df88c08dSLiad Kaufman 
2244df88c08dSLiad Kaufman 	/*
22452f7a3863SLuca Coelho 	 * For 22000 firmware and on we cannot add queue to a station unknown
2246c5a719eeSSara Sharon 	 * to firmware so enable queue here - after the station was added
2247df88c08dSLiad Kaufman 	 */
2248310181ecSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
224909369983SJohannes Berg 		queue = iwl_mvm_tvqm_enable_txq(mvm, NULL, bsta->sta_id,
2250310181ecSSara Sharon 						IWL_MAX_TID_COUNT,
2251c5a719eeSSara Sharon 						wdg_timeout);
225291cf5dedSJohannes Berg 		if (queue < 0) {
225391cf5dedSJohannes Berg 			iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
225491cf5dedSJohannes Berg 			return queue;
225591cf5dedSJohannes Berg 		}
22567daa7624SJohannes Berg 
22577b758a11SLuca Coelho 		if (vif->type == NL80211_IFTYPE_AP ||
22587b758a11SLuca Coelho 		    vif->type == NL80211_IFTYPE_ADHOC)
2259310181ecSSara Sharon 			mvm->probe_queue = queue;
2260310181ecSSara Sharon 		else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
2261310181ecSSara Sharon 			mvm->p2p_dev_queue = queue;
2262310181ecSSara Sharon 	}
2263df88c08dSLiad Kaufman 
2264df88c08dSLiad Kaufman 	return 0;
2265df88c08dSLiad Kaufman }
2266df88c08dSLiad Kaufman 
226760efeca1SMiri Korenblit void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2268df88c08dSLiad Kaufman 				   struct ieee80211_vif *vif)
2269df88c08dSLiad Kaufman {
2270df88c08dSLiad Kaufman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2271c6ce1c74SJohannes Berg 	u16 *queueptr, queue;
2272df88c08dSLiad Kaufman 
2273df88c08dSLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
2274df88c08dSLiad Kaufman 
2275f9084775SNathan Errera 	iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true);
2276d49394a1SSara Sharon 
2277d167e81aSMordechai Goodstein 	switch (vif->type) {
2278d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_AP:
2279d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_ADHOC:
2280c6ce1c74SJohannes Berg 		queueptr = &mvm->probe_queue;
2281d167e81aSMordechai Goodstein 		break;
2282d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_P2P_DEVICE:
2283c6ce1c74SJohannes Berg 		queueptr = &mvm->p2p_dev_queue;
2284d167e81aSMordechai Goodstein 		break;
2285d167e81aSMordechai Goodstein 	default:
2286d167e81aSMordechai Goodstein 		WARN(1, "Can't free bcast queue on vif type %d\n",
2287d167e81aSMordechai Goodstein 		     vif->type);
2288d167e81aSMordechai Goodstein 		return;
2289df88c08dSLiad Kaufman 	}
2290df88c08dSLiad Kaufman 
2291c6ce1c74SJohannes Berg 	queue = *queueptr;
2292c5a976cfSJohannes Berg 	iwl_mvm_disable_txq(mvm, NULL, mvmvif->bcast_sta.sta_id,
2293c5a976cfSJohannes Berg 			    queueptr, IWL_MAX_TID_COUNT);
2294d167e81aSMordechai Goodstein 	if (iwl_mvm_has_new_tx_api(mvm))
2295d167e81aSMordechai Goodstein 		return;
2296d167e81aSMordechai Goodstein 
2297d167e81aSMordechai Goodstein 	WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
2298d167e81aSMordechai Goodstein 	mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
2299e705c121SKalle Valo }
2300e705c121SKalle Valo 
2301e705c121SKalle Valo /* Send the FW a request to remove the station from it's internal data
2302e705c121SKalle Valo  * structures, but DO NOT remove the entry from the local data structures. */
2303e705c121SKalle Valo int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2304e705c121SKalle Valo {
2305e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2306e705c121SKalle Valo 	int ret;
2307e705c121SKalle Valo 
2308e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2309e705c121SKalle Valo 
2310df88c08dSLiad Kaufman 	iwl_mvm_free_bcast_sta_queues(mvm, vif);
2311df88c08dSLiad Kaufman 
2312e705c121SKalle Valo 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
2313e705c121SKalle Valo 	if (ret)
2314e705c121SKalle Valo 		IWL_WARN(mvm, "Failed sending remove station\n");
2315e705c121SKalle Valo 	return ret;
2316e705c121SKalle Valo }
2317e705c121SKalle Valo 
2318e705c121SKalle Valo int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2319e705c121SKalle Valo {
2320e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2321e705c121SKalle Valo 
2322e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2323e705c121SKalle Valo 
2324c8f54701SJohannes Berg 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, 0,
2325ced19f26SSara Sharon 					ieee80211_vif_type_p2p(vif),
2326ced19f26SSara Sharon 					IWL_STA_GENERAL_PURPOSE);
2327e705c121SKalle Valo }
2328e705c121SKalle Valo 
2329e705c121SKalle Valo /* Allocate a new station entry for the broadcast station to the given vif,
2330e705c121SKalle Valo  * and send it to the FW.
2331e705c121SKalle Valo  * Note that each P2P mac should have its own broadcast station.
2332e705c121SKalle Valo  *
2333e705c121SKalle Valo  * @mvm: the mvm component
2334e705c121SKalle Valo  * @vif: the interface to which the broadcast station is added
2335e705c121SKalle Valo  * @bsta: the broadcast station to add. */
2336d197358bSLuca Coelho int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2337e705c121SKalle Valo {
2338e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2339e705c121SKalle Valo 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2340e705c121SKalle Valo 	int ret;
2341e705c121SKalle Valo 
2342e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2343e705c121SKalle Valo 
2344e705c121SKalle Valo 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2345e705c121SKalle Valo 	if (ret)
2346e705c121SKalle Valo 		return ret;
2347e705c121SKalle Valo 
2348e705c121SKalle Valo 	ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2349e705c121SKalle Valo 
2350e705c121SKalle Valo 	if (ret)
2351e705c121SKalle Valo 		iwl_mvm_dealloc_int_sta(mvm, bsta);
2352e705c121SKalle Valo 
2353e705c121SKalle Valo 	return ret;
2354e705c121SKalle Valo }
2355e705c121SKalle Valo 
2356e705c121SKalle Valo void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2357e705c121SKalle Valo {
2358e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2359e705c121SKalle Valo 
2360e705c121SKalle Valo 	iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2361e705c121SKalle Valo }
2362e705c121SKalle Valo 
2363e705c121SKalle Valo /*
2364e705c121SKalle Valo  * Send the FW a request to remove the station from it's internal data
2365e705c121SKalle Valo  * structures, and in addition remove it from the local data structure.
2366e705c121SKalle Valo  */
2367d197358bSLuca Coelho int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2368e705c121SKalle Valo {
2369e705c121SKalle Valo 	int ret;
2370e705c121SKalle Valo 
2371e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2372e705c121SKalle Valo 
2373e705c121SKalle Valo 	ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2374e705c121SKalle Valo 
2375e705c121SKalle Valo 	iwl_mvm_dealloc_bcast_sta(mvm, vif);
2376e705c121SKalle Valo 
2377e705c121SKalle Valo 	return ret;
2378e705c121SKalle Valo }
2379e705c121SKalle Valo 
238026d6c16bSSara Sharon /*
238126d6c16bSSara Sharon  * Allocate a new station entry for the multicast station to the given vif,
238226d6c16bSSara Sharon  * and send it to the FW.
238326d6c16bSSara Sharon  * Note that each AP/GO mac should have its own multicast station.
238426d6c16bSSara Sharon  *
238526d6c16bSSara Sharon  * @mvm: the mvm component
238626d6c16bSSara Sharon  * @vif: the interface to which the multicast station is added
238726d6c16bSSara Sharon  */
238826d6c16bSSara Sharon int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
238926d6c16bSSara Sharon {
239026d6c16bSSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
239126d6c16bSSara Sharon 	struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
239226d6c16bSSara Sharon 	static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
239326d6c16bSSara Sharon 	const u8 *maddr = _maddr;
239426d6c16bSSara Sharon 	struct iwl_trans_txq_scd_cfg cfg = {
2395192a7e1fSJohannes Berg 		.fifo = vif->type == NL80211_IFTYPE_AP ?
2396192a7e1fSJohannes Berg 			IWL_MVM_TX_FIFO_MCAST : IWL_MVM_TX_FIFO_BE,
239726d6c16bSSara Sharon 		.sta_id = msta->sta_id,
23986508de03SIlan Peer 		.tid = 0,
239926d6c16bSSara Sharon 		.aggregate = false,
240026d6c16bSSara Sharon 		.frame_limit = IWL_FRAME_LIMIT,
240126d6c16bSSara Sharon 	};
240226d6c16bSSara Sharon 	unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
240326d6c16bSSara Sharon 	int ret;
240426d6c16bSSara Sharon 
240526d6c16bSSara Sharon 	lockdep_assert_held(&mvm->mutex);
240626d6c16bSSara Sharon 
2407ee48b722SLiad Kaufman 	if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2408ee48b722SLiad Kaufman 		    vif->type != NL80211_IFTYPE_ADHOC))
240926d6c16bSSara Sharon 		return -ENOTSUPP;
241026d6c16bSSara Sharon 
2411ced19f26SSara Sharon 	/*
2412fc07bd8cSSara Sharon 	 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2413fc07bd8cSSara Sharon 	 * invalid, so make sure we use the queue we want.
2414fc07bd8cSSara Sharon 	 * Note that this is done here as we want to avoid making DQA
2415fc07bd8cSSara Sharon 	 * changes in mac80211 layer.
2416fc07bd8cSSara Sharon 	 */
2417cfbc6c4cSSara Sharon 	if (vif->type == NL80211_IFTYPE_ADHOC)
2418cfbc6c4cSSara Sharon 		mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2419fc07bd8cSSara Sharon 
2420fc07bd8cSSara Sharon 	/*
2421ced19f26SSara Sharon 	 * While in previous FWs we had to exclude cab queue from TFD queue
2422ced19f26SSara Sharon 	 * mask, now it is needed as any other queue.
2423ced19f26SSara Sharon 	 */
2424ced19f26SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm) &&
2425ced19f26SSara Sharon 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2426cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2427cfbc6c4cSSara Sharon 				   timeout);
2428cfbc6c4cSSara Sharon 		msta->tfd_queue_msk |= BIT(mvmvif->cab_queue);
2429ced19f26SSara Sharon 	}
243026d6c16bSSara Sharon 	ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
243126d6c16bSSara Sharon 					 mvmvif->id, mvmvif->color);
243291cf5dedSJohannes Berg 	if (ret)
243391cf5dedSJohannes Berg 		goto err;
243426d6c16bSSara Sharon 
243526d6c16bSSara Sharon 	/*
243626d6c16bSSara Sharon 	 * Enable cab queue after the ADD_STA command is sent.
24372f7a3863SLuca Coelho 	 * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
2438ced19f26SSara Sharon 	 * command with unknown station id, and for FW that doesn't support
2439ced19f26SSara Sharon 	 * station API since the cab queue is not included in the
2440ced19f26SSara Sharon 	 * tfd_queue_mask.
244126d6c16bSSara Sharon 	 */
2442310181ecSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
244309369983SJohannes Berg 		int queue = iwl_mvm_tvqm_enable_txq(mvm, NULL, msta->sta_id,
244409369983SJohannes Berg 						    0, timeout);
244591cf5dedSJohannes Berg 		if (queue < 0) {
244691cf5dedSJohannes Berg 			ret = queue;
244791cf5dedSJohannes Berg 			goto err;
244891cf5dedSJohannes Berg 		}
2449e2af3fabSSara Sharon 		mvmvif->cab_queue = queue;
2450ced19f26SSara Sharon 	} else if (!fw_has_api(&mvm->fw->ucode_capa,
2451fc07bd8cSSara Sharon 			       IWL_UCODE_TLV_API_STA_TYPE))
2452cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2453cfbc6c4cSSara Sharon 				   timeout);
245426d6c16bSSara Sharon 
245526d6c16bSSara Sharon 	return 0;
245691cf5dedSJohannes Berg err:
245791cf5dedSJohannes Berg 	iwl_mvm_dealloc_int_sta(mvm, msta);
245891cf5dedSJohannes Berg 	return ret;
245926d6c16bSSara Sharon }
246026d6c16bSSara Sharon 
246128916a16SEmmanuel Grumbach static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
246228916a16SEmmanuel Grumbach 				    struct ieee80211_key_conf *keyconf,
246328916a16SEmmanuel Grumbach 				    bool mcast)
246428916a16SEmmanuel Grumbach {
246528916a16SEmmanuel Grumbach 	union {
246628916a16SEmmanuel Grumbach 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
246728916a16SEmmanuel Grumbach 		struct iwl_mvm_add_sta_key_cmd cmd;
246828916a16SEmmanuel Grumbach 	} u = {};
246928916a16SEmmanuel Grumbach 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
247028916a16SEmmanuel Grumbach 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
247128916a16SEmmanuel Grumbach 	__le16 key_flags;
247228916a16SEmmanuel Grumbach 	int ret, size;
247328916a16SEmmanuel Grumbach 	u32 status;
247428916a16SEmmanuel Grumbach 
247528916a16SEmmanuel Grumbach 	/* This is a valid situation for GTK removal */
247628916a16SEmmanuel Grumbach 	if (sta_id == IWL_MVM_INVALID_STA)
247728916a16SEmmanuel Grumbach 		return 0;
247828916a16SEmmanuel Grumbach 
247928916a16SEmmanuel Grumbach 	key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
248028916a16SEmmanuel Grumbach 				 STA_KEY_FLG_KEYID_MSK);
248128916a16SEmmanuel Grumbach 	key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
248228916a16SEmmanuel Grumbach 	key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
248328916a16SEmmanuel Grumbach 
248428916a16SEmmanuel Grumbach 	if (mcast)
248528916a16SEmmanuel Grumbach 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
248628916a16SEmmanuel Grumbach 
248728916a16SEmmanuel Grumbach 	/*
248828916a16SEmmanuel Grumbach 	 * The fields assigned here are in the same location at the start
248928916a16SEmmanuel Grumbach 	 * of the command, so we can do this union trick.
249028916a16SEmmanuel Grumbach 	 */
249128916a16SEmmanuel Grumbach 	u.cmd.common.key_flags = key_flags;
249228916a16SEmmanuel Grumbach 	u.cmd.common.key_offset = keyconf->hw_key_idx;
249328916a16SEmmanuel Grumbach 	u.cmd.common.sta_id = sta_id;
249428916a16SEmmanuel Grumbach 
249528916a16SEmmanuel Grumbach 	size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
249628916a16SEmmanuel Grumbach 
249728916a16SEmmanuel Grumbach 	status = ADD_STA_SUCCESS;
249828916a16SEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
249928916a16SEmmanuel Grumbach 					  &status);
250028916a16SEmmanuel Grumbach 
250128916a16SEmmanuel Grumbach 	switch (status) {
250228916a16SEmmanuel Grumbach 	case ADD_STA_SUCCESS:
250328916a16SEmmanuel Grumbach 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
250428916a16SEmmanuel Grumbach 		break;
250528916a16SEmmanuel Grumbach 	default:
250628916a16SEmmanuel Grumbach 		ret = -EIO;
250728916a16SEmmanuel Grumbach 		IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
250828916a16SEmmanuel Grumbach 		break;
250928916a16SEmmanuel Grumbach 	}
251028916a16SEmmanuel Grumbach 
251128916a16SEmmanuel Grumbach 	return ret;
251228916a16SEmmanuel Grumbach }
251328916a16SEmmanuel Grumbach 
251426d6c16bSSara Sharon /*
251526d6c16bSSara Sharon  * Send the FW a request to remove the station from it's internal data
251626d6c16bSSara Sharon  * structures, and in addition remove it from the local data structure.
251726d6c16bSSara Sharon  */
251826d6c16bSSara Sharon int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
251926d6c16bSSara Sharon {
252026d6c16bSSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
252126d6c16bSSara Sharon 	int ret;
252226d6c16bSSara Sharon 
252326d6c16bSSara Sharon 	lockdep_assert_held(&mvm->mutex);
252426d6c16bSSara Sharon 
2525f9084775SNathan Errera 	iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true);
2526d49394a1SSara Sharon 
2527c5a976cfSJohannes Berg 	iwl_mvm_disable_txq(mvm, NULL, mvmvif->mcast_sta.sta_id,
2528c5a976cfSJohannes Berg 			    &mvmvif->cab_queue, 0);
252926d6c16bSSara Sharon 
253026d6c16bSSara Sharon 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
253126d6c16bSSara Sharon 	if (ret)
253226d6c16bSSara Sharon 		IWL_WARN(mvm, "Failed sending remove station\n");
253326d6c16bSSara Sharon 
253426d6c16bSSara Sharon 	return ret;
253526d6c16bSSara Sharon }
253626d6c16bSSara Sharon 
2537b915c101SSara Sharon static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
253810b2b201SSara Sharon {
25395e1688ceSJohannes Berg 	struct iwl_mvm_delba_data notif = {
25405e1688ceSJohannes Berg 		.baid = baid,
2541b915c101SSara Sharon 	};
25425e1688ceSJohannes Berg 
25435e1688ceSJohannes Berg 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_NOTIF_DEL_BA, true,
25445e1688ceSJohannes Berg 					&notif, sizeof(notif));
254510b2b201SSara Sharon };
254610b2b201SSara Sharon 
2547b915c101SSara Sharon static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2548b915c101SSara Sharon 				 struct iwl_mvm_baid_data *data)
2549b915c101SSara Sharon {
2550b915c101SSara Sharon 	int i;
2551b915c101SSara Sharon 
2552b915c101SSara Sharon 	iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2553b915c101SSara Sharon 
2554b915c101SSara Sharon 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2555b915c101SSara Sharon 		int j;
2556b915c101SSara Sharon 		struct iwl_mvm_reorder_buffer *reorder_buf =
2557b915c101SSara Sharon 			&data->reorder_buf[i];
2558dfdddd92SJohannes Berg 		struct iwl_mvm_reorder_buf_entry *entries =
2559dfdddd92SJohannes Berg 			&data->entries[i * data->entries_per_queue];
2560b915c101SSara Sharon 
25610690405fSSara Sharon 		spin_lock_bh(&reorder_buf->lock);
25620690405fSSara Sharon 		if (likely(!reorder_buf->num_stored)) {
25630690405fSSara Sharon 			spin_unlock_bh(&reorder_buf->lock);
2564b915c101SSara Sharon 			continue;
25650690405fSSara Sharon 		}
2566b915c101SSara Sharon 
2567b915c101SSara Sharon 		/*
2568b915c101SSara Sharon 		 * This shouldn't happen in regular DELBA since the internal
2569b915c101SSara Sharon 		 * delBA notification should trigger a release of all frames in
2570b915c101SSara Sharon 		 * the reorder buffer.
2571b915c101SSara Sharon 		 */
2572b915c101SSara Sharon 		WARN_ON(1);
2573b915c101SSara Sharon 
2574b915c101SSara Sharon 		for (j = 0; j < reorder_buf->buf_size; j++)
2575dfdddd92SJohannes Berg 			__skb_queue_purge(&entries[j].e.frames);
25760690405fSSara Sharon 		/*
25770690405fSSara Sharon 		 * Prevent timer re-arm. This prevents a very far fetched case
25780690405fSSara Sharon 		 * where we timed out on the notification. There may be prior
25790690405fSSara Sharon 		 * RX frames pending in the RX queue before the notification
25800690405fSSara Sharon 		 * that might get processed between now and the actual deletion
25810690405fSSara Sharon 		 * and we would re-arm the timer although we are deleting the
25820690405fSSara Sharon 		 * reorder buffer.
25830690405fSSara Sharon 		 */
25840690405fSSara Sharon 		reorder_buf->removed = true;
25850690405fSSara Sharon 		spin_unlock_bh(&reorder_buf->lock);
25860690405fSSara Sharon 		del_timer_sync(&reorder_buf->reorder_timer);
2587b915c101SSara Sharon 	}
2588b915c101SSara Sharon }
2589b915c101SSara Sharon 
2590b915c101SSara Sharon static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2591b915c101SSara Sharon 					struct iwl_mvm_baid_data *data,
2592514c3069SLuca Coelho 					u16 ssn, u16 buf_size)
2593b915c101SSara Sharon {
2594b915c101SSara Sharon 	int i;
2595b915c101SSara Sharon 
2596b915c101SSara Sharon 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2597b915c101SSara Sharon 		struct iwl_mvm_reorder_buffer *reorder_buf =
2598b915c101SSara Sharon 			&data->reorder_buf[i];
2599dfdddd92SJohannes Berg 		struct iwl_mvm_reorder_buf_entry *entries =
2600dfdddd92SJohannes Berg 			&data->entries[i * data->entries_per_queue];
2601b915c101SSara Sharon 		int j;
2602b915c101SSara Sharon 
2603b915c101SSara Sharon 		reorder_buf->num_stored = 0;
2604b915c101SSara Sharon 		reorder_buf->head_sn = ssn;
2605b915c101SSara Sharon 		reorder_buf->buf_size = buf_size;
26060690405fSSara Sharon 		/* rx reorder timer */
26078cef5344SKees Cook 		timer_setup(&reorder_buf->reorder_timer,
26088cef5344SKees Cook 			    iwl_mvm_reorder_timer_expired, 0);
26090690405fSSara Sharon 		spin_lock_init(&reorder_buf->lock);
26100690405fSSara Sharon 		reorder_buf->mvm = mvm;
2611b915c101SSara Sharon 		reorder_buf->queue = i;
26125d43eab6SSara Sharon 		reorder_buf->valid = false;
2613b915c101SSara Sharon 		for (j = 0; j < reorder_buf->buf_size; j++)
2614dfdddd92SJohannes Berg 			__skb_queue_head_init(&entries[j].e.frames);
2615b915c101SSara Sharon 	}
261610b2b201SSara Sharon }
261710b2b201SSara Sharon 
261897f70c56SJohannes Berg static int iwl_mvm_fw_baid_op_sta(struct iwl_mvm *mvm,
261997f70c56SJohannes Berg 				  struct iwl_mvm_sta *mvm_sta,
262097f70c56SJohannes Berg 				  bool start, int tid, u16 ssn,
262197f70c56SJohannes Berg 				  u16 buf_size)
26220f3a4e48SJohannes Berg {
26230f3a4e48SJohannes Berg 	struct iwl_mvm_add_sta_cmd cmd = {
26240f3a4e48SJohannes Berg 		.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
26250f3a4e48SJohannes Berg 		.sta_id = mvm_sta->sta_id,
26260f3a4e48SJohannes Berg 		.add_modify = STA_MODE_MODIFY,
26270f3a4e48SJohannes Berg 	};
26280f3a4e48SJohannes Berg 	u32 status;
26290f3a4e48SJohannes Berg 	int ret;
26300f3a4e48SJohannes Berg 
26310f3a4e48SJohannes Berg 	if (start) {
26320f3a4e48SJohannes Berg 		cmd.add_immediate_ba_tid = tid;
26330f3a4e48SJohannes Berg 		cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
26340f3a4e48SJohannes Berg 		cmd.rx_ba_window = cpu_to_le16(buf_size);
26350f3a4e48SJohannes Berg 		cmd.modify_mask = STA_MODIFY_ADD_BA_TID;
26360f3a4e48SJohannes Berg 	} else {
26370f3a4e48SJohannes Berg 		cmd.remove_immediate_ba_tid = tid;
26380f3a4e48SJohannes Berg 		cmd.modify_mask = STA_MODIFY_REMOVE_BA_TID;
26390f3a4e48SJohannes Berg 	}
26400f3a4e48SJohannes Berg 
26410f3a4e48SJohannes Berg 	status = ADD_STA_SUCCESS;
26420f3a4e48SJohannes Berg 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
26430f3a4e48SJohannes Berg 					  iwl_mvm_add_sta_cmd_size(mvm),
26440f3a4e48SJohannes Berg 					  &cmd, &status);
26450f3a4e48SJohannes Berg 	if (ret)
26460f3a4e48SJohannes Berg 		return ret;
26470f3a4e48SJohannes Berg 
26480f3a4e48SJohannes Berg 	switch (status & IWL_ADD_STA_STATUS_MASK) {
26490f3a4e48SJohannes Berg 	case ADD_STA_SUCCESS:
26500f3a4e48SJohannes Berg 		IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
26510f3a4e48SJohannes Berg 			     start ? "start" : "stopp");
26520f3a4e48SJohannes Berg 		if (WARN_ON(start && iwl_mvm_has_new_rx_api(mvm) &&
26530f3a4e48SJohannes Berg 			    !(status & IWL_ADD_STA_BAID_VALID_MASK)))
26540f3a4e48SJohannes Berg 			return -EINVAL;
26550f3a4e48SJohannes Berg 		return u32_get_bits(status, IWL_ADD_STA_BAID_MASK);
26560f3a4e48SJohannes Berg 	case ADD_STA_IMMEDIATE_BA_FAILURE:
26570f3a4e48SJohannes Berg 		IWL_WARN(mvm, "RX BA Session refused by fw\n");
26580f3a4e48SJohannes Berg 		return -ENOSPC;
26590f3a4e48SJohannes Berg 	default:
26600f3a4e48SJohannes Berg 		IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
26610f3a4e48SJohannes Berg 			start ? "start" : "stopp", status);
26620f3a4e48SJohannes Berg 		return -EIO;
26630f3a4e48SJohannes Berg 	}
26640f3a4e48SJohannes Berg }
26650f3a4e48SJohannes Berg 
266697f70c56SJohannes Berg static int iwl_mvm_fw_baid_op_cmd(struct iwl_mvm *mvm,
266797f70c56SJohannes Berg 				  struct iwl_mvm_sta *mvm_sta,
266897f70c56SJohannes Berg 				  bool start, int tid, u16 ssn,
266997f70c56SJohannes Berg 				  u16 buf_size, int baid)
267097f70c56SJohannes Berg {
267197f70c56SJohannes Berg 	struct iwl_rx_baid_cfg_cmd cmd = {
267297f70c56SJohannes Berg 		.action = start ? cpu_to_le32(IWL_RX_BAID_ACTION_ADD) :
267397f70c56SJohannes Berg 				  cpu_to_le32(IWL_RX_BAID_ACTION_REMOVE),
267497f70c56SJohannes Berg 	};
267597f70c56SJohannes Berg 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, RX_BAID_ALLOCATION_CONFIG_CMD);
267697f70c56SJohannes Berg 	int ret;
267797f70c56SJohannes Berg 
267897f70c56SJohannes Berg 	BUILD_BUG_ON(sizeof(struct iwl_rx_baid_cfg_resp) != sizeof(baid));
267997f70c56SJohannes Berg 
268097f70c56SJohannes Berg 	if (start) {
268197f70c56SJohannes Berg 		cmd.alloc.sta_id_mask = cpu_to_le32(BIT(mvm_sta->sta_id));
268297f70c56SJohannes Berg 		cmd.alloc.tid = tid;
268397f70c56SJohannes Berg 		cmd.alloc.ssn = cpu_to_le16(ssn);
268497f70c56SJohannes Berg 		cmd.alloc.win_size = cpu_to_le16(buf_size);
268597f70c56SJohannes Berg 		baid = -EIO;
268626de4c8bSJohannes Berg 	} else if (iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 1) == 1) {
268726de4c8bSJohannes Berg 		cmd.remove_v1.baid = cpu_to_le32(baid);
268826de4c8bSJohannes Berg 		BUILD_BUG_ON(sizeof(cmd.remove_v1) > sizeof(cmd.remove));
268997f70c56SJohannes Berg 	} else {
269026de4c8bSJohannes Berg 		cmd.remove.sta_id_mask = cpu_to_le32(BIT(mvm_sta->sta_id));
269126de4c8bSJohannes Berg 		cmd.remove.tid = cpu_to_le32(tid);
269297f70c56SJohannes Berg 	}
269397f70c56SJohannes Berg 
269497f70c56SJohannes Berg 	ret = iwl_mvm_send_cmd_pdu_status(mvm, cmd_id, sizeof(cmd),
269597f70c56SJohannes Berg 					  &cmd, &baid);
269697f70c56SJohannes Berg 	if (ret)
269797f70c56SJohannes Berg 		return ret;
269897f70c56SJohannes Berg 
269997f70c56SJohannes Berg 	if (!start) {
270097f70c56SJohannes Berg 		/* ignore firmware baid on remove */
270197f70c56SJohannes Berg 		baid = 0;
270297f70c56SJohannes Berg 	}
270397f70c56SJohannes Berg 
270497f70c56SJohannes Berg 	IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
270597f70c56SJohannes Berg 		     start ? "start" : "stopp");
270697f70c56SJohannes Berg 
270797f70c56SJohannes Berg 	if (baid < 0 || baid >= ARRAY_SIZE(mvm->baid_map))
270897f70c56SJohannes Berg 		return -EINVAL;
270997f70c56SJohannes Berg 
271097f70c56SJohannes Berg 	return baid;
271197f70c56SJohannes Berg }
271297f70c56SJohannes Berg 
271397f70c56SJohannes Berg static int iwl_mvm_fw_baid_op(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvm_sta,
271497f70c56SJohannes Berg 			      bool start, int tid, u16 ssn, u16 buf_size,
271597f70c56SJohannes Berg 			      int baid)
271697f70c56SJohannes Berg {
271797f70c56SJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
271897f70c56SJohannes Berg 			IWL_UCODE_TLV_CAPA_BAID_ML_SUPPORT))
271997f70c56SJohannes Berg 		return iwl_mvm_fw_baid_op_cmd(mvm, mvm_sta, start,
272097f70c56SJohannes Berg 					      tid, ssn, buf_size, baid);
272197f70c56SJohannes Berg 
272297f70c56SJohannes Berg 	return iwl_mvm_fw_baid_op_sta(mvm, mvm_sta, start,
272397f70c56SJohannes Berg 				      tid, ssn, buf_size);
272497f70c56SJohannes Berg }
272597f70c56SJohannes Berg 
2726e705c121SKalle Valo int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2727514c3069SLuca Coelho 		       int tid, u16 ssn, bool start, u16 buf_size, u16 timeout)
2728e705c121SKalle Valo {
2729e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
273010b2b201SSara Sharon 	struct iwl_mvm_baid_data *baid_data = NULL;
27310f3a4e48SJohannes Berg 	int ret, baid;
2732ded5ded3SEmmanuel Grumbach 	u32 max_ba_id_sessions = iwl_mvm_has_new_tx_api(mvm) ? IWL_MAX_BAID :
2733ded5ded3SEmmanuel Grumbach 							       IWL_MAX_BAID_OLD;
2734e705c121SKalle Valo 
2735e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2736e705c121SKalle Valo 
2737ded5ded3SEmmanuel Grumbach 	if (start && mvm->rx_ba_sessions >= max_ba_id_sessions) {
2738e705c121SKalle Valo 		IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2739e705c121SKalle Valo 		return -ENOSPC;
2740e705c121SKalle Valo 	}
2741e705c121SKalle Valo 
274210b2b201SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) && start) {
2743dfdddd92SJohannes Berg 		u16 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2744dfdddd92SJohannes Berg 
2745dfdddd92SJohannes Berg 		/* sparse doesn't like the __align() so don't check */
2746dfdddd92SJohannes Berg #ifndef __CHECKER__
2747dfdddd92SJohannes Berg 		/*
2748dfdddd92SJohannes Berg 		 * The division below will be OK if either the cache line size
2749dfdddd92SJohannes Berg 		 * can be divided by the entry size (ALIGN will round up) or if
2750dfdddd92SJohannes Berg 		 * if the entry size can be divided by the cache line size, in
2751dfdddd92SJohannes Berg 		 * which case the ALIGN() will do nothing.
2752dfdddd92SJohannes Berg 		 */
2753dfdddd92SJohannes Berg 		BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2754dfdddd92SJohannes Berg 			     sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2755dfdddd92SJohannes Berg #endif
2756dfdddd92SJohannes Berg 
2757dfdddd92SJohannes Berg 		/*
2758dfdddd92SJohannes Berg 		 * Upward align the reorder buffer size to fill an entire cache
2759dfdddd92SJohannes Berg 		 * line for each queue, to avoid sharing cache lines between
2760dfdddd92SJohannes Berg 		 * different queues.
2761dfdddd92SJohannes Berg 		 */
2762dfdddd92SJohannes Berg 		reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2763dfdddd92SJohannes Berg 
276410b2b201SSara Sharon 		/*
276510b2b201SSara Sharon 		 * Allocate here so if allocation fails we can bail out early
276610b2b201SSara Sharon 		 * before starting the BA session in the firmware
276710b2b201SSara Sharon 		 */
2768b915c101SSara Sharon 		baid_data = kzalloc(sizeof(*baid_data) +
2769b915c101SSara Sharon 				    mvm->trans->num_rx_queues *
2770dfdddd92SJohannes Berg 				    reorder_buf_size,
2771b915c101SSara Sharon 				    GFP_KERNEL);
277210b2b201SSara Sharon 		if (!baid_data)
277310b2b201SSara Sharon 			return -ENOMEM;
2774dfdddd92SJohannes Berg 
2775dfdddd92SJohannes Berg 		/*
2776dfdddd92SJohannes Berg 		 * This division is why we need the above BUILD_BUG_ON(),
2777dfdddd92SJohannes Berg 		 * if that doesn't hold then this will not be right.
2778dfdddd92SJohannes Berg 		 */
2779dfdddd92SJohannes Berg 		baid_data->entries_per_queue =
2780dfdddd92SJohannes Berg 			reorder_buf_size / sizeof(baid_data->entries[0]);
278110b2b201SSara Sharon 	}
278210b2b201SSara Sharon 
278397f70c56SJohannes Berg 	if (iwl_mvm_has_new_rx_api(mvm) && !start) {
278497f70c56SJohannes Berg 		baid = mvm_sta->tid_to_baid[tid];
278597f70c56SJohannes Berg 	} else {
278697f70c56SJohannes Berg 		/* we don't really need it in this case */
278797f70c56SJohannes Berg 		baid = -1;
278897f70c56SJohannes Berg 	}
278997f70c56SJohannes Berg 
27903538c809SLuca Coelho 	/* Don't send command to remove (start=0) BAID during restart */
27913538c809SLuca Coelho 	if (start || !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
279297f70c56SJohannes Berg 		baid = iwl_mvm_fw_baid_op(mvm, mvm_sta, start, tid, ssn, buf_size,
279397f70c56SJohannes Berg 					  baid);
27943538c809SLuca Coelho 
27950f3a4e48SJohannes Berg 	if (baid < 0) {
27960f3a4e48SJohannes Berg 		ret = baid;
279710b2b201SSara Sharon 		goto out_free;
2798e705c121SKalle Valo 	}
2799e705c121SKalle Valo 
280010b2b201SSara Sharon 	if (start) {
2801e705c121SKalle Valo 		mvm->rx_ba_sessions++;
280210b2b201SSara Sharon 
280310b2b201SSara Sharon 		if (!iwl_mvm_has_new_rx_api(mvm))
280410b2b201SSara Sharon 			return 0;
280510b2b201SSara Sharon 
280610b2b201SSara Sharon 		baid_data->baid = baid;
280710b2b201SSara Sharon 		baid_data->timeout = timeout;
280810b2b201SSara Sharon 		baid_data->last_rx = jiffies;
28098cef5344SKees Cook 		baid_data->rcu_ptr = &mvm->baid_map[baid];
28108cef5344SKees Cook 		timer_setup(&baid_data->session_timer,
28118cef5344SKees Cook 			    iwl_mvm_rx_agg_session_expired, 0);
281210b2b201SSara Sharon 		baid_data->mvm = mvm;
281310b2b201SSara Sharon 		baid_data->tid = tid;
281410b2b201SSara Sharon 		baid_data->sta_id = mvm_sta->sta_id;
281510b2b201SSara Sharon 
281610b2b201SSara Sharon 		mvm_sta->tid_to_baid[tid] = baid;
281710b2b201SSara Sharon 		if (timeout)
281810b2b201SSara Sharon 			mod_timer(&baid_data->session_timer,
281910b2b201SSara Sharon 				  TU_TO_EXP_TIME(timeout * 2));
282010b2b201SSara Sharon 
28213f1c4c58SSara Sharon 		iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size);
282210b2b201SSara Sharon 		/*
282310b2b201SSara Sharon 		 * protect the BA data with RCU to cover a case where our
282410b2b201SSara Sharon 		 * internal RX sync mechanism will timeout (not that it's
282510b2b201SSara Sharon 		 * supposed to happen) and we will free the session data while
282610b2b201SSara Sharon 		 * RX is being processed in parallel
282710b2b201SSara Sharon 		 */
282835263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
282935263a03SSara Sharon 			     mvm_sta->sta_id, tid, baid);
283010b2b201SSara Sharon 		WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
283110b2b201SSara Sharon 		rcu_assign_pointer(mvm->baid_map[baid], baid_data);
283260dec523SSara Sharon 	} else  {
28330f3a4e48SJohannes Berg 		baid = mvm_sta->tid_to_baid[tid];
283410b2b201SSara Sharon 
283560dec523SSara Sharon 		if (mvm->rx_ba_sessions > 0)
2836e705c121SKalle Valo 			/* check that restart flow didn't zero the counter */
2837e705c121SKalle Valo 			mvm->rx_ba_sessions--;
283810b2b201SSara Sharon 		if (!iwl_mvm_has_new_rx_api(mvm))
283910b2b201SSara Sharon 			return 0;
2840e705c121SKalle Valo 
284110b2b201SSara Sharon 		if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
284210b2b201SSara Sharon 			return -EINVAL;
284310b2b201SSara Sharon 
284410b2b201SSara Sharon 		baid_data = rcu_access_pointer(mvm->baid_map[baid]);
284510b2b201SSara Sharon 		if (WARN_ON(!baid_data))
284610b2b201SSara Sharon 			return -EINVAL;
284710b2b201SSara Sharon 
284810b2b201SSara Sharon 		/* synchronize all rx queues so we can safely delete */
2849b915c101SSara Sharon 		iwl_mvm_free_reorder(mvm, baid_data);
2850292a089dSSteven Rostedt (Google) 		timer_shutdown_sync(&baid_data->session_timer);
285110b2b201SSara Sharon 		RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
285210b2b201SSara Sharon 		kfree_rcu(baid_data, rcu_head);
285335263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
28542438d430SJohannes Berg 
28552438d430SJohannes Berg 		/*
28562438d430SJohannes Berg 		 * After we've deleted it, do another queue sync
28572438d430SJohannes Berg 		 * so if an IWL_MVM_RXQ_NSSN_SYNC was concurrently
28582438d430SJohannes Berg 		 * running it won't find a new session in the old
28592438d430SJohannes Berg 		 * BAID. It can find the NULL pointer for the BAID,
28602438d430SJohannes Berg 		 * but we must not have it find a different session.
28612438d430SJohannes Berg 		 */
28622438d430SJohannes Berg 		iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY,
28632438d430SJohannes Berg 						true, NULL, 0);
286410b2b201SSara Sharon 	}
286510b2b201SSara Sharon 	return 0;
286610b2b201SSara Sharon 
286710b2b201SSara Sharon out_free:
286810b2b201SSara Sharon 	kfree(baid_data);
2869e705c121SKalle Valo 	return ret;
2870e705c121SKalle Valo }
2871e705c121SKalle Valo 
28729794c64fSLiad Kaufman int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2873e705c121SKalle Valo 		       int tid, u8 queue, bool start)
2874e705c121SKalle Valo {
2875e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2876e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
2877e705c121SKalle Valo 	int ret;
2878e705c121SKalle Valo 	u32 status;
2879e705c121SKalle Valo 
2880e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2881e705c121SKalle Valo 
2882e705c121SKalle Valo 	if (start) {
2883e705c121SKalle Valo 		mvm_sta->tfd_queue_msk |= BIT(queue);
2884e705c121SKalle Valo 		mvm_sta->tid_disable_agg &= ~BIT(tid);
2885e705c121SKalle Valo 	} else {
2886cf961e16SLiad Kaufman 		/* In DQA-mode the queue isn't removed on agg termination */
2887e705c121SKalle Valo 		mvm_sta->tid_disable_agg |= BIT(tid);
2888e705c121SKalle Valo 	}
2889e705c121SKalle Valo 
2890e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2891e705c121SKalle Valo 	cmd.sta_id = mvm_sta->sta_id;
2892e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
2893bb49701bSSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm))
2894bb49701bSSara Sharon 		cmd.modify_mask = STA_MODIFY_QUEUES;
2895bb49701bSSara Sharon 	cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
2896e705c121SKalle Valo 	cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2897e705c121SKalle Valo 	cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2898e705c121SKalle Valo 
2899e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
2900854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2901854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
2902e705c121SKalle Valo 					  &cmd, &status);
2903e705c121SKalle Valo 	if (ret)
2904e705c121SKalle Valo 		return ret;
2905e705c121SKalle Valo 
2906837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2907e705c121SKalle Valo 	case ADD_STA_SUCCESS:
2908e705c121SKalle Valo 		break;
2909e705c121SKalle Valo 	default:
2910e705c121SKalle Valo 		ret = -EIO;
2911e705c121SKalle Valo 		IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2912e705c121SKalle Valo 			start ? "start" : "stopp", status);
2913e705c121SKalle Valo 		break;
2914e705c121SKalle Valo 	}
2915e705c121SKalle Valo 
2916e705c121SKalle Valo 	return ret;
2917e705c121SKalle Valo }
2918e705c121SKalle Valo 
2919e705c121SKalle Valo const u8 tid_to_mac80211_ac[] = {
2920e705c121SKalle Valo 	IEEE80211_AC_BE,
2921e705c121SKalle Valo 	IEEE80211_AC_BK,
2922e705c121SKalle Valo 	IEEE80211_AC_BK,
2923e705c121SKalle Valo 	IEEE80211_AC_BE,
2924e705c121SKalle Valo 	IEEE80211_AC_VI,
2925e705c121SKalle Valo 	IEEE80211_AC_VI,
2926e705c121SKalle Valo 	IEEE80211_AC_VO,
2927e705c121SKalle Valo 	IEEE80211_AC_VO,
29289794c64fSLiad Kaufman 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2929e705c121SKalle Valo };
2930e705c121SKalle Valo 
2931e705c121SKalle Valo static const u8 tid_to_ucode_ac[] = {
2932e705c121SKalle Valo 	AC_BE,
2933e705c121SKalle Valo 	AC_BK,
2934e705c121SKalle Valo 	AC_BK,
2935e705c121SKalle Valo 	AC_BE,
2936e705c121SKalle Valo 	AC_VI,
2937e705c121SKalle Valo 	AC_VI,
2938e705c121SKalle Valo 	AC_VO,
2939e705c121SKalle Valo 	AC_VO,
2940e705c121SKalle Valo };
2941e705c121SKalle Valo 
2942e705c121SKalle Valo int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2943e705c121SKalle Valo 			     struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2944e705c121SKalle Valo {
2945e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2946e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data;
2947dd32162dSLiad Kaufman 	u16 normalized_ssn;
2948b0d795a9SMordechay Goodstein 	u16 txq_id;
2949e705c121SKalle Valo 	int ret;
2950e705c121SKalle Valo 
2951e705c121SKalle Valo 	if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2952e705c121SKalle Valo 		return -EINVAL;
2953e705c121SKalle Valo 
2954bd800e41SNaftali Goldstein 	if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
2955bd800e41SNaftali Goldstein 	    mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2956bd800e41SNaftali Goldstein 		IWL_ERR(mvm,
2957bd800e41SNaftali Goldstein 			"Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
2958e705c121SKalle Valo 			mvmsta->tid_data[tid].state);
2959e705c121SKalle Valo 		return -ENXIO;
2960e705c121SKalle Valo 	}
2961e705c121SKalle Valo 
2962e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2963e705c121SKalle Valo 
2964bd8f3fc6SLiad Kaufman 	if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE &&
2965bd8f3fc6SLiad Kaufman 	    iwl_mvm_has_new_tx_api(mvm)) {
2966bd8f3fc6SLiad Kaufman 		u8 ac = tid_to_mac80211_ac[tid];
2967bd8f3fc6SLiad Kaufman 
2968bd8f3fc6SLiad Kaufman 		ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
2969bd8f3fc6SLiad Kaufman 		if (ret)
2970bd8f3fc6SLiad Kaufman 			return ret;
2971bd8f3fc6SLiad Kaufman 	}
2972bd8f3fc6SLiad Kaufman 
2973e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2974e705c121SKalle Valo 
2975cf961e16SLiad Kaufman 	/*
2976cf961e16SLiad Kaufman 	 * Note the possible cases:
29774a6d2e52SAvraham Stern 	 *  1. An enabled TXQ - TXQ needs to become agg'ed
29784a6d2e52SAvraham Stern 	 *  2. The TXQ hasn't yet been enabled, so find a free one and mark
29794a6d2e52SAvraham Stern 	 *	it as reserved
2980cf961e16SLiad Kaufman 	 */
2981cf961e16SLiad Kaufman 	txq_id = mvmsta->tid_data[tid].txq_id;
298234e10860SSara Sharon 	if (txq_id == IWL_MVM_INVALID_QUEUE) {
2983b0d795a9SMordechay Goodstein 		ret = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2984c8f54701SJohannes Berg 					      IWL_MVM_DQA_MIN_DATA_QUEUE,
2985c8f54701SJohannes Berg 					      IWL_MVM_DQA_MAX_DATA_QUEUE);
2986b0d795a9SMordechay Goodstein 		if (ret < 0) {
2987e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to allocate agg queue\n");
2988f3f240f9SJohannes Berg 			goto out;
2989e705c121SKalle Valo 		}
2990cf961e16SLiad Kaufman 
2991b0d795a9SMordechay Goodstein 		txq_id = ret;
2992b0d795a9SMordechay Goodstein 
2993cf961e16SLiad Kaufman 		/* TXQ hasn't yet been enabled, so mark it only as reserved */
2994cf961e16SLiad Kaufman 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2995b0d795a9SMordechay Goodstein 	} else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) {
2996b0d795a9SMordechay Goodstein 		ret = -ENXIO;
2997b0d795a9SMordechay Goodstein 		IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n",
2998b0d795a9SMordechay Goodstein 			tid, IWL_MAX_HW_QUEUES - 1);
2999b0d795a9SMordechay Goodstein 		goto out;
3000b0d795a9SMordechay Goodstein 
30014a6d2e52SAvraham Stern 	} else if (unlikely(mvm->queue_info[txq_id].status ==
30024a6d2e52SAvraham Stern 			    IWL_MVM_QUEUE_SHARED)) {
30034a6d2e52SAvraham Stern 		ret = -ENXIO;
30044a6d2e52SAvraham Stern 		IWL_DEBUG_TX_QUEUES(mvm,
30054a6d2e52SAvraham Stern 				    "Can't start tid %d agg on shared queue!\n",
30064a6d2e52SAvraham Stern 				    tid);
3007f3f240f9SJohannes Berg 		goto out;
3008cf961e16SLiad Kaufman 	}
30099f9af3d7SLiad Kaufman 
3010cf961e16SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm,
3011cf961e16SLiad Kaufman 			    "AGG for tid %d will be on queue #%d\n",
3012cf961e16SLiad Kaufman 			    tid, txq_id);
3013cf961e16SLiad Kaufman 
3014e705c121SKalle Valo 	tid_data = &mvmsta->tid_data[tid];
3015e705c121SKalle Valo 	tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3016e705c121SKalle Valo 	tid_data->txq_id = txq_id;
3017e705c121SKalle Valo 	*ssn = tid_data->ssn;
3018e705c121SKalle Valo 
3019e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm,
3020e705c121SKalle Valo 			    "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
3021e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->ssn,
3022e705c121SKalle Valo 			    tid_data->next_reclaimed);
3023e705c121SKalle Valo 
3024dd32162dSLiad Kaufman 	/*
30252f7a3863SLuca Coelho 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
3026dd32162dSLiad Kaufman 	 * to align the wrap around of ssn so we compare relevant values.
3027dd32162dSLiad Kaufman 	 */
3028dd32162dSLiad Kaufman 	normalized_ssn = tid_data->ssn;
3029286ca8ebSLuca Coelho 	if (mvm->trans->trans_cfg->gen2)
3030dd32162dSLiad Kaufman 		normalized_ssn &= 0xff;
3031dd32162dSLiad Kaufman 
3032dd32162dSLiad Kaufman 	if (normalized_ssn == tid_data->next_reclaimed) {
3033e705c121SKalle Valo 		tid_data->state = IWL_AGG_STARTING;
30342ce113deSJohannes Berg 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
3035e705c121SKalle Valo 	} else {
3036e705c121SKalle Valo 		tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
30377e0ca723SMordechay Goodstein 		ret = IEEE80211_AMPDU_TX_START_DELAY_ADDBA;
30382ce113deSJohannes Berg 	}
3039e705c121SKalle Valo 
30409f9af3d7SLiad Kaufman out:
3041e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3042e705c121SKalle Valo 
3043e705c121SKalle Valo 	return ret;
3044e705c121SKalle Valo }
3045e705c121SKalle Valo 
3046e705c121SKalle Valo int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3047514c3069SLuca Coelho 			    struct ieee80211_sta *sta, u16 tid, u16 buf_size,
3048bb81bb68SEmmanuel Grumbach 			    bool amsdu)
3049e705c121SKalle Valo {
3050e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3051e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3052e705c121SKalle Valo 	unsigned int wdg_timeout =
3053e705c121SKalle Valo 		iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
3054eea76c36SEmmanuel Grumbach 	int queue, ret;
3055cf961e16SLiad Kaufman 	bool alloc_queue = true;
30569f9af3d7SLiad Kaufman 	enum iwl_mvm_queue_status queue_status;
3057e705c121SKalle Valo 	u16 ssn;
3058e705c121SKalle Valo 
3059eea76c36SEmmanuel Grumbach 	struct iwl_trans_txq_scd_cfg cfg = {
3060eea76c36SEmmanuel Grumbach 		.sta_id = mvmsta->sta_id,
3061eea76c36SEmmanuel Grumbach 		.tid = tid,
3062eea76c36SEmmanuel Grumbach 		.frame_limit = buf_size,
3063eea76c36SEmmanuel Grumbach 		.aggregate = true,
3064eea76c36SEmmanuel Grumbach 	};
3065eea76c36SEmmanuel Grumbach 
3066ecaf71deSGregory Greenman 	/*
3067ecaf71deSGregory Greenman 	 * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
3068ecaf71deSGregory Greenman 	 * manager, so this function should never be called in this case.
3069ecaf71deSGregory Greenman 	 */
30704243edb4SEmmanuel Grumbach 	if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
3071ecaf71deSGregory Greenman 		return -EINVAL;
3072ecaf71deSGregory Greenman 
3073e705c121SKalle Valo 	BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
3074e705c121SKalle Valo 		     != IWL_MAX_TID_COUNT);
3075e705c121SKalle Valo 
3076e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3077e705c121SKalle Valo 	ssn = tid_data->ssn;
3078e705c121SKalle Valo 	queue = tid_data->txq_id;
3079e705c121SKalle Valo 	tid_data->state = IWL_AGG_ON;
3080e705c121SKalle Valo 	mvmsta->agg_tids |= BIT(tid);
3081e705c121SKalle Valo 	tid_data->ssn = 0xffff;
3082bb81bb68SEmmanuel Grumbach 	tid_data->amsdu_in_ampdu_allowed = amsdu;
3083e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3084e705c121SKalle Valo 
308534e10860SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
308634e10860SSara Sharon 		/*
30870ec9257bSSara Sharon 		 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
30880ec9257bSSara Sharon 		 * would have failed, so if we are here there is no need to
30890ec9257bSSara Sharon 		 * allocate a queue.
30900ec9257bSSara Sharon 		 * However, if aggregation size is different than the default
30910ec9257bSSara Sharon 		 * size, the scheduler should be reconfigured.
30920ec9257bSSara Sharon 		 * We cannot do this with the new TX API, so return unsupported
30930ec9257bSSara Sharon 		 * for now, until it will be offloaded to firmware..
30940ec9257bSSara Sharon 		 * Note that if SCD default value changes - this condition
30950ec9257bSSara Sharon 		 * should be updated as well.
309634e10860SSara Sharon 		 */
30970ec9257bSSara Sharon 		if (buf_size < IWL_FRAME_LIMIT)
309834e10860SSara Sharon 			return -ENOTSUPP;
309934e10860SSara Sharon 
310034e10860SSara Sharon 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
310134e10860SSara Sharon 		if (ret)
310234e10860SSara Sharon 			return -EIO;
310334e10860SSara Sharon 		goto out;
310434e10860SSara Sharon 	}
310534e10860SSara Sharon 
3106eea76c36SEmmanuel Grumbach 	cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
3107e705c121SKalle Valo 
31089f9af3d7SLiad Kaufman 	queue_status = mvm->queue_info[queue].status;
31099f9af3d7SLiad Kaufman 
3110cf961e16SLiad Kaufman 	/* Maybe there is no need to even alloc a queue... */
3111cf961e16SLiad Kaufman 	if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
3112cf961e16SLiad Kaufman 		alloc_queue = false;
3113cf961e16SLiad Kaufman 
3114cf961e16SLiad Kaufman 	/*
3115cf961e16SLiad Kaufman 	 * Only reconfig the SCD for the queue if the window size has
3116cf961e16SLiad Kaufman 	 * changed from current (become smaller)
3117cf961e16SLiad Kaufman 	 */
31180ec9257bSSara Sharon 	if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
3119cf961e16SLiad Kaufman 		/*
3120cf961e16SLiad Kaufman 		 * If reconfiguring an existing queue, it first must be
3121cf961e16SLiad Kaufman 		 * drained
3122cf961e16SLiad Kaufman 		 */
3123a1a57877SSara Sharon 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
3124cf961e16SLiad Kaufman 						     BIT(queue));
3125cf961e16SLiad Kaufman 		if (ret) {
3126cf961e16SLiad Kaufman 			IWL_ERR(mvm,
3127cf961e16SLiad Kaufman 				"Error draining queue before reconfig\n");
3128cf961e16SLiad Kaufman 			return ret;
3129cf961e16SLiad Kaufman 		}
3130cf961e16SLiad Kaufman 
3131cf961e16SLiad Kaufman 		ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
3132cf961e16SLiad Kaufman 					   mvmsta->sta_id, tid,
3133cf961e16SLiad Kaufman 					   buf_size, ssn);
3134cf961e16SLiad Kaufman 		if (ret) {
3135cf961e16SLiad Kaufman 			IWL_ERR(mvm,
3136cf961e16SLiad Kaufman 				"Error reconfiguring TXQ #%d\n", queue);
3137cf961e16SLiad Kaufman 			return ret;
3138cf961e16SLiad Kaufman 		}
3139cf961e16SLiad Kaufman 	}
3140cf961e16SLiad Kaufman 
3141cf961e16SLiad Kaufman 	if (alloc_queue)
3142cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, sta, queue, ssn,
3143cf961e16SLiad Kaufman 				   &cfg, wdg_timeout);
3144e705c121SKalle Valo 
31459f9af3d7SLiad Kaufman 	/* Send ADD_STA command to enable aggs only if the queue isn't shared */
31469f9af3d7SLiad Kaufman 	if (queue_status != IWL_MVM_QUEUE_SHARED) {
3147e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
3148e705c121SKalle Valo 		if (ret)
3149e705c121SKalle Valo 			return -EIO;
31509f9af3d7SLiad Kaufman 	}
3151e705c121SKalle Valo 
3152e705c121SKalle Valo 	/* No need to mark as reserved */
3153cf961e16SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
3154e705c121SKalle Valo 
315534e10860SSara Sharon out:
3156e705c121SKalle Valo 	/*
3157e705c121SKalle Valo 	 * Even though in theory the peer could have different
3158e705c121SKalle Valo 	 * aggregation reorder buffer sizes for different sessions,
3159e705c121SKalle Valo 	 * our ucode doesn't allow for that and has a global limit
3160e705c121SKalle Valo 	 * for each station. Therefore, use the minimum of all the
3161e705c121SKalle Valo 	 * aggregation sessions and our default value.
3162e705c121SKalle Valo 	 */
3163e705c121SKalle Valo 	mvmsta->max_agg_bufsize =
3164e705c121SKalle Valo 		min(mvmsta->max_agg_bufsize, buf_size);
3165ecaf71deSGregory Greenman 	mvmsta->lq_sta.rs_drv.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
3166e705c121SKalle Valo 
3167e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
3168e705c121SKalle Valo 		     sta->addr, tid);
3169e705c121SKalle Valo 
3170cd4d6b0bSGregory Greenman 	return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.rs_drv.lq);
3171e705c121SKalle Valo }
3172e705c121SKalle Valo 
317334e10860SSara Sharon static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
317434e10860SSara Sharon 					struct iwl_mvm_sta *mvmsta,
31754b387906SAvraham Stern 					struct iwl_mvm_tid_data *tid_data)
317634e10860SSara Sharon {
31774b387906SAvraham Stern 	u16 txq_id = tid_data->txq_id;
31784b387906SAvraham Stern 
3179f3f240f9SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
3180f3f240f9SJohannes Berg 
318134e10860SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm))
318234e10860SSara Sharon 		return;
318334e10860SSara Sharon 
318434e10860SSara Sharon 	/*
318534e10860SSara Sharon 	 * The TXQ is marked as reserved only if no traffic came through yet
318634e10860SSara Sharon 	 * This means no traffic has been sent on this TID (agg'd or not), so
318734e10860SSara Sharon 	 * we no longer have use for the queue. Since it hasn't even been
318834e10860SSara Sharon 	 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
318934e10860SSara Sharon 	 * free.
319034e10860SSara Sharon 	 */
31914b387906SAvraham Stern 	if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
319234e10860SSara Sharon 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
31934b387906SAvraham Stern 		tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
31944b387906SAvraham Stern 	}
319534e10860SSara Sharon }
319634e10860SSara Sharon 
3197e705c121SKalle Valo int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3198e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid)
3199e705c121SKalle Valo {
3200e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3201e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3202e705c121SKalle Valo 	u16 txq_id;
3203e705c121SKalle Valo 	int err;
3204e705c121SKalle Valo 
3205e705c121SKalle Valo 	/*
3206e705c121SKalle Valo 	 * If mac80211 is cleaning its state, then say that we finished since
3207e705c121SKalle Valo 	 * our state has been cleared anyway.
3208e705c121SKalle Valo 	 */
3209e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3210e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3211e705c121SKalle Valo 		return 0;
3212e705c121SKalle Valo 	}
3213e705c121SKalle Valo 
3214e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3215e705c121SKalle Valo 
3216e705c121SKalle Valo 	txq_id = tid_data->txq_id;
3217e705c121SKalle Valo 
3218e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
3219e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
3220e705c121SKalle Valo 
3221e705c121SKalle Valo 	mvmsta->agg_tids &= ~BIT(tid);
3222e705c121SKalle Valo 
32234b387906SAvraham Stern 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3224e705c121SKalle Valo 
3225e705c121SKalle Valo 	switch (tid_data->state) {
3226e705c121SKalle Valo 	case IWL_AGG_ON:
3227e705c121SKalle Valo 		tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3228e705c121SKalle Valo 
3229e705c121SKalle Valo 		IWL_DEBUG_TX_QUEUES(mvm,
3230e705c121SKalle Valo 				    "ssn = %d, next_recl = %d\n",
3231e705c121SKalle Valo 				    tid_data->ssn, tid_data->next_reclaimed);
3232e705c121SKalle Valo 
3233e705c121SKalle Valo 		tid_data->ssn = 0xffff;
3234e705c121SKalle Valo 		tid_data->state = IWL_AGG_OFF;
3235e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
3236e705c121SKalle Valo 
3237e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3238e705c121SKalle Valo 
3239e705c121SKalle Valo 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3240e705c121SKalle Valo 		return 0;
3241e705c121SKalle Valo 	case IWL_AGG_STARTING:
3242e705c121SKalle Valo 	case IWL_EMPTYING_HW_QUEUE_ADDBA:
3243e705c121SKalle Valo 		/*
3244e705c121SKalle Valo 		 * The agg session has been stopped before it was set up. This
3245e705c121SKalle Valo 		 * can happen when the AddBA timer times out for example.
3246e705c121SKalle Valo 		 */
3247e705c121SKalle Valo 
3248e705c121SKalle Valo 		/* No barriers since we are under mutex */
3249e705c121SKalle Valo 		lockdep_assert_held(&mvm->mutex);
3250e705c121SKalle Valo 
3251e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3252e705c121SKalle Valo 		tid_data->state = IWL_AGG_OFF;
3253e705c121SKalle Valo 		err = 0;
3254e705c121SKalle Valo 		break;
3255e705c121SKalle Valo 	default:
3256e705c121SKalle Valo 		IWL_ERR(mvm,
3257e705c121SKalle Valo 			"Stopping AGG while state not ON or starting for %d on %d (%d)\n",
3258e705c121SKalle Valo 			mvmsta->sta_id, tid, tid_data->state);
3259e705c121SKalle Valo 		IWL_ERR(mvm,
3260e705c121SKalle Valo 			"\ttid_data->txq_id = %d\n", tid_data->txq_id);
3261e705c121SKalle Valo 		err = -EINVAL;
3262e705c121SKalle Valo 	}
3263e705c121SKalle Valo 
3264e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3265e705c121SKalle Valo 
3266e705c121SKalle Valo 	return err;
3267e705c121SKalle Valo }
3268e705c121SKalle Valo 
3269e705c121SKalle Valo int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3270e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid)
3271e705c121SKalle Valo {
3272e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3273e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3274e705c121SKalle Valo 	u16 txq_id;
3275e705c121SKalle Valo 	enum iwl_mvm_agg_state old_state;
3276e705c121SKalle Valo 
3277e705c121SKalle Valo 	/*
3278e705c121SKalle Valo 	 * First set the agg state to OFF to avoid calling
3279e705c121SKalle Valo 	 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
3280e705c121SKalle Valo 	 */
3281e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3282e705c121SKalle Valo 	txq_id = tid_data->txq_id;
3283e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
3284e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
3285e705c121SKalle Valo 	old_state = tid_data->state;
3286e705c121SKalle Valo 	tid_data->state = IWL_AGG_OFF;
3287e705c121SKalle Valo 	mvmsta->agg_tids &= ~BIT(tid);
3288e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3289e705c121SKalle Valo 
32904b387906SAvraham Stern 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3291e705c121SKalle Valo 
3292e705c121SKalle Valo 	if (old_state >= IWL_AGG_ON) {
3293e705c121SKalle Valo 		iwl_mvm_drain_sta(mvm, mvmsta, true);
3294d167e81aSMordechai Goodstein 
3295d167e81aSMordechai Goodstein 		if (iwl_mvm_has_new_tx_api(mvm)) {
3296d167e81aSMordechai Goodstein 			if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
3297d4e3a341SMordechay Goodstein 						   BIT(tid)))
3298d167e81aSMordechai Goodstein 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3299d167e81aSMordechai Goodstein 			iwl_trans_wait_txq_empty(mvm->trans, txq_id);
3300d167e81aSMordechai Goodstein 		} else {
3301d4e3a341SMordechay Goodstein 			if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id)))
3302e705c121SKalle Valo 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
33030b90964aSSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
3304d167e81aSMordechai Goodstein 		}
3305d6d517b7SSara Sharon 
3306e705c121SKalle Valo 		iwl_mvm_drain_sta(mvm, mvmsta, false);
3307e705c121SKalle Valo 
3308e705c121SKalle Valo 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3309e705c121SKalle Valo 	}
3310e705c121SKalle Valo 
3311e705c121SKalle Valo 	return 0;
3312e705c121SKalle Valo }
3313e705c121SKalle Valo 
3314e705c121SKalle Valo static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
3315e705c121SKalle Valo {
3316e705c121SKalle Valo 	int i, max = -1, max_offs = -1;
3317e705c121SKalle Valo 
3318e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3319e705c121SKalle Valo 
3320e705c121SKalle Valo 	/* Pick the unused key offset with the highest 'deleted'
3321e705c121SKalle Valo 	 * counter. Every time a key is deleted, all the counters
3322e705c121SKalle Valo 	 * are incremented and the one that was just deleted is
3323e705c121SKalle Valo 	 * reset to zero. Thus, the highest counter is the one
3324e705c121SKalle Valo 	 * that was deleted longest ago. Pick that one.
3325e705c121SKalle Valo 	 */
3326e705c121SKalle Valo 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3327e705c121SKalle Valo 		if (test_bit(i, mvm->fw_key_table))
3328e705c121SKalle Valo 			continue;
3329e705c121SKalle Valo 		if (mvm->fw_key_deleted[i] > max) {
3330e705c121SKalle Valo 			max = mvm->fw_key_deleted[i];
3331e705c121SKalle Valo 			max_offs = i;
3332e705c121SKalle Valo 		}
3333e705c121SKalle Valo 	}
3334e705c121SKalle Valo 
3335e705c121SKalle Valo 	if (max_offs < 0)
3336e705c121SKalle Valo 		return STA_KEY_IDX_INVALID;
3337e705c121SKalle Valo 
3338e705c121SKalle Valo 	return max_offs;
3339e705c121SKalle Valo }
3340e705c121SKalle Valo 
33415f7a1847SJohannes Berg static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
33424615fd15SEmmanuel Grumbach 					       struct ieee80211_vif *vif,
3343e705c121SKalle Valo 					       struct ieee80211_sta *sta)
3344e705c121SKalle Valo {
3345e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3346e705c121SKalle Valo 
33475f7a1847SJohannes Berg 	if (sta)
33485f7a1847SJohannes Berg 		return iwl_mvm_sta_from_mac80211(sta);
3349e705c121SKalle Valo 
3350e705c121SKalle Valo 	/*
3351e705c121SKalle Valo 	 * The device expects GTKs for station interfaces to be
3352e705c121SKalle Valo 	 * installed as GTKs for the AP station. If we have no
3353e705c121SKalle Valo 	 * station ID, then use AP's station ID.
3354e705c121SKalle Valo 	 */
3355e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
33560ae98812SSara Sharon 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
33574615fd15SEmmanuel Grumbach 		u8 sta_id = mvmvif->ap_sta_id;
33584615fd15SEmmanuel Grumbach 
33597d6a1ab6SEmmanuel Grumbach 		sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
33607d6a1ab6SEmmanuel Grumbach 					    lockdep_is_held(&mvm->mutex));
33617d6a1ab6SEmmanuel Grumbach 
33624615fd15SEmmanuel Grumbach 		/*
33634615fd15SEmmanuel Grumbach 		 * It is possible that the 'sta' parameter is NULL,
33644615fd15SEmmanuel Grumbach 		 * for example when a GTK is removed - the sta_id will then
33654615fd15SEmmanuel Grumbach 		 * be the AP ID, and no station was passed by mac80211.
33664615fd15SEmmanuel Grumbach 		 */
33677d6a1ab6SEmmanuel Grumbach 		if (IS_ERR_OR_NULL(sta))
33687d6a1ab6SEmmanuel Grumbach 			return NULL;
33697d6a1ab6SEmmanuel Grumbach 
33707d6a1ab6SEmmanuel Grumbach 		return iwl_mvm_sta_from_mac80211(sta);
33714615fd15SEmmanuel Grumbach 	}
3372e705c121SKalle Valo 
33735f7a1847SJohannes Berg 	return NULL;
3374e705c121SKalle Valo }
3375e705c121SKalle Valo 
3376a6dfbd04SJohannes Berg static int iwl_mvm_pn_cmp(const u8 *pn1, const u8 *pn2, int len)
3377a6dfbd04SJohannes Berg {
3378a6dfbd04SJohannes Berg 	int i;
3379a6dfbd04SJohannes Berg 
3380a6dfbd04SJohannes Berg 	for (i = len - 1; i >= 0; i--) {
3381a6dfbd04SJohannes Berg 		if (pn1[i] > pn2[i])
3382a6dfbd04SJohannes Berg 			return 1;
3383a6dfbd04SJohannes Berg 		if (pn1[i] < pn2[i])
3384a6dfbd04SJohannes Berg 			return -1;
3385a6dfbd04SJohannes Berg 	}
3386a6dfbd04SJohannes Berg 
3387a6dfbd04SJohannes Berg 	return 0;
3388a6dfbd04SJohannes Berg }
3389a6dfbd04SJohannes Berg 
3390e705c121SKalle Valo static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
339185aeb58cSDavid Spinadel 				u32 sta_id,
339245c458b4SSara Sharon 				struct ieee80211_key_conf *key, bool mcast,
33934615fd15SEmmanuel Grumbach 				u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
33944883145aSEmmanuel Grumbach 				u8 key_offset, bool mfp)
3395e705c121SKalle Valo {
339645c458b4SSara Sharon 	union {
339745c458b4SSara Sharon 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
339845c458b4SSara Sharon 		struct iwl_mvm_add_sta_key_cmd cmd;
339945c458b4SSara Sharon 	} u = {};
3400e705c121SKalle Valo 	__le16 key_flags;
3401e705c121SKalle Valo 	int ret;
3402e705c121SKalle Valo 	u32 status;
3403e705c121SKalle Valo 	u16 keyidx;
340445c458b4SSara Sharon 	u64 pn = 0;
340545c458b4SSara Sharon 	int i, size;
340645c458b4SSara Sharon 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
340745c458b4SSara Sharon 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3408971cbe50SJohannes Berg 	int api_ver = iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA_KEY,
3409199d895fSJohannes Berg 					    new_api ? 2 : 1);
3410e705c121SKalle Valo 
341185aeb58cSDavid Spinadel 	if (sta_id == IWL_MVM_INVALID_STA)
341285aeb58cSDavid Spinadel 		return -EINVAL;
341385aeb58cSDavid Spinadel 
341445c458b4SSara Sharon 	keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
3415e705c121SKalle Valo 		 STA_KEY_FLG_KEYID_MSK;
3416e705c121SKalle Valo 	key_flags = cpu_to_le16(keyidx);
3417e705c121SKalle Valo 	key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3418e705c121SKalle Valo 
341945c458b4SSara Sharon 	switch (key->cipher) {
3420e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
3421e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
3422199d895fSJohannes Berg 		if (api_ver >= 2) {
342345c458b4SSara Sharon 			memcpy((void *)&u.cmd.tx_mic_key,
342445c458b4SSara Sharon 			       &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
342545c458b4SSara Sharon 			       IWL_MIC_KEY_SIZE);
342645c458b4SSara Sharon 
342745c458b4SSara Sharon 			memcpy((void *)&u.cmd.rx_mic_key,
342845c458b4SSara Sharon 			       &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
342945c458b4SSara Sharon 			       IWL_MIC_KEY_SIZE);
343045c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
343145c458b4SSara Sharon 
343245c458b4SSara Sharon 		} else {
343345c458b4SSara Sharon 			u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3434e705c121SKalle Valo 			for (i = 0; i < 5; i++)
343545c458b4SSara Sharon 				u.cmd_v1.tkip_rx_ttak[i] =
343645c458b4SSara Sharon 					cpu_to_le16(tkip_p1k[i]);
343745c458b4SSara Sharon 		}
343845c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3439e705c121SKalle Valo 		break;
3440e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
3441e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
344245c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3443199d895fSJohannes Berg 		if (api_ver >= 2)
344445c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
3445e705c121SKalle Valo 		break;
3446e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
3447e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
34485a2abdcaSGustavo A. R. Silva 		fallthrough;
3449e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
3450e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
345145c458b4SSara Sharon 		memcpy(u.cmd.common.key + 3, key->key, key->keylen);
3452e705c121SKalle Valo 		break;
34532a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
34542a53d166SAyala Beker 		key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
34555a2abdcaSGustavo A. R. Silva 		fallthrough;
34562a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
34572a53d166SAyala Beker 		key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
345845c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3459199d895fSJohannes Berg 		if (api_ver >= 2)
346045c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
34612a53d166SAyala Beker 		break;
3462e705c121SKalle Valo 	default:
3463e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
346445c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3465e705c121SKalle Valo 	}
3466e705c121SKalle Valo 
3467e705c121SKalle Valo 	if (mcast)
3468e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
34694883145aSEmmanuel Grumbach 	if (mfp)
34704883145aSEmmanuel Grumbach 		key_flags |= cpu_to_le16(STA_KEY_MFP);
3471e705c121SKalle Valo 
347245c458b4SSara Sharon 	u.cmd.common.key_offset = key_offset;
347345c458b4SSara Sharon 	u.cmd.common.key_flags = key_flags;
347485aeb58cSDavid Spinadel 	u.cmd.common.sta_id = sta_id;
347545c458b4SSara Sharon 
3476a6dfbd04SJohannes Berg 	if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
3477a6dfbd04SJohannes Berg 		i = 0;
3478a6dfbd04SJohannes Berg 	else
3479a6dfbd04SJohannes Berg 		i = -1;
3480a6dfbd04SJohannes Berg 
3481a6dfbd04SJohannes Berg 	for (; i < IEEE80211_NUM_TIDS; i++) {
3482a6dfbd04SJohannes Berg 		struct ieee80211_key_seq seq = {};
3483a6dfbd04SJohannes Berg 		u8 _rx_pn[IEEE80211_MAX_PN_LEN] = {}, *rx_pn = _rx_pn;
3484a6dfbd04SJohannes Berg 		int rx_pn_len = 8;
3485199d895fSJohannes Berg 		/* there's a hole at 2/3 in FW format depending on version */
3486199d895fSJohannes Berg 		int hole = api_ver >= 3 ? 0 : 2;
3487a6dfbd04SJohannes Berg 
3488a6dfbd04SJohannes Berg 		ieee80211_get_key_rx_seq(key, i, &seq);
3489a6dfbd04SJohannes Berg 
3490a6dfbd04SJohannes Berg 		if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
3491a6dfbd04SJohannes Berg 			rx_pn[0] = seq.tkip.iv16;
3492a6dfbd04SJohannes Berg 			rx_pn[1] = seq.tkip.iv16 >> 8;
3493199d895fSJohannes Berg 			rx_pn[2 + hole] = seq.tkip.iv32;
3494199d895fSJohannes Berg 			rx_pn[3 + hole] = seq.tkip.iv32 >> 8;
3495199d895fSJohannes Berg 			rx_pn[4 + hole] = seq.tkip.iv32 >> 16;
3496199d895fSJohannes Berg 			rx_pn[5 + hole] = seq.tkip.iv32 >> 24;
3497a6dfbd04SJohannes Berg 		} else if (key_flags & cpu_to_le16(STA_KEY_FLG_EXT)) {
3498a6dfbd04SJohannes Berg 			rx_pn = seq.hw.seq;
3499a6dfbd04SJohannes Berg 			rx_pn_len = seq.hw.seq_len;
3500a6dfbd04SJohannes Berg 		} else {
3501a6dfbd04SJohannes Berg 			rx_pn[0] = seq.ccmp.pn[0];
3502a6dfbd04SJohannes Berg 			rx_pn[1] = seq.ccmp.pn[1];
3503199d895fSJohannes Berg 			rx_pn[2 + hole] = seq.ccmp.pn[2];
3504199d895fSJohannes Berg 			rx_pn[3 + hole] = seq.ccmp.pn[3];
3505199d895fSJohannes Berg 			rx_pn[4 + hole] = seq.ccmp.pn[4];
3506199d895fSJohannes Berg 			rx_pn[5 + hole] = seq.ccmp.pn[5];
3507a6dfbd04SJohannes Berg 		}
3508a6dfbd04SJohannes Berg 
3509a6dfbd04SJohannes Berg 		if (iwl_mvm_pn_cmp(rx_pn, (u8 *)&u.cmd.common.rx_secur_seq_cnt,
3510a6dfbd04SJohannes Berg 				   rx_pn_len) > 0)
3511a6dfbd04SJohannes Berg 			memcpy(&u.cmd.common.rx_secur_seq_cnt, rx_pn,
3512a6dfbd04SJohannes Berg 			       rx_pn_len);
3513a6dfbd04SJohannes Berg 	}
3514a6dfbd04SJohannes Berg 
3515199d895fSJohannes Berg 	if (api_ver >= 2) {
351645c458b4SSara Sharon 		u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
351745c458b4SSara Sharon 		size = sizeof(u.cmd);
351845c458b4SSara Sharon 	} else {
351945c458b4SSara Sharon 		size = sizeof(u.cmd_v1);
352045c458b4SSara Sharon 	}
3521e705c121SKalle Valo 
3522e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
3523e705c121SKalle Valo 	if (cmd_flags & CMD_ASYNC)
352445c458b4SSara Sharon 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
352545c458b4SSara Sharon 					   &u.cmd);
3526e705c121SKalle Valo 	else
352745c458b4SSara Sharon 		ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
352845c458b4SSara Sharon 						  &u.cmd, &status);
3529e705c121SKalle Valo 
3530e705c121SKalle Valo 	switch (status) {
3531e705c121SKalle Valo 	case ADD_STA_SUCCESS:
3532e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3533e705c121SKalle Valo 		break;
3534e705c121SKalle Valo 	default:
3535e705c121SKalle Valo 		ret = -EIO;
3536e705c121SKalle Valo 		IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3537e705c121SKalle Valo 		break;
3538e705c121SKalle Valo 	}
3539e705c121SKalle Valo 
3540e705c121SKalle Valo 	return ret;
3541e705c121SKalle Valo }
3542e705c121SKalle Valo 
3543e705c121SKalle Valo static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3544e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
3545e705c121SKalle Valo 				 u8 sta_id, bool remove_key)
3546e705c121SKalle Valo {
3547e705c121SKalle Valo 	struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3548e705c121SKalle Valo 
3549e705c121SKalle Valo 	/* verify the key details match the required command's expectations */
35508e160ab8SAyala Beker 	if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3551b1fdc250SJohannes Berg 		    (keyconf->keyidx != 4 && keyconf->keyidx != 5 &&
3552b1fdc250SJohannes Berg 		     keyconf->keyidx != 6 && keyconf->keyidx != 7) ||
35538e160ab8SAyala Beker 		    (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
35548e160ab8SAyala Beker 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
35558e160ab8SAyala Beker 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
35568e160ab8SAyala Beker 		return -EINVAL;
35578e160ab8SAyala Beker 
35588e160ab8SAyala Beker 	if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
35598e160ab8SAyala Beker 		    keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3560e705c121SKalle Valo 		return -EINVAL;
3561e705c121SKalle Valo 
3562e705c121SKalle Valo 	igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3563e705c121SKalle Valo 	igtk_cmd.sta_id = cpu_to_le32(sta_id);
3564e705c121SKalle Valo 
3565e705c121SKalle Valo 	if (remove_key) {
3566197288d5SLuca Coelho 		/* This is a valid situation for IGTK */
3567197288d5SLuca Coelho 		if (sta_id == IWL_MVM_INVALID_STA)
3568197288d5SLuca Coelho 			return 0;
3569197288d5SLuca Coelho 
3570e705c121SKalle Valo 		igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3571e705c121SKalle Valo 	} else {
3572e705c121SKalle Valo 		struct ieee80211_key_seq seq;
3573e705c121SKalle Valo 		const u8 *pn;
3574e705c121SKalle Valo 
3575aa950524SAyala Beker 		switch (keyconf->cipher) {
3576aa950524SAyala Beker 		case WLAN_CIPHER_SUITE_AES_CMAC:
3577aa950524SAyala Beker 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3578aa950524SAyala Beker 			break;
35798e160ab8SAyala Beker 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
35808e160ab8SAyala Beker 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
35818e160ab8SAyala Beker 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
35828e160ab8SAyala Beker 			break;
3583aa950524SAyala Beker 		default:
3584aa950524SAyala Beker 			return -EINVAL;
3585aa950524SAyala Beker 		}
3586aa950524SAyala Beker 
35878e160ab8SAyala Beker 		memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
35888e160ab8SAyala Beker 		if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
35898e160ab8SAyala Beker 			igtk_cmd.ctrl_flags |=
35908e160ab8SAyala Beker 				cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3591e705c121SKalle Valo 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3592e705c121SKalle Valo 		pn = seq.aes_cmac.pn;
3593e705c121SKalle Valo 		igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3594e705c121SKalle Valo 						       ((u64) pn[4] << 8) |
3595e705c121SKalle Valo 						       ((u64) pn[3] << 16) |
3596e705c121SKalle Valo 						       ((u64) pn[2] << 24) |
3597e705c121SKalle Valo 						       ((u64) pn[1] << 32) |
3598e705c121SKalle Valo 						       ((u64) pn[0] << 40));
3599e705c121SKalle Valo 	}
3600e705c121SKalle Valo 
3601b1fdc250SJohannes Berg 	IWL_DEBUG_INFO(mvm, "%s %sIGTK (%d) for sta %u\n",
3602e705c121SKalle Valo 		       remove_key ? "removing" : "installing",
3603b1fdc250SJohannes Berg 		       keyconf->keyidx >= 6 ? "B" : "",
3604b1fdc250SJohannes Berg 		       keyconf->keyidx, igtk_cmd.sta_id);
3605e705c121SKalle Valo 
36068e160ab8SAyala Beker 	if (!iwl_mvm_has_new_rx_api(mvm)) {
36078e160ab8SAyala Beker 		struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
36088e160ab8SAyala Beker 			.ctrl_flags = igtk_cmd.ctrl_flags,
36098e160ab8SAyala Beker 			.key_id = igtk_cmd.key_id,
36108e160ab8SAyala Beker 			.sta_id = igtk_cmd.sta_id,
36118e160ab8SAyala Beker 			.receive_seq_cnt = igtk_cmd.receive_seq_cnt
36128e160ab8SAyala Beker 		};
36138e160ab8SAyala Beker 
36148e160ab8SAyala Beker 		memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
36158e160ab8SAyala Beker 		       ARRAY_SIZE(igtk_cmd_v1.igtk));
36168e160ab8SAyala Beker 		return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
36178e160ab8SAyala Beker 					    sizeof(igtk_cmd_v1), &igtk_cmd_v1);
36188e160ab8SAyala Beker 	}
3619e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3620e705c121SKalle Valo 				    sizeof(igtk_cmd), &igtk_cmd);
3621e705c121SKalle Valo }
3622e705c121SKalle Valo 
3623e705c121SKalle Valo 
3624e705c121SKalle Valo static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3625e705c121SKalle Valo 				       struct ieee80211_vif *vif,
3626e705c121SKalle Valo 				       struct ieee80211_sta *sta)
3627e705c121SKalle Valo {
3628e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3629e705c121SKalle Valo 
3630e705c121SKalle Valo 	if (sta)
3631e705c121SKalle Valo 		return sta->addr;
3632e705c121SKalle Valo 
3633e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
36340ae98812SSara Sharon 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3635e705c121SKalle Valo 		u8 sta_id = mvmvif->ap_sta_id;
3636e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3637e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
3638e705c121SKalle Valo 		return sta->addr;
3639e705c121SKalle Valo 	}
3640e705c121SKalle Valo 
3641e705c121SKalle Valo 
3642e705c121SKalle Valo 	return NULL;
3643e705c121SKalle Valo }
3644e705c121SKalle Valo 
3645e705c121SKalle Valo static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3646e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3647e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3648e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
36494615fd15SEmmanuel Grumbach 				 u8 key_offset,
3650e705c121SKalle Valo 				 bool mcast)
3651e705c121SKalle Valo {
3652e705c121SKalle Valo 	const u8 *addr;
3653e705c121SKalle Valo 	struct ieee80211_key_seq seq;
3654e705c121SKalle Valo 	u16 p1k[5];
365585aeb58cSDavid Spinadel 	u32 sta_id;
36564883145aSEmmanuel Grumbach 	bool mfp = false;
365785aeb58cSDavid Spinadel 
365885aeb58cSDavid Spinadel 	if (sta) {
365985aeb58cSDavid Spinadel 		struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
366085aeb58cSDavid Spinadel 
366185aeb58cSDavid Spinadel 		sta_id = mvm_sta->sta_id;
36624883145aSEmmanuel Grumbach 		mfp = sta->mfp;
366385aeb58cSDavid Spinadel 	} else if (vif->type == NL80211_IFTYPE_AP &&
366485aeb58cSDavid Spinadel 		   !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
366585aeb58cSDavid Spinadel 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
366685aeb58cSDavid Spinadel 
366785aeb58cSDavid Spinadel 		sta_id = mvmvif->mcast_sta.sta_id;
366885aeb58cSDavid Spinadel 	} else {
366985aeb58cSDavid Spinadel 		IWL_ERR(mvm, "Failed to find station id\n");
367085aeb58cSDavid Spinadel 		return -EINVAL;
367185aeb58cSDavid Spinadel 	}
3672e705c121SKalle Valo 
367395a62c33SJohannes Berg 	if (keyconf->cipher == WLAN_CIPHER_SUITE_TKIP) {
3674e705c121SKalle Valo 		addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3675e705c121SKalle Valo 		/* get phase 1 key from mac80211 */
3676e705c121SKalle Valo 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3677e705c121SKalle Valo 		ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
367895a62c33SJohannes Berg 
367995a62c33SJohannes Berg 		return iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
36804883145aSEmmanuel Grumbach 					    seq.tkip.iv32, p1k, 0, key_offset,
36814883145aSEmmanuel Grumbach 					    mfp);
3682e705c121SKalle Valo 	}
3683e705c121SKalle Valo 
368495a62c33SJohannes Berg 	return iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
368595a62c33SJohannes Berg 				    0, NULL, 0, key_offset, mfp);
3686e705c121SKalle Valo }
3687e705c121SKalle Valo 
3688e705c121SKalle Valo int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3689e705c121SKalle Valo 			struct ieee80211_vif *vif,
3690e705c121SKalle Valo 			struct ieee80211_sta *sta,
3691e705c121SKalle Valo 			struct ieee80211_key_conf *keyconf,
36924615fd15SEmmanuel Grumbach 			u8 key_offset)
3693e705c121SKalle Valo {
3694e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
36955f7a1847SJohannes Berg 	struct iwl_mvm_sta *mvm_sta;
369685aeb58cSDavid Spinadel 	u8 sta_id = IWL_MVM_INVALID_STA;
3697e705c121SKalle Valo 	int ret;
3698e705c121SKalle Valo 	static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3699e705c121SKalle Valo 
3700e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3701e705c121SKalle Valo 
370285aeb58cSDavid Spinadel 	if (vif->type != NL80211_IFTYPE_AP ||
370385aeb58cSDavid Spinadel 	    keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3704e705c121SKalle Valo 		/* Get the station id from the mvm local station table */
37055f7a1847SJohannes Berg 		mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
37065f7a1847SJohannes Berg 		if (!mvm_sta) {
37075f7a1847SJohannes Berg 			IWL_ERR(mvm, "Failed to find station\n");
3708e705c121SKalle Valo 			return -EINVAL;
3709e705c121SKalle Valo 		}
37105f7a1847SJohannes Berg 		sta_id = mvm_sta->sta_id;
3711e705c121SKalle Valo 
3712e705c121SKalle Valo 		/*
3713e705c121SKalle Valo 		 * It is possible that the 'sta' parameter is NULL, and thus
371485aeb58cSDavid Spinadel 		 * there is a need to retrieve the sta from the local station
371585aeb58cSDavid Spinadel 		 * table.
3716e705c121SKalle Valo 		 */
3717e705c121SKalle Valo 		if (!sta) {
371885aeb58cSDavid Spinadel 			sta = rcu_dereference_protected(
371985aeb58cSDavid Spinadel 				mvm->fw_id_to_mac_id[sta_id],
3720e705c121SKalle Valo 				lockdep_is_held(&mvm->mutex));
3721e705c121SKalle Valo 			if (IS_ERR_OR_NULL(sta)) {
3722e705c121SKalle Valo 				IWL_ERR(mvm, "Invalid station id\n");
3723e705c121SKalle Valo 				return -EINVAL;
3724e705c121SKalle Valo 			}
3725e705c121SKalle Valo 		}
3726e705c121SKalle Valo 
3727e705c121SKalle Valo 		if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3728e705c121SKalle Valo 			return -EINVAL;
3729e829b17cSBeni Lev 	} else {
3730e829b17cSBeni Lev 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3731e829b17cSBeni Lev 
3732e829b17cSBeni Lev 		sta_id = mvmvif->mcast_sta.sta_id;
3733e829b17cSBeni Lev 	}
3734e829b17cSBeni Lev 
3735e829b17cSBeni Lev 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3736e829b17cSBeni Lev 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3737e829b17cSBeni Lev 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3738e829b17cSBeni Lev 		ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3739e829b17cSBeni Lev 		goto end;
374085aeb58cSDavid Spinadel 	}
3741e705c121SKalle Valo 
37424615fd15SEmmanuel Grumbach 	/* If the key_offset is not pre-assigned, we need to find a
37434615fd15SEmmanuel Grumbach 	 * new offset to use.  In normal cases, the offset is not
37444615fd15SEmmanuel Grumbach 	 * pre-assigned, but during HW_RESTART we want to reuse the
37454615fd15SEmmanuel Grumbach 	 * same indices, so we pass them when this function is called.
37464615fd15SEmmanuel Grumbach 	 *
37474615fd15SEmmanuel Grumbach 	 * In D3 entry, we need to hardcoded the indices (because the
37484615fd15SEmmanuel Grumbach 	 * firmware hardcodes the PTK offset to 0).  In this case, we
37494615fd15SEmmanuel Grumbach 	 * need to make sure we don't overwrite the hw_key_idx in the
37504615fd15SEmmanuel Grumbach 	 * keyconf structure, because otherwise we cannot configure
37514615fd15SEmmanuel Grumbach 	 * the original ones back when resuming.
3752e705c121SKalle Valo 	 */
37534615fd15SEmmanuel Grumbach 	if (key_offset == STA_KEY_IDX_INVALID) {
37544615fd15SEmmanuel Grumbach 		key_offset  = iwl_mvm_set_fw_key_idx(mvm);
37554615fd15SEmmanuel Grumbach 		if (key_offset == STA_KEY_IDX_INVALID)
3756e705c121SKalle Valo 			return -ENOSPC;
37574615fd15SEmmanuel Grumbach 		keyconf->hw_key_idx = key_offset;
3758e705c121SKalle Valo 	}
3759e705c121SKalle Valo 
37604615fd15SEmmanuel Grumbach 	ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
37619c3deeb5SLuca Coelho 	if (ret)
3762e705c121SKalle Valo 		goto end;
3763e705c121SKalle Valo 
3764e705c121SKalle Valo 	/*
3765e705c121SKalle Valo 	 * For WEP, the same key is used for multicast and unicast. Upload it
3766e705c121SKalle Valo 	 * again, using the same key offset, and now pointing the other one
3767e705c121SKalle Valo 	 * to the same key slot (offset).
3768e705c121SKalle Valo 	 * If this fails, remove the original as well.
3769e705c121SKalle Valo 	 */
377085aeb58cSDavid Spinadel 	if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
377185aeb58cSDavid Spinadel 	     keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
377285aeb58cSDavid Spinadel 	    sta) {
37734615fd15SEmmanuel Grumbach 		ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
37744615fd15SEmmanuel Grumbach 					    key_offset, !mcast);
3775e705c121SKalle Valo 		if (ret) {
3776e705c121SKalle Valo 			__iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
37779c3deeb5SLuca Coelho 			goto end;
3778e705c121SKalle Valo 		}
3779e705c121SKalle Valo 	}
3780e705c121SKalle Valo 
37819c3deeb5SLuca Coelho 	__set_bit(key_offset, mvm->fw_key_table);
37829c3deeb5SLuca Coelho 
3783e705c121SKalle Valo end:
3784e705c121SKalle Valo 	IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3785e705c121SKalle Valo 		      keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3786e705c121SKalle Valo 		      sta ? sta->addr : zero_addr, ret);
3787e705c121SKalle Valo 	return ret;
3788e705c121SKalle Valo }
3789e705c121SKalle Valo 
3790e705c121SKalle Valo int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3791e705c121SKalle Valo 			   struct ieee80211_vif *vif,
3792e705c121SKalle Valo 			   struct ieee80211_sta *sta,
3793e705c121SKalle Valo 			   struct ieee80211_key_conf *keyconf)
3794e705c121SKalle Valo {
3795e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
37965f7a1847SJohannes Berg 	struct iwl_mvm_sta *mvm_sta;
37970ae98812SSara Sharon 	u8 sta_id = IWL_MVM_INVALID_STA;
3798e705c121SKalle Valo 	int ret, i;
3799e705c121SKalle Valo 
3800e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3801e705c121SKalle Valo 
38025f7a1847SJohannes Berg 	/* Get the station from the mvm local station table */
38035f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
380471793b7dSLuca Coelho 	if (mvm_sta)
3805cd4d23c1SIlan Peer 		sta_id = mvm_sta->sta_id;
380685aeb58cSDavid Spinadel 	else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
380785aeb58cSDavid Spinadel 		sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
380885aeb58cSDavid Spinadel 
3809e705c121SKalle Valo 
3810e705c121SKalle Valo 	IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3811e705c121SKalle Valo 		      keyconf->keyidx, sta_id);
3812e705c121SKalle Valo 
3813197288d5SLuca Coelho 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
38148e160ab8SAyala Beker 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3815197288d5SLuca Coelho 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3816e705c121SKalle Valo 		return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3817e705c121SKalle Valo 
3818e705c121SKalle Valo 	if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3819e705c121SKalle Valo 		IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3820e705c121SKalle Valo 			keyconf->hw_key_idx);
3821e705c121SKalle Valo 		return -ENOENT;
3822e705c121SKalle Valo 	}
3823e705c121SKalle Valo 
3824e705c121SKalle Valo 	/* track which key was deleted last */
3825e705c121SKalle Valo 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3826e705c121SKalle Valo 		if (mvm->fw_key_deleted[i] < U8_MAX)
3827e705c121SKalle Valo 			mvm->fw_key_deleted[i]++;
3828e705c121SKalle Valo 	}
3829e705c121SKalle Valo 	mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3830e705c121SKalle Valo 
383185aeb58cSDavid Spinadel 	if (sta && !mvm_sta) {
3832e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3833e705c121SKalle Valo 		return 0;
3834e705c121SKalle Valo 	}
3835e705c121SKalle Valo 
3836e705c121SKalle Valo 	ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3837e705c121SKalle Valo 	if (ret)
3838e705c121SKalle Valo 		return ret;
3839e705c121SKalle Valo 
3840e705c121SKalle Valo 	/* delete WEP key twice to get rid of (now useless) offset */
3841e705c121SKalle Valo 	if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3842e705c121SKalle Valo 	    keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3843e705c121SKalle Valo 		ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3844e705c121SKalle Valo 
3845e705c121SKalle Valo 	return ret;
3846e705c121SKalle Valo }
3847e705c121SKalle Valo 
3848e705c121SKalle Valo void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3849e705c121SKalle Valo 			     struct ieee80211_vif *vif,
3850e705c121SKalle Valo 			     struct ieee80211_key_conf *keyconf,
3851e705c121SKalle Valo 			     struct ieee80211_sta *sta, u32 iv32,
3852e705c121SKalle Valo 			     u16 *phase1key)
3853e705c121SKalle Valo {
3854e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta;
3855e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
38564883145aSEmmanuel Grumbach 	bool mfp = sta ? sta->mfp : false;
3857e705c121SKalle Valo 
3858e705c121SKalle Valo 	rcu_read_lock();
3859e705c121SKalle Valo 
38605f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
38615f7a1847SJohannes Berg 	if (WARN_ON_ONCE(!mvm_sta))
386212f17211SEmmanuel Grumbach 		goto unlock;
386385aeb58cSDavid Spinadel 	iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
38644883145aSEmmanuel Grumbach 			     iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx,
38654883145aSEmmanuel Grumbach 			     mfp);
386612f17211SEmmanuel Grumbach 
386712f17211SEmmanuel Grumbach  unlock:
3868e705c121SKalle Valo 	rcu_read_unlock();
3869e705c121SKalle Valo }
3870e705c121SKalle Valo 
3871e705c121SKalle Valo void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3872e705c121SKalle Valo 				struct ieee80211_sta *sta)
3873e705c121SKalle Valo {
3874e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3875e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3876e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3877e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3878e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_PS),
3879e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3880e705c121SKalle Valo 	};
3881e705c121SKalle Valo 	int ret;
3882e705c121SKalle Valo 
3883854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3884854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3885e705c121SKalle Valo 	if (ret)
3886e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3887e705c121SKalle Valo }
3888e705c121SKalle Valo 
3889e705c121SKalle Valo void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3890e705c121SKalle Valo 				       struct ieee80211_sta *sta,
3891e705c121SKalle Valo 				       enum ieee80211_frame_release_type reason,
3892e705c121SKalle Valo 				       u16 cnt, u16 tids, bool more_data,
38939a3fcf91SSara Sharon 				       bool single_sta_queue)
3894e705c121SKalle Valo {
3895e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3896e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3897e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3898e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3899e705c121SKalle Valo 		.modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3900e705c121SKalle Valo 		.sleep_tx_count = cpu_to_le16(cnt),
3901e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3902e705c121SKalle Valo 	};
3903e705c121SKalle Valo 	int tid, ret;
3904e705c121SKalle Valo 	unsigned long _tids = tids;
3905e705c121SKalle Valo 
3906e705c121SKalle Valo 	/* convert TIDs to ACs - we don't support TSPEC so that's OK
3907e705c121SKalle Valo 	 * Note that this field is reserved and unused by firmware not
3908e705c121SKalle Valo 	 * supporting GO uAPSD, so it's safe to always do this.
3909e705c121SKalle Valo 	 */
3910e705c121SKalle Valo 	for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3911e705c121SKalle Valo 		cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3912e705c121SKalle Valo 
39139a3fcf91SSara Sharon 	/* If we're releasing frames from aggregation or dqa queues then check
39149a3fcf91SSara Sharon 	 * if all the queues that we're releasing frames from, combined, have:
3915e705c121SKalle Valo 	 *  - more frames than the service period, in which case more_data
3916e705c121SKalle Valo 	 *    needs to be set
3917e705c121SKalle Valo 	 *  - fewer than 'cnt' frames, in which case we need to adjust the
3918e705c121SKalle Valo 	 *    firmware command (but do that unconditionally)
3919e705c121SKalle Valo 	 */
39209a3fcf91SSara Sharon 	if (single_sta_queue) {
3921e705c121SKalle Valo 		int remaining = cnt;
392236be0eb6SEmmanuel Grumbach 		int sleep_tx_count;
3923e705c121SKalle Valo 
3924e705c121SKalle Valo 		spin_lock_bh(&mvmsta->lock);
3925e705c121SKalle Valo 		for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3926e705c121SKalle Valo 			struct iwl_mvm_tid_data *tid_data;
3927e705c121SKalle Valo 			u16 n_queued;
3928e705c121SKalle Valo 
3929e705c121SKalle Valo 			tid_data = &mvmsta->tid_data[tid];
3930e705c121SKalle Valo 
3931dd32162dSLiad Kaufman 			n_queued = iwl_mvm_tid_queued(mvm, tid_data);
3932e705c121SKalle Valo 			if (n_queued > remaining) {
3933e705c121SKalle Valo 				more_data = true;
3934e705c121SKalle Valo 				remaining = 0;
3935e705c121SKalle Valo 				break;
3936e705c121SKalle Valo 			}
3937e705c121SKalle Valo 			remaining -= n_queued;
3938e705c121SKalle Valo 		}
393936be0eb6SEmmanuel Grumbach 		sleep_tx_count = cnt - remaining;
394036be0eb6SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
394136be0eb6SEmmanuel Grumbach 			mvmsta->sleep_tx_count = sleep_tx_count;
3942e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
3943e705c121SKalle Valo 
394436be0eb6SEmmanuel Grumbach 		cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3945e705c121SKalle Valo 		if (WARN_ON(cnt - remaining == 0)) {
3946e705c121SKalle Valo 			ieee80211_sta_eosp(sta);
3947e705c121SKalle Valo 			return;
3948e705c121SKalle Valo 		}
3949e705c121SKalle Valo 	}
3950e705c121SKalle Valo 
3951e705c121SKalle Valo 	/* Note: this is ignored by firmware not supporting GO uAPSD */
3952e705c121SKalle Valo 	if (more_data)
3953ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
3954e705c121SKalle Valo 
3955e705c121SKalle Valo 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3956e705c121SKalle Valo 		mvmsta->next_status_eosp = true;
3957ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
3958e705c121SKalle Valo 	} else {
3959ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
3960e705c121SKalle Valo 	}
3961e705c121SKalle Valo 
3962156f92f2SEmmanuel Grumbach 	/* block the Tx queues until the FW updated the sleep Tx count */
3963156f92f2SEmmanuel Grumbach 	iwl_trans_block_txq_ptrs(mvm->trans, true);
3964156f92f2SEmmanuel Grumbach 
3965156f92f2SEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3966156f92f2SEmmanuel Grumbach 				   CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3967854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3968e705c121SKalle Valo 	if (ret)
3969e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3970e705c121SKalle Valo }
3971e705c121SKalle Valo 
3972e705c121SKalle Valo void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3973e705c121SKalle Valo 			   struct iwl_rx_cmd_buffer *rxb)
3974e705c121SKalle Valo {
3975e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3976e705c121SKalle Valo 	struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3977e705c121SKalle Valo 	struct ieee80211_sta *sta;
3978e705c121SKalle Valo 	u32 sta_id = le32_to_cpu(notif->sta_id);
3979e705c121SKalle Valo 
3980be9ae34eSNathan Errera 	if (WARN_ON_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations))
3981e705c121SKalle Valo 		return;
3982e705c121SKalle Valo 
3983e705c121SKalle Valo 	rcu_read_lock();
3984e705c121SKalle Valo 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3985e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(sta))
3986e705c121SKalle Valo 		ieee80211_sta_eosp(sta);
3987e705c121SKalle Valo 	rcu_read_unlock();
3988e705c121SKalle Valo }
3989e705c121SKalle Valo 
3990e705c121SKalle Valo void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3991e705c121SKalle Valo 				   struct iwl_mvm_sta *mvmsta, bool disable)
3992e705c121SKalle Valo {
3993e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3994e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3995e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3996e705c121SKalle Valo 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3997e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3998e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3999e705c121SKalle Valo 	};
4000e705c121SKalle Valo 	int ret;
4001e705c121SKalle Valo 
4002854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
4003854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
4004e705c121SKalle Valo 	if (ret)
4005e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
4006e705c121SKalle Valo }
4007e705c121SKalle Valo 
4008e705c121SKalle Valo void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
4009e705c121SKalle Valo 				      struct ieee80211_sta *sta,
4010e705c121SKalle Valo 				      bool disable)
4011e705c121SKalle Valo {
4012e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4013e705c121SKalle Valo 
4014e705c121SKalle Valo 	spin_lock_bh(&mvm_sta->lock);
4015e705c121SKalle Valo 
4016e705c121SKalle Valo 	if (mvm_sta->disable_tx == disable) {
4017e705c121SKalle Valo 		spin_unlock_bh(&mvm_sta->lock);
4018e705c121SKalle Valo 		return;
4019e705c121SKalle Valo 	}
4020e705c121SKalle Valo 
4021e705c121SKalle Valo 	mvm_sta->disable_tx = disable;
4022e705c121SKalle Valo 
4023b1c6cec0SNaftali Goldstein 	/*
4024b1c6cec0SNaftali Goldstein 	 * If sta PS state is handled by mac80211, tell it to start/stop
4025b1c6cec0SNaftali Goldstein 	 * queuing tx for this station.
4026b1c6cec0SNaftali Goldstein 	 */
4027b1c6cec0SNaftali Goldstein 	if (!ieee80211_hw_check(mvm->hw, AP_LINK_PS))
4028e705c121SKalle Valo 		ieee80211_sta_block_awake(mvm->hw, sta, disable);
4029e705c121SKalle Valo 
4030e705c121SKalle Valo 	iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
4031e705c121SKalle Valo 
4032e705c121SKalle Valo 	spin_unlock_bh(&mvm_sta->lock);
4033e705c121SKalle Valo }
4034e705c121SKalle Valo 
4035ced19f26SSara Sharon static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
4036ced19f26SSara Sharon 					      struct iwl_mvm_vif *mvmvif,
4037ced19f26SSara Sharon 					      struct iwl_mvm_int_sta *sta,
4038ced19f26SSara Sharon 					      bool disable)
4039ced19f26SSara Sharon {
4040ced19f26SSara Sharon 	u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
4041ced19f26SSara Sharon 	struct iwl_mvm_add_sta_cmd cmd = {
4042ced19f26SSara Sharon 		.add_modify = STA_MODE_MODIFY,
4043ced19f26SSara Sharon 		.sta_id = sta->sta_id,
4044ced19f26SSara Sharon 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
4045ced19f26SSara Sharon 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
4046ced19f26SSara Sharon 		.mac_id_n_color = cpu_to_le32(id),
4047ced19f26SSara Sharon 	};
4048ced19f26SSara Sharon 	int ret;
4049ced19f26SSara Sharon 
4050e5d153ecSEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
4051ced19f26SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
4052ced19f26SSara Sharon 	if (ret)
4053ced19f26SSara Sharon 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
4054ced19f26SSara Sharon }
4055ced19f26SSara Sharon 
4056e705c121SKalle Valo void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
4057e705c121SKalle Valo 				       struct iwl_mvm_vif *mvmvif,
4058e705c121SKalle Valo 				       bool disable)
4059e705c121SKalle Valo {
4060e705c121SKalle Valo 	struct ieee80211_sta *sta;
4061e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta;
4062e705c121SKalle Valo 	int i;
4063e705c121SKalle Valo 
4064e5d153ecSEmmanuel Grumbach 	rcu_read_lock();
4065e705c121SKalle Valo 
4066e705c121SKalle Valo 	/* Block/unblock all the stations of the given mvmvif */
4067be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
4068e5d153ecSEmmanuel Grumbach 		sta = rcu_dereference(mvm->fw_id_to_mac_id[i]);
4069e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
4070e705c121SKalle Valo 			continue;
4071e705c121SKalle Valo 
4072e705c121SKalle Valo 		mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4073e705c121SKalle Valo 		if (mvm_sta->mac_id_n_color !=
4074e705c121SKalle Valo 		    FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
4075e705c121SKalle Valo 			continue;
4076e705c121SKalle Valo 
4077e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
4078e705c121SKalle Valo 	}
4079ced19f26SSara Sharon 
4080e5d153ecSEmmanuel Grumbach 	rcu_read_unlock();
4081e5d153ecSEmmanuel Grumbach 
4082ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
4083ced19f26SSara Sharon 		return;
4084ced19f26SSara Sharon 
4085ced19f26SSara Sharon 	/* Need to block/unblock also multicast station */
4086ced19f26SSara Sharon 	if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
4087ced19f26SSara Sharon 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
4088ced19f26SSara Sharon 						  &mvmvif->mcast_sta, disable);
4089ced19f26SSara Sharon 
4090ced19f26SSara Sharon 	/*
4091ced19f26SSara Sharon 	 * Only unblock the broadcast station (FW blocks it for immediate
4092ced19f26SSara Sharon 	 * quiet, not the driver)
4093ced19f26SSara Sharon 	 */
4094ced19f26SSara Sharon 	if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
4095ced19f26SSara Sharon 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
4096ced19f26SSara Sharon 						  &mvmvif->bcast_sta, disable);
4097e705c121SKalle Valo }
4098e705c121SKalle Valo 
4099e705c121SKalle Valo void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
4100e705c121SKalle Valo {
4101e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4102e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
4103e705c121SKalle Valo 
4104e705c121SKalle Valo 	rcu_read_lock();
4105e705c121SKalle Valo 
4106e705c121SKalle Valo 	mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
4107e705c121SKalle Valo 
4108ad12b231SNathan Errera 	if (mvmsta)
4109e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
4110e705c121SKalle Valo 
4111e705c121SKalle Valo 	rcu_read_unlock();
4112e705c121SKalle Valo }
4113dd32162dSLiad Kaufman 
4114dd32162dSLiad Kaufman u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
4115dd32162dSLiad Kaufman {
4116dd32162dSLiad Kaufman 	u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
4117dd32162dSLiad Kaufman 
4118dd32162dSLiad Kaufman 	/*
41192f7a3863SLuca Coelho 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
4120dd32162dSLiad Kaufman 	 * to align the wrap around of ssn so we compare relevant values.
4121dd32162dSLiad Kaufman 	 */
4122286ca8ebSLuca Coelho 	if (mvm->trans->trans_cfg->gen2)
4123dd32162dSLiad Kaufman 		sn &= 0xff;
4124dd32162dSLiad Kaufman 
4125dd32162dSLiad Kaufman 	return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
4126dd32162dSLiad Kaufman }
4127be82ecd3SAvraham Stern 
4128be82ecd3SAvraham Stern int iwl_mvm_add_pasn_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
4129be82ecd3SAvraham Stern 			 struct iwl_mvm_int_sta *sta, u8 *addr, u32 cipher,
4130be82ecd3SAvraham Stern 			 u8 *key, u32 key_len)
4131be82ecd3SAvraham Stern {
4132be82ecd3SAvraham Stern 	int ret;
4133be82ecd3SAvraham Stern 	u16 queue;
4134be82ecd3SAvraham Stern 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4135be82ecd3SAvraham Stern 	struct ieee80211_key_conf *keyconf;
4136be82ecd3SAvraham Stern 
4137be82ecd3SAvraham Stern 	ret = iwl_mvm_allocate_int_sta(mvm, sta, 0,
4138be82ecd3SAvraham Stern 				       NL80211_IFTYPE_UNSPECIFIED,
4139be82ecd3SAvraham Stern 				       IWL_STA_LINK);
4140be82ecd3SAvraham Stern 	if (ret)
4141be82ecd3SAvraham Stern 		return ret;
4142be82ecd3SAvraham Stern 
4143be82ecd3SAvraham Stern 	ret = iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
4144be82ecd3SAvraham Stern 					     addr, sta, &queue,
4145be82ecd3SAvraham Stern 					     IWL_MVM_TX_FIFO_BE);
4146be82ecd3SAvraham Stern 	if (ret)
4147be82ecd3SAvraham Stern 		goto out;
4148be82ecd3SAvraham Stern 
4149be82ecd3SAvraham Stern 	keyconf = kzalloc(sizeof(*keyconf) + key_len, GFP_KERNEL);
4150be82ecd3SAvraham Stern 	if (!keyconf) {
4151be82ecd3SAvraham Stern 		ret = -ENOBUFS;
4152be82ecd3SAvraham Stern 		goto out;
4153be82ecd3SAvraham Stern 	}
4154be82ecd3SAvraham Stern 
4155be82ecd3SAvraham Stern 	keyconf->cipher = cipher;
4156be82ecd3SAvraham Stern 	memcpy(keyconf->key, key, key_len);
4157be82ecd3SAvraham Stern 	keyconf->keylen = key_len;
4158be82ecd3SAvraham Stern 
4159be82ecd3SAvraham Stern 	ret = iwl_mvm_send_sta_key(mvm, sta->sta_id, keyconf, false,
4160be82ecd3SAvraham Stern 				   0, NULL, 0, 0, true);
4161be82ecd3SAvraham Stern 	kfree(keyconf);
4162be82ecd3SAvraham Stern 	return 0;
4163be82ecd3SAvraham Stern out:
4164be82ecd3SAvraham Stern 	iwl_mvm_dealloc_int_sta(mvm, sta);
4165be82ecd3SAvraham Stern 	return ret;
4166be82ecd3SAvraham Stern }
4167ad12b231SNathan Errera 
4168ad12b231SNathan Errera void iwl_mvm_cancel_channel_switch(struct iwl_mvm *mvm,
4169ad12b231SNathan Errera 				   struct ieee80211_vif *vif,
4170ad12b231SNathan Errera 				   u32 mac_id)
4171ad12b231SNathan Errera {
4172ad12b231SNathan Errera 	struct iwl_cancel_channel_switch_cmd cancel_channel_switch_cmd = {
4173ad12b231SNathan Errera 		.mac_id = cpu_to_le32(mac_id),
4174ad12b231SNathan Errera 	};
4175ad12b231SNathan Errera 	int ret;
4176ad12b231SNathan Errera 
4177ad12b231SNathan Errera 	ret = iwl_mvm_send_cmd_pdu(mvm,
4178f0c86427SJohannes Berg 				   WIDE_ID(MAC_CONF_GROUP, CANCEL_CHANNEL_SWITCH_CMD),
4179ad12b231SNathan Errera 				   CMD_ASYNC,
4180ad12b231SNathan Errera 				   sizeof(cancel_channel_switch_cmd),
4181ad12b231SNathan Errera 				   &cancel_channel_switch_cmd);
4182ad12b231SNathan Errera 	if (ret)
4183ad12b231SNathan Errera 		IWL_ERR(mvm, "Failed to cancel the channel switch\n");
4184ad12b231SNathan Errera }
4185