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"
12e705c121SKalle Valo 
13854c5705SSara Sharon /*
14854c5705SSara Sharon  * New version of ADD_STA_sta command added new fields at the end of the
15854c5705SSara Sharon  * structure, so sending the size of the relevant API's structure is enough to
16854c5705SSara Sharon  * support both API versions.
17854c5705SSara Sharon  */
18854c5705SSara Sharon static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
19854c5705SSara Sharon {
20ced19f26SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) ||
21ced19f26SSara Sharon 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
22ced19f26SSara Sharon 		return sizeof(struct iwl_mvm_add_sta_cmd);
23ced19f26SSara Sharon 	else
24ced19f26SSara Sharon 		return sizeof(struct iwl_mvm_add_sta_cmd_v7);
25854c5705SSara Sharon }
26854c5705SSara Sharon 
27e705c121SKalle Valo static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
28e705c121SKalle Valo 				    enum nl80211_iftype iftype)
29e705c121SKalle Valo {
30e705c121SKalle Valo 	int sta_id;
31e705c121SKalle Valo 	u32 reserved_ids = 0;
32e705c121SKalle Valo 
33be9ae34eSNathan Errera 	BUILD_BUG_ON(IWL_MVM_STATION_COUNT_MAX > 32);
34e705c121SKalle Valo 	WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
35e705c121SKalle Valo 
36e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
37e705c121SKalle Valo 
38e705c121SKalle Valo 	/* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
39e705c121SKalle Valo 	if (iftype != NL80211_IFTYPE_STATION)
40e705c121SKalle Valo 		reserved_ids = BIT(0);
41e705c121SKalle Valo 
42e705c121SKalle Valo 	/* Don't take rcu_read_lock() since we are protected by mvm->mutex */
43be9ae34eSNathan Errera 	for (sta_id = 0; sta_id < mvm->fw->ucode_capa.num_stations; sta_id++) {
44e705c121SKalle Valo 		if (BIT(sta_id) & reserved_ids)
45e705c121SKalle Valo 			continue;
46e705c121SKalle Valo 
47e705c121SKalle Valo 		if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
48e705c121SKalle Valo 					       lockdep_is_held(&mvm->mutex)))
49e705c121SKalle Valo 			return sta_id;
50e705c121SKalle Valo 	}
510ae98812SSara Sharon 	return IWL_MVM_INVALID_STA;
52e705c121SKalle Valo }
53e705c121SKalle Valo 
54e705c121SKalle Valo /* send station add/update command to firmware */
55e705c121SKalle Valo int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
5624afba76SLiad Kaufman 			   bool update, unsigned int flags)
57e705c121SKalle Valo {
58e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
59e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd add_sta_cmd = {
60e705c121SKalle Valo 		.sta_id = mvm_sta->sta_id,
61e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
62e705c121SKalle Valo 		.add_modify = update ? 1 : 0,
63e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
648addabf8SNaftali Goldstein 						 STA_FLG_MIMO_EN_MSK |
658addabf8SNaftali Goldstein 						 STA_FLG_RTS_MIMO_PROT),
66cf0cda19SLiad Kaufman 		.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
67e705c121SKalle Valo 	};
68e705c121SKalle Valo 	int ret;
69e705c121SKalle Valo 	u32 status;
70e705c121SKalle Valo 	u32 agg_size = 0, mpdu_dens = 0;
71e705c121SKalle Valo 
72ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
73ced19f26SSara Sharon 		add_sta_cmd.station_type = mvm_sta->sta_type;
74ced19f26SSara Sharon 
7524afba76SLiad Kaufman 	if (!update || (flags & STA_MODIFY_QUEUES)) {
76e705c121SKalle Valo 		memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
7724afba76SLiad Kaufman 
78bb49701bSSara Sharon 		if (!iwl_mvm_has_new_tx_api(mvm)) {
79bb49701bSSara Sharon 			add_sta_cmd.tfd_queue_msk =
80bb49701bSSara Sharon 				cpu_to_le32(mvm_sta->tfd_queue_msk);
81bb49701bSSara Sharon 
8224afba76SLiad Kaufman 			if (flags & STA_MODIFY_QUEUES)
8324afba76SLiad Kaufman 				add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
84bb49701bSSara Sharon 		} else {
85bb49701bSSara Sharon 			WARN_ON(flags & STA_MODIFY_QUEUES);
86bb49701bSSara Sharon 		}
87e705c121SKalle Valo 	}
88e705c121SKalle Valo 
89046d2e7cSSriram R 	switch (sta->deflink.bandwidth) {
905dca295dSIlan Peer 	case IEEE80211_STA_RX_BW_320:
91e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_160:
92e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
935a2abdcaSGustavo A. R. Silva 		fallthrough;
94e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_80:
95e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
965a2abdcaSGustavo A. R. Silva 		fallthrough;
97e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_40:
98e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
995a2abdcaSGustavo A. R. Silva 		fallthrough;
100e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_20:
101046d2e7cSSriram R 		if (sta->deflink.ht_cap.ht_supported)
102e705c121SKalle Valo 			add_sta_cmd.station_flags |=
103e705c121SKalle Valo 				cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
104e705c121SKalle Valo 		break;
105e705c121SKalle Valo 	}
106e705c121SKalle Valo 
107046d2e7cSSriram R 	switch (sta->deflink.rx_nss) {
108e705c121SKalle Valo 	case 1:
109e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
110e705c121SKalle Valo 		break;
111e705c121SKalle Valo 	case 2:
112e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
113e705c121SKalle Valo 		break;
114e705c121SKalle Valo 	case 3 ... 8:
115e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
116e705c121SKalle Valo 		break;
117e705c121SKalle Valo 	}
118e705c121SKalle Valo 
119261ce887SBenjamin Berg 	switch (sta->deflink.smps_mode) {
120e705c121SKalle Valo 	case IEEE80211_SMPS_AUTOMATIC:
121e705c121SKalle Valo 	case IEEE80211_SMPS_NUM_MODES:
122e705c121SKalle Valo 		WARN_ON(1);
123e705c121SKalle Valo 		break;
124e705c121SKalle Valo 	case IEEE80211_SMPS_STATIC:
125e705c121SKalle Valo 		/* override NSS */
126e705c121SKalle Valo 		add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
127e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
128e705c121SKalle Valo 		break;
129e705c121SKalle Valo 	case IEEE80211_SMPS_DYNAMIC:
130e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
131e705c121SKalle Valo 		break;
132e705c121SKalle Valo 	case IEEE80211_SMPS_OFF:
133e705c121SKalle Valo 		/* nothing */
134e705c121SKalle Valo 		break;
135e705c121SKalle Valo 	}
136e705c121SKalle Valo 
137046d2e7cSSriram R 	if (sta->deflink.ht_cap.ht_supported) {
138e705c121SKalle Valo 		add_sta_cmd.station_flags_msk |=
139e705c121SKalle Valo 			cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
140e705c121SKalle Valo 				    STA_FLG_AGG_MPDU_DENS_MSK);
141e705c121SKalle Valo 
142046d2e7cSSriram R 		mpdu_dens = sta->deflink.ht_cap.ampdu_density;
143e705c121SKalle Valo 	}
144e705c121SKalle Valo 
145eae94cf8SLuca Coelho 	if (mvm_sta->vif->bss_conf.chandef.chan->band == NL80211_BAND_6GHZ) {
146eae94cf8SLuca Coelho 		add_sta_cmd.station_flags_msk |=
147eae94cf8SLuca Coelho 			cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
148eae94cf8SLuca Coelho 				    STA_FLG_AGG_MPDU_DENS_MSK);
149c8a2e7a2SMordechay Goodstein 
150046d2e7cSSriram R 		mpdu_dens = le16_get_bits(sta->deflink.he_6ghz_capa.capa,
151eae94cf8SLuca Coelho 					  IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
152046d2e7cSSriram R 		agg_size = le16_get_bits(sta->deflink.he_6ghz_capa.capa,
153eae94cf8SLuca Coelho 					 IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
154046d2e7cSSriram R 	} else if (sta->deflink.vht_cap.vht_supported) {
155046d2e7cSSriram R 		agg_size = sta->deflink.vht_cap.cap &
156e705c121SKalle Valo 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
157e705c121SKalle Valo 		agg_size >>=
158e705c121SKalle Valo 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
159046d2e7cSSriram R 	} else if (sta->deflink.ht_cap.ht_supported) {
160046d2e7cSSriram R 		agg_size = sta->deflink.ht_cap.ampdu_factor;
161e705c121SKalle Valo 	}
162e705c121SKalle Valo 
163c8a2e7a2SMordechay Goodstein 	/* D6.0 10.12.2 A-MPDU length limit rules
164c8a2e7a2SMordechay Goodstein 	 * A STA indicates the maximum length of the A-MPDU preEOF padding
165c8a2e7a2SMordechay Goodstein 	 * that it can receive in an HE PPDU in the Maximum A-MPDU Length
166c8a2e7a2SMordechay Goodstein 	 * Exponent field in its HT Capabilities, VHT Capabilities,
167c8a2e7a2SMordechay Goodstein 	 * and HE 6 GHz Band Capabilities elements (if present) and the
168c8a2e7a2SMordechay Goodstein 	 * Maximum AMPDU Length Exponent Extension field in its HE
169c8a2e7a2SMordechay Goodstein 	 * Capabilities element
170c8a2e7a2SMordechay Goodstein 	 */
171046d2e7cSSriram R 	if (sta->deflink.he_cap.has_he)
172046d2e7cSSriram R 		agg_size += u8_get_bits(sta->deflink.he_cap.he_cap_elem.mac_cap_info[3],
173c8a2e7a2SMordechay Goodstein 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
174c8a2e7a2SMordechay Goodstein 
175c8a2e7a2SMordechay Goodstein 	/* Limit to max A-MPDU supported by FW */
176c8a2e7a2SMordechay Goodstein 	if (agg_size > (STA_FLG_MAX_AGG_SIZE_4M >> STA_FLG_MAX_AGG_SIZE_SHIFT))
177c8a2e7a2SMordechay Goodstein 		agg_size = (STA_FLG_MAX_AGG_SIZE_4M >>
178c8a2e7a2SMordechay Goodstein 			    STA_FLG_MAX_AGG_SIZE_SHIFT);
179c8a2e7a2SMordechay Goodstein 
180e705c121SKalle Valo 	add_sta_cmd.station_flags |=
181e705c121SKalle Valo 		cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
182e705c121SKalle Valo 	add_sta_cmd.station_flags |=
183e705c121SKalle Valo 		cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
184d94c5a82SGregory Greenman 	if (mvm_sta->sta_state >= IEEE80211_STA_ASSOC)
18519c52f46SEmmanuel Grumbach 		add_sta_cmd.assoc_id = cpu_to_le16(sta->aid);
186e705c121SKalle Valo 
18765e25482SJohannes Berg 	if (sta->wme) {
18865e25482SJohannes Berg 		add_sta_cmd.modify_mask |= STA_MODIFY_UAPSD_ACS;
18965e25482SJohannes Berg 
19065e25482SJohannes Berg 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
191c80eb570SEmmanuel Grumbach 			add_sta_cmd.uapsd_acs |= BIT(AC_BK);
19265e25482SJohannes Berg 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
193c80eb570SEmmanuel Grumbach 			add_sta_cmd.uapsd_acs |= BIT(AC_BE);
19465e25482SJohannes Berg 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
195c80eb570SEmmanuel Grumbach 			add_sta_cmd.uapsd_acs |= BIT(AC_VI);
19665e25482SJohannes Berg 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
197c80eb570SEmmanuel Grumbach 			add_sta_cmd.uapsd_acs |= BIT(AC_VO);
198c80eb570SEmmanuel Grumbach 		add_sta_cmd.uapsd_acs |= add_sta_cmd.uapsd_acs << 4;
199e71ca5eaSEmmanuel Grumbach 		add_sta_cmd.sp_length = sta->max_sp ? sta->max_sp * 2 : 128;
20065e25482SJohannes Berg 	}
20165e25482SJohannes Berg 
202e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
203854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
204854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
205e705c121SKalle Valo 					  &add_sta_cmd, &status);
206e705c121SKalle Valo 	if (ret)
207e705c121SKalle Valo 		return ret;
208e705c121SKalle Valo 
209837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
210e705c121SKalle Valo 	case ADD_STA_SUCCESS:
211e705c121SKalle Valo 		IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
212e705c121SKalle Valo 		break;
213e705c121SKalle Valo 	default:
214e705c121SKalle Valo 		ret = -EIO;
215e705c121SKalle Valo 		IWL_ERR(mvm, "ADD_STA failed\n");
216e705c121SKalle Valo 		break;
217e705c121SKalle Valo 	}
218e705c121SKalle Valo 
219e705c121SKalle Valo 	return ret;
220e705c121SKalle Valo }
221e705c121SKalle Valo 
2228cef5344SKees Cook static void iwl_mvm_rx_agg_session_expired(struct timer_list *t)
22310b2b201SSara Sharon {
2248cef5344SKees Cook 	struct iwl_mvm_baid_data *data =
2258cef5344SKees Cook 		from_timer(data, t, session_timer);
2268cef5344SKees Cook 	struct iwl_mvm_baid_data __rcu **rcu_ptr = data->rcu_ptr;
22710b2b201SSara Sharon 	struct iwl_mvm_baid_data *ba_data;
22810b2b201SSara Sharon 	struct ieee80211_sta *sta;
22910b2b201SSara Sharon 	struct iwl_mvm_sta *mvm_sta;
23010b2b201SSara Sharon 	unsigned long timeout;
23110b2b201SSara Sharon 
23210b2b201SSara Sharon 	rcu_read_lock();
23310b2b201SSara Sharon 
23410b2b201SSara Sharon 	ba_data = rcu_dereference(*rcu_ptr);
23510b2b201SSara Sharon 
23610b2b201SSara Sharon 	if (WARN_ON(!ba_data))
23710b2b201SSara Sharon 		goto unlock;
23810b2b201SSara Sharon 
23910b2b201SSara Sharon 	if (!ba_data->timeout)
24010b2b201SSara Sharon 		goto unlock;
24110b2b201SSara Sharon 
24210b2b201SSara Sharon 	timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
24310b2b201SSara Sharon 	if (time_is_after_jiffies(timeout)) {
24410b2b201SSara Sharon 		mod_timer(&ba_data->session_timer, timeout);
24510b2b201SSara Sharon 		goto unlock;
24610b2b201SSara Sharon 	}
24710b2b201SSara Sharon 
24810b2b201SSara Sharon 	/* Timer expired */
24910b2b201SSara Sharon 	sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
25061dd8a8aSEmmanuel Grumbach 
25161dd8a8aSEmmanuel Grumbach 	/*
25261dd8a8aSEmmanuel Grumbach 	 * sta should be valid unless the following happens:
25361dd8a8aSEmmanuel Grumbach 	 * The firmware asserts which triggers a reconfig flow, but
25461dd8a8aSEmmanuel Grumbach 	 * the reconfig fails before we set the pointer to sta into
25561dd8a8aSEmmanuel Grumbach 	 * the fw_id_to_mac_id pointer table. Mac80211 can't stop
25661dd8a8aSEmmanuel Grumbach 	 * A-MDPU and hence the timer continues to run. Then, the
25761dd8a8aSEmmanuel Grumbach 	 * timer expires and sta is NULL.
25861dd8a8aSEmmanuel Grumbach 	 */
25961dd8a8aSEmmanuel Grumbach 	if (!sta)
26061dd8a8aSEmmanuel Grumbach 		goto unlock;
26161dd8a8aSEmmanuel Grumbach 
26210b2b201SSara Sharon 	mvm_sta = iwl_mvm_sta_from_mac80211(sta);
26320fc690fSNaftali Goldstein 	ieee80211_rx_ba_timer_expired(mvm_sta->vif,
26410b2b201SSara Sharon 				      sta->addr, ba_data->tid);
26510b2b201SSara Sharon unlock:
26610b2b201SSara Sharon 	rcu_read_unlock();
26710b2b201SSara Sharon }
26810b2b201SSara Sharon 
2699794c64fSLiad Kaufman /* Disable aggregations for a bitmap of TIDs for a given station */
2709794c64fSLiad Kaufman static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
2719794c64fSLiad Kaufman 					unsigned long disable_agg_tids,
2729794c64fSLiad Kaufman 					bool remove_queue)
2739794c64fSLiad Kaufman {
2749794c64fSLiad Kaufman 	struct iwl_mvm_add_sta_cmd cmd = {};
2759794c64fSLiad Kaufman 	struct ieee80211_sta *sta;
2769794c64fSLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
2779794c64fSLiad Kaufman 	u32 status;
2789794c64fSLiad Kaufman 	u8 sta_id;
2799794c64fSLiad Kaufman 
280bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
281bb49701bSSara Sharon 		return -EINVAL;
282bb49701bSSara Sharon 
2839794c64fSLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
2849794c64fSLiad Kaufman 
2859794c64fSLiad Kaufman 	rcu_read_lock();
2869794c64fSLiad Kaufman 
2879794c64fSLiad Kaufman 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
2889794c64fSLiad Kaufman 
2899794c64fSLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
2909794c64fSLiad Kaufman 		rcu_read_unlock();
2919794c64fSLiad Kaufman 		return -EINVAL;
2929794c64fSLiad Kaufman 	}
2939794c64fSLiad Kaufman 
2949794c64fSLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
2959794c64fSLiad Kaufman 
2969794c64fSLiad Kaufman 	mvmsta->tid_disable_agg |= disable_agg_tids;
2979794c64fSLiad Kaufman 
2989794c64fSLiad Kaufman 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
2999794c64fSLiad Kaufman 	cmd.sta_id = mvmsta->sta_id;
3009794c64fSLiad Kaufman 	cmd.add_modify = STA_MODE_MODIFY;
3019794c64fSLiad Kaufman 	cmd.modify_mask = STA_MODIFY_QUEUES;
3029794c64fSLiad Kaufman 	if (disable_agg_tids)
3039794c64fSLiad Kaufman 		cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
3049794c64fSLiad Kaufman 	if (remove_queue)
3059794c64fSLiad Kaufman 		cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
3069794c64fSLiad Kaufman 	cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
3079794c64fSLiad Kaufman 	cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
3089794c64fSLiad Kaufman 
3099794c64fSLiad Kaufman 	rcu_read_unlock();
3109794c64fSLiad Kaufman 
3119794c64fSLiad Kaufman 	/* Notify FW of queue removal from the STA queues */
3129794c64fSLiad Kaufman 	status = ADD_STA_SUCCESS;
313b2c1bf59SSara Sharon 	return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
3149794c64fSLiad Kaufman 					   iwl_mvm_add_sta_cmd_size(mvm),
3159794c64fSLiad Kaufman 					   &cmd, &status);
3169794c64fSLiad Kaufman }
3179794c64fSLiad Kaufman 
318cfbc6c4cSSara Sharon static int iwl_mvm_disable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
319c5a976cfSJohannes Berg 			       int sta_id, u16 *queueptr, u8 tid)
32099448a8cSJohannes Berg {
321c6ce1c74SJohannes Berg 	int queue = *queueptr;
32299448a8cSJohannes Berg 	struct iwl_scd_txq_cfg_cmd cmd = {
32399448a8cSJohannes Berg 		.scd_queue = queue,
32499448a8cSJohannes Berg 		.action = SCD_CFG_DISABLE_QUEUE,
32599448a8cSJohannes Berg 	};
32699448a8cSJohannes Berg 	int ret;
32799448a8cSJohannes Berg 
328227f2597SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
329227f2597SJohannes Berg 
33099448a8cSJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm)) {
331227f2597SJohannes Berg 		if (mvm->sta_remove_requires_queue_remove) {
332227f2597SJohannes Berg 			u32 cmd_id = WIDE_ID(DATA_PATH_GROUP,
333227f2597SJohannes Berg 					     SCD_QUEUE_CONFIG_CMD);
334227f2597SJohannes Berg 			struct iwl_scd_queue_cfg_cmd remove_cmd = {
335227f2597SJohannes Berg 				.operation = cpu_to_le32(IWL_SCD_QUEUE_REMOVE),
336c5a976cfSJohannes Berg 				.u.remove.tid = cpu_to_le32(tid),
337c5a976cfSJohannes Berg 				.u.remove.sta_mask = cpu_to_le32(BIT(sta_id)),
338227f2597SJohannes Berg 			};
339227f2597SJohannes Berg 
340227f2597SJohannes Berg 			ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0,
341227f2597SJohannes Berg 						   sizeof(remove_cmd),
342227f2597SJohannes Berg 						   &remove_cmd);
343227f2597SJohannes Berg 		} else {
344227f2597SJohannes Berg 			ret = 0;
345227f2597SJohannes Berg 		}
346227f2597SJohannes Berg 
34799448a8cSJohannes Berg 		iwl_trans_txq_free(mvm->trans, queue);
348c6ce1c74SJohannes Berg 		*queueptr = IWL_MVM_INVALID_QUEUE;
34999448a8cSJohannes Berg 
350227f2597SJohannes Berg 		return ret;
35199448a8cSJohannes Berg 	}
35299448a8cSJohannes Berg 
353f3f240f9SJohannes Berg 	if (WARN_ON(mvm->queue_info[queue].tid_bitmap == 0))
35499448a8cSJohannes Berg 		return 0;
35599448a8cSJohannes Berg 
35699448a8cSJohannes Berg 	mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
35799448a8cSJohannes Berg 
3581c14089eSJohannes Berg 	cmd.action = mvm->queue_info[queue].tid_bitmap ?
35999448a8cSJohannes Berg 		SCD_CFG_ENABLE_QUEUE : SCD_CFG_DISABLE_QUEUE;
36099448a8cSJohannes Berg 	if (cmd.action == SCD_CFG_DISABLE_QUEUE)
36199448a8cSJohannes Berg 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_FREE;
36299448a8cSJohannes Berg 
36399448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
364cfbc6c4cSSara Sharon 			    "Disabling TXQ #%d tids=0x%x\n",
36599448a8cSJohannes Berg 			    queue,
366cfbc6c4cSSara Sharon 			    mvm->queue_info[queue].tid_bitmap);
36799448a8cSJohannes Berg 
36899448a8cSJohannes Berg 	/* If the queue is still enabled - nothing left to do in this func */
369f3f240f9SJohannes Berg 	if (cmd.action == SCD_CFG_ENABLE_QUEUE)
37099448a8cSJohannes Berg 		return 0;
37199448a8cSJohannes Berg 
37299448a8cSJohannes Berg 	cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
37399448a8cSJohannes Berg 	cmd.tid = mvm->queue_info[queue].txq_tid;
37499448a8cSJohannes Berg 
37599448a8cSJohannes Berg 	/* Make sure queue info is correct even though we overwrite it */
376cfbc6c4cSSara Sharon 	WARN(mvm->queue_info[queue].tid_bitmap,
377cfbc6c4cSSara Sharon 	     "TXQ #%d info out-of-sync - tids=0x%x\n",
378cfbc6c4cSSara Sharon 	     queue, mvm->queue_info[queue].tid_bitmap);
37999448a8cSJohannes Berg 
38099448a8cSJohannes Berg 	/* If we are here - the queue is freed and we can zero out these vals */
38199448a8cSJohannes Berg 	mvm->queue_info[queue].tid_bitmap = 0;
382cfbc6c4cSSara Sharon 
383cfbc6c4cSSara Sharon 	if (sta) {
384cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
385cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_tid(sta, tid);
386cfbc6c4cSSara Sharon 
387cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
38895b0f666SJose Ignacio Tornos Martinez 		list_del_init(&mvmtxq->list);
389cfbc6c4cSSara Sharon 	}
39099448a8cSJohannes Berg 
39199448a8cSJohannes Berg 	/* Regardless if this is a reserved TXQ for a STA - mark it as false */
39299448a8cSJohannes Berg 	mvm->queue_info[queue].reserved = false;
39399448a8cSJohannes Berg 
39499448a8cSJohannes Berg 	iwl_trans_txq_disable(mvm->trans, queue, false);
39564ff7eb0SJohannes Berg 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0,
39699448a8cSJohannes Berg 				   sizeof(struct iwl_scd_txq_cfg_cmd), &cmd);
39799448a8cSJohannes Berg 
39899448a8cSJohannes Berg 	if (ret)
39999448a8cSJohannes Berg 		IWL_ERR(mvm, "Failed to disable queue %d (ret=%d)\n",
40099448a8cSJohannes Berg 			queue, ret);
40199448a8cSJohannes Berg 	return ret;
40299448a8cSJohannes Berg }
40399448a8cSJohannes Berg 
40442db09c1SLiad Kaufman static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
40542db09c1SLiad Kaufman {
40642db09c1SLiad Kaufman 	struct ieee80211_sta *sta;
40742db09c1SLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
40842db09c1SLiad Kaufman 	unsigned long tid_bitmap;
40942db09c1SLiad Kaufman 	unsigned long agg_tids = 0;
410806911daSSharon Dvir 	u8 sta_id;
41142db09c1SLiad Kaufman 	int tid;
41242db09c1SLiad Kaufman 
41342db09c1SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
41442db09c1SLiad Kaufman 
415bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
416bb49701bSSara Sharon 		return -EINVAL;
417bb49701bSSara Sharon 
41842db09c1SLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
41942db09c1SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
42042db09c1SLiad Kaufman 
42142db09c1SLiad Kaufman 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
42242db09c1SLiad Kaufman 					lockdep_is_held(&mvm->mutex));
42342db09c1SLiad Kaufman 
42442db09c1SLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
42542db09c1SLiad Kaufman 		return -EINVAL;
42642db09c1SLiad Kaufman 
42742db09c1SLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
42842db09c1SLiad Kaufman 
42942db09c1SLiad Kaufman 	spin_lock_bh(&mvmsta->lock);
43042db09c1SLiad Kaufman 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
43142db09c1SLiad Kaufman 		if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
43242db09c1SLiad Kaufman 			agg_tids |= BIT(tid);
43342db09c1SLiad Kaufman 	}
43442db09c1SLiad Kaufman 	spin_unlock_bh(&mvmsta->lock);
43542db09c1SLiad Kaufman 
43642db09c1SLiad Kaufman 	return agg_tids;
43742db09c1SLiad Kaufman }
43842db09c1SLiad Kaufman 
4399794c64fSLiad Kaufman /*
4409794c64fSLiad Kaufman  * Remove a queue from a station's resources.
4419794c64fSLiad Kaufman  * Note that this only marks as free. It DOESN'T delete a BA agreement, and
4429794c64fSLiad Kaufman  * doesn't disable the queue
4439794c64fSLiad Kaufman  */
4449794c64fSLiad Kaufman static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
4459794c64fSLiad Kaufman {
4469794c64fSLiad Kaufman 	struct ieee80211_sta *sta;
4479794c64fSLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
4489794c64fSLiad Kaufman 	unsigned long tid_bitmap;
4499794c64fSLiad Kaufman 	unsigned long disable_agg_tids = 0;
4509794c64fSLiad Kaufman 	u8 sta_id;
4519794c64fSLiad Kaufman 	int tid;
4529794c64fSLiad Kaufman 
4539794c64fSLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
4549794c64fSLiad Kaufman 
455bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
456bb49701bSSara Sharon 		return -EINVAL;
457bb49701bSSara Sharon 
4589794c64fSLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
4599794c64fSLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
4609794c64fSLiad Kaufman 
4619794c64fSLiad Kaufman 	rcu_read_lock();
4629794c64fSLiad Kaufman 
4639794c64fSLiad Kaufman 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
4649794c64fSLiad Kaufman 
4659794c64fSLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
4669794c64fSLiad Kaufman 		rcu_read_unlock();
4679794c64fSLiad Kaufman 		return 0;
4689794c64fSLiad Kaufman 	}
4699794c64fSLiad Kaufman 
4709794c64fSLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
4719794c64fSLiad Kaufman 
4729794c64fSLiad Kaufman 	spin_lock_bh(&mvmsta->lock);
47342db09c1SLiad Kaufman 	/* Unmap MAC queues and TIDs from this queue */
4749794c64fSLiad Kaufman 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
475cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
476cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_tid(sta, tid);
477cfbc6c4cSSara Sharon 
4789794c64fSLiad Kaufman 		if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
4799794c64fSLiad Kaufman 			disable_agg_tids |= BIT(tid);
4806862fceeSSara Sharon 		mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
481cfbc6c4cSSara Sharon 
482cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
48395b0f666SJose Ignacio Tornos Martinez 		list_del_init(&mvmtxq->list);
4849794c64fSLiad Kaufman 	}
4859794c64fSLiad Kaufman 
48642db09c1SLiad Kaufman 	mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
4879794c64fSLiad Kaufman 	spin_unlock_bh(&mvmsta->lock);
4889794c64fSLiad Kaufman 
4899794c64fSLiad Kaufman 	rcu_read_unlock();
4909794c64fSLiad Kaufman 
49106bc6f6eSJohannes Berg 	/*
49206bc6f6eSJohannes Berg 	 * The TX path may have been using this TXQ_ID from the tid_data,
49306bc6f6eSJohannes Berg 	 * so make sure it's no longer running so that we can safely reuse
49406bc6f6eSJohannes Berg 	 * this TXQ later. We've set all the TIDs to IWL_MVM_INVALID_QUEUE
49506bc6f6eSJohannes Berg 	 * above, but nothing guarantees we've stopped using them. Thus,
49606bc6f6eSJohannes Berg 	 * without this, we could get to iwl_mvm_disable_txq() and remove
49706bc6f6eSJohannes Berg 	 * the queue while still sending frames to it.
49806bc6f6eSJohannes Berg 	 */
49906bc6f6eSJohannes Berg 	synchronize_net();
50006bc6f6eSJohannes Berg 
5019794c64fSLiad Kaufman 	return disable_agg_tids;
5029794c64fSLiad Kaufman }
5039794c64fSLiad Kaufman 
50401796ff2SSara Sharon static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
505cfbc6c4cSSara Sharon 				       struct ieee80211_sta *old_sta,
506724fe771SJohannes Berg 				       u8 new_sta_id)
50701796ff2SSara Sharon {
50801796ff2SSara Sharon 	struct iwl_mvm_sta *mvmsta;
509cfbc6c4cSSara Sharon 	u8 sta_id, tid;
51001796ff2SSara Sharon 	unsigned long disable_agg_tids = 0;
511724fe771SJohannes Berg 	bool same_sta;
512c6ce1c74SJohannes Berg 	u16 queue_tmp = queue;
51301796ff2SSara Sharon 	int ret;
51401796ff2SSara Sharon 
51501796ff2SSara Sharon 	lockdep_assert_held(&mvm->mutex);
51601796ff2SSara Sharon 
517bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
518bb49701bSSara Sharon 		return -EINVAL;
519bb49701bSSara Sharon 
52001796ff2SSara Sharon 	sta_id = mvm->queue_info[queue].ra_sta_id;
52101796ff2SSara Sharon 	tid = mvm->queue_info[queue].txq_tid;
52201796ff2SSara Sharon 
523724fe771SJohannes Berg 	same_sta = sta_id == new_sta_id;
524724fe771SJohannes Berg 
52501796ff2SSara Sharon 	mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
526e3df1e4bSSharon Dvir 	if (WARN_ON(!mvmsta))
527e3df1e4bSSharon Dvir 		return -EINVAL;
52801796ff2SSara Sharon 
52901796ff2SSara Sharon 	disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
53001796ff2SSara Sharon 	/* Disable the queue */
53101796ff2SSara Sharon 	if (disable_agg_tids)
53201796ff2SSara Sharon 		iwl_mvm_invalidate_sta_queue(mvm, queue,
53301796ff2SSara Sharon 					     disable_agg_tids, false);
53401796ff2SSara Sharon 
535c5a976cfSJohannes Berg 	ret = iwl_mvm_disable_txq(mvm, old_sta, sta_id, &queue_tmp, tid);
53601796ff2SSara Sharon 	if (ret) {
53701796ff2SSara Sharon 		IWL_ERR(mvm,
53801796ff2SSara Sharon 			"Failed to free inactive queue %d (ret=%d)\n",
53901796ff2SSara Sharon 			queue, ret);
54001796ff2SSara Sharon 
54101796ff2SSara Sharon 		return ret;
54201796ff2SSara Sharon 	}
54301796ff2SSara Sharon 
54401796ff2SSara Sharon 	/* If TXQ is allocated to another STA, update removal in FW */
54501796ff2SSara Sharon 	if (!same_sta)
54601796ff2SSara Sharon 		iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
54701796ff2SSara Sharon 
54801796ff2SSara Sharon 	return 0;
54901796ff2SSara Sharon }
55001796ff2SSara Sharon 
55142db09c1SLiad Kaufman static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
55242db09c1SLiad Kaufman 				    unsigned long tfd_queue_mask, u8 ac)
55342db09c1SLiad Kaufman {
55442db09c1SLiad Kaufman 	int queue = 0;
55542db09c1SLiad Kaufman 	u8 ac_to_queue[IEEE80211_NUM_ACS];
55642db09c1SLiad Kaufman 	int i;
55742db09c1SLiad Kaufman 
55890d2d94cSJohannes Berg 	/*
55990d2d94cSJohannes Berg 	 * This protects us against grabbing a queue that's being reconfigured
56090d2d94cSJohannes Berg 	 * by the inactivity checker.
56190d2d94cSJohannes Berg 	 */
56290d2d94cSJohannes Berg 	lockdep_assert_held(&mvm->mutex);
56390d2d94cSJohannes Berg 
564bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
565bb49701bSSara Sharon 		return -EINVAL;
56642db09c1SLiad Kaufman 
56742db09c1SLiad Kaufman 	memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
56842db09c1SLiad Kaufman 
56942db09c1SLiad Kaufman 	/* See what ACs the existing queues for this STA have */
57042db09c1SLiad Kaufman 	for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
57142db09c1SLiad Kaufman 		/* Only DATA queues can be shared */
57242db09c1SLiad Kaufman 		if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
57342db09c1SLiad Kaufman 		    i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
57442db09c1SLiad Kaufman 			continue;
57542db09c1SLiad Kaufman 
57642db09c1SLiad Kaufman 		ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
57742db09c1SLiad Kaufman 	}
57842db09c1SLiad Kaufman 
57942db09c1SLiad Kaufman 	/*
58042db09c1SLiad Kaufman 	 * The queue to share is chosen only from DATA queues as follows (in
58142db09c1SLiad Kaufman 	 * descending priority):
58242db09c1SLiad Kaufman 	 * 1. An AC_BE queue
58342db09c1SLiad Kaufman 	 * 2. Same AC queue
58442db09c1SLiad Kaufman 	 * 3. Highest AC queue that is lower than new AC
58542db09c1SLiad Kaufman 	 * 4. Any existing AC (there always is at least 1 DATA queue)
58642db09c1SLiad Kaufman 	 */
58742db09c1SLiad Kaufman 
58842db09c1SLiad Kaufman 	/* Priority 1: An AC_BE queue */
58942db09c1SLiad Kaufman 	if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
59042db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_BE];
59142db09c1SLiad Kaufman 	/* Priority 2: Same AC queue */
59242db09c1SLiad Kaufman 	else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
59342db09c1SLiad Kaufman 		queue = ac_to_queue[ac];
59442db09c1SLiad Kaufman 	/* Priority 3a: If new AC is VO and VI exists - use VI */
59542db09c1SLiad Kaufman 	else if (ac == IEEE80211_AC_VO &&
59642db09c1SLiad Kaufman 		 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
59742db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VI];
59842db09c1SLiad Kaufman 	/* Priority 3b: No BE so only AC less than the new one is BK */
59942db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
60042db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_BK];
60142db09c1SLiad Kaufman 	/* Priority 4a: No BE nor BK - use VI if exists */
60242db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
60342db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VI];
60442db09c1SLiad Kaufman 	/* Priority 4b: No BE, BK nor VI - use VO if exists */
60542db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
60642db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VO];
60742db09c1SLiad Kaufman 
60842db09c1SLiad Kaufman 	/* Make sure queue found (or not) is legal */
6099f9af3d7SLiad Kaufman 	if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
6109f9af3d7SLiad Kaufman 	    !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
6119f9af3d7SLiad Kaufman 	    (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
61242db09c1SLiad Kaufman 		IWL_ERR(mvm, "No DATA queues available to share\n");
6139f9af3d7SLiad Kaufman 		return -ENOSPC;
6149f9af3d7SLiad Kaufman 	}
6159f9af3d7SLiad Kaufman 
61642db09c1SLiad Kaufman 	return queue;
61742db09c1SLiad Kaufman }
61842db09c1SLiad Kaufman 
619a54844d4SJohannes Berg /* Re-configure the SCD for a queue that has already been configured */
620a54844d4SJohannes Berg static int iwl_mvm_reconfig_scd(struct iwl_mvm *mvm, int queue, int fifo,
621a54844d4SJohannes Berg 				int sta_id, int tid, int frame_limit, u16 ssn)
622a54844d4SJohannes Berg {
623a54844d4SJohannes Berg 	struct iwl_scd_txq_cfg_cmd cmd = {
624a54844d4SJohannes Berg 		.scd_queue = queue,
625a54844d4SJohannes Berg 		.action = SCD_CFG_ENABLE_QUEUE,
626a54844d4SJohannes Berg 		.window = frame_limit,
627a54844d4SJohannes Berg 		.sta_id = sta_id,
628a54844d4SJohannes Berg 		.ssn = cpu_to_le16(ssn),
629a54844d4SJohannes Berg 		.tx_fifo = fifo,
630a54844d4SJohannes Berg 		.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
631a54844d4SJohannes Berg 			      queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE),
632a54844d4SJohannes Berg 		.tid = tid,
633a54844d4SJohannes Berg 	};
634a54844d4SJohannes Berg 	int ret;
635a54844d4SJohannes Berg 
636a54844d4SJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
637a54844d4SJohannes Berg 		return -EINVAL;
638a54844d4SJohannes Berg 
639a54844d4SJohannes Berg 	if (WARN(mvm->queue_info[queue].tid_bitmap == 0,
640a54844d4SJohannes Berg 		 "Trying to reconfig unallocated queue %d\n", queue))
641a54844d4SJohannes Berg 		return -ENXIO;
642a54844d4SJohannes Berg 
643a54844d4SJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm, "Reconfig SCD for TXQ #%d\n", queue);
644a54844d4SJohannes Berg 
645a54844d4SJohannes Berg 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
646a54844d4SJohannes Berg 	WARN_ONCE(ret, "Failed to re-configure queue %d on FIFO %d, ret=%d\n",
647a54844d4SJohannes Berg 		  queue, fifo, ret);
648a54844d4SJohannes Berg 
649a54844d4SJohannes Berg 	return ret;
650a54844d4SJohannes Berg }
651a54844d4SJohannes Berg 
65258f2cc57SLiad Kaufman /*
6539f9af3d7SLiad Kaufman  * If a given queue has a higher AC than the TID stream that is being compared
6549f9af3d7SLiad Kaufman  * to, the queue needs to be redirected to the lower AC. This function does that
65558f2cc57SLiad Kaufman  * in such a case, otherwise - if no redirection required - it does nothing,
65658f2cc57SLiad Kaufman  * unless the %force param is true.
65758f2cc57SLiad Kaufman  */
658cfbc6c4cSSara Sharon static int iwl_mvm_redirect_queue(struct iwl_mvm *mvm, int queue, int tid,
65958f2cc57SLiad Kaufman 				  int ac, int ssn, unsigned int wdg_timeout,
660cfbc6c4cSSara Sharon 				  bool force, struct iwl_mvm_txq *txq)
66158f2cc57SLiad Kaufman {
66258f2cc57SLiad Kaufman 	struct iwl_scd_txq_cfg_cmd cmd = {
66358f2cc57SLiad Kaufman 		.scd_queue = queue,
664f7c692deSLiad Kaufman 		.action = SCD_CFG_DISABLE_QUEUE,
66558f2cc57SLiad Kaufman 	};
66658f2cc57SLiad Kaufman 	bool shared_queue;
66758f2cc57SLiad Kaufman 	int ret;
66858f2cc57SLiad Kaufman 
669bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
670bb49701bSSara Sharon 		return -EINVAL;
671bb49701bSSara Sharon 
67258f2cc57SLiad Kaufman 	/*
67358f2cc57SLiad Kaufman 	 * If the AC is lower than current one - FIFO needs to be redirected to
67458f2cc57SLiad Kaufman 	 * the lowest one of the streams in the queue. Check if this is needed
67558f2cc57SLiad Kaufman 	 * here.
67658f2cc57SLiad Kaufman 	 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
67758f2cc57SLiad Kaufman 	 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
67858f2cc57SLiad Kaufman 	 * we need to check if the numerical value of X is LARGER than of Y.
67958f2cc57SLiad Kaufman 	 */
68058f2cc57SLiad Kaufman 	if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
68158f2cc57SLiad Kaufman 		IWL_DEBUG_TX_QUEUES(mvm,
68258f2cc57SLiad Kaufman 				    "No redirection needed on TXQ #%d\n",
68358f2cc57SLiad Kaufman 				    queue);
68458f2cc57SLiad Kaufman 		return 0;
68558f2cc57SLiad Kaufman 	}
68658f2cc57SLiad Kaufman 
68758f2cc57SLiad Kaufman 	cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
68858f2cc57SLiad Kaufman 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
689edbe961cSLiad Kaufman 	cmd.tid = mvm->queue_info[queue].txq_tid;
6901c14089eSJohannes Berg 	shared_queue = hweight16(mvm->queue_info[queue].tid_bitmap) > 1;
69158f2cc57SLiad Kaufman 
6929f9af3d7SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
69358f2cc57SLiad Kaufman 			    queue, iwl_mvm_ac_to_tx_fifo[ac]);
69458f2cc57SLiad Kaufman 
695cfbc6c4cSSara Sharon 	/* Stop the queue and wait for it to empty */
696*b58e3d43SJohannes Berg 	set_bit(IWL_MVM_TXQ_STATE_STOP_REDIRECT, &txq->state);
697cfbc6c4cSSara Sharon 
698a1a57877SSara Sharon 	ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue));
69958f2cc57SLiad Kaufman 	if (ret) {
70058f2cc57SLiad Kaufman 		IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
70158f2cc57SLiad Kaufman 			queue);
70258f2cc57SLiad Kaufman 		ret = -EIO;
70358f2cc57SLiad Kaufman 		goto out;
70458f2cc57SLiad Kaufman 	}
70558f2cc57SLiad Kaufman 
70658f2cc57SLiad Kaufman 	/* Before redirecting the queue we need to de-activate it */
70758f2cc57SLiad Kaufman 	iwl_trans_txq_disable(mvm->trans, queue, false);
70858f2cc57SLiad Kaufman 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
70958f2cc57SLiad Kaufman 	if (ret)
71058f2cc57SLiad Kaufman 		IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
71158f2cc57SLiad Kaufman 			ret);
71258f2cc57SLiad Kaufman 
71358f2cc57SLiad Kaufman 	/* Make sure the SCD wrptr is correctly set before reconfiguring */
714ca3b9c6bSSara Sharon 	iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
71558f2cc57SLiad Kaufman 
716edbe961cSLiad Kaufman 	/* Update the TID "owner" of the queue */
717edbe961cSLiad Kaufman 	mvm->queue_info[queue].txq_tid = tid;
718edbe961cSLiad Kaufman 
71958f2cc57SLiad Kaufman 	/* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
72058f2cc57SLiad Kaufman 
72158f2cc57SLiad Kaufman 	/* Redirect to lower AC */
72258f2cc57SLiad Kaufman 	iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
7230ec9257bSSara Sharon 			     cmd.sta_id, tid, IWL_FRAME_LIMIT, ssn);
72458f2cc57SLiad Kaufman 
72558f2cc57SLiad Kaufman 	/* Update AC marking of the queue */
72658f2cc57SLiad Kaufman 	mvm->queue_info[queue].mac80211_ac = ac;
72758f2cc57SLiad Kaufman 
72858f2cc57SLiad Kaufman 	/*
72958f2cc57SLiad Kaufman 	 * Mark queue as shared in transport if shared
73058f2cc57SLiad Kaufman 	 * Note this has to be done after queue enablement because enablement
73158f2cc57SLiad Kaufman 	 * can also set this value, and there is no indication there to shared
73258f2cc57SLiad Kaufman 	 * queues
73358f2cc57SLiad Kaufman 	 */
73458f2cc57SLiad Kaufman 	if (shared_queue)
73558f2cc57SLiad Kaufman 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
73658f2cc57SLiad Kaufman 
73758f2cc57SLiad Kaufman out:
738cfbc6c4cSSara Sharon 	/* Continue using the queue */
739*b58e3d43SJohannes Berg 	clear_bit(IWL_MVM_TXQ_STATE_STOP_REDIRECT, &txq->state);
74058f2cc57SLiad Kaufman 
74158f2cc57SLiad Kaufman 	return ret;
74258f2cc57SLiad Kaufman }
74358f2cc57SLiad Kaufman 
74499448a8cSJohannes Berg static int iwl_mvm_find_free_queue(struct iwl_mvm *mvm, u8 sta_id,
74599448a8cSJohannes Berg 				   u8 minq, u8 maxq)
74699448a8cSJohannes Berg {
74799448a8cSJohannes Berg 	int i;
74899448a8cSJohannes Berg 
749f3f240f9SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
75099448a8cSJohannes Berg 
751e5b72e3bSJohannes Berg 	if (WARN(maxq >= mvm->trans->trans_cfg->base_params->num_of_queues,
752e5b72e3bSJohannes Berg 		 "max queue %d >= num_of_queues (%d)", maxq,
753e5b72e3bSJohannes Berg 		 mvm->trans->trans_cfg->base_params->num_of_queues))
754e5b72e3bSJohannes Berg 		maxq = mvm->trans->trans_cfg->base_params->num_of_queues - 1;
755e5b72e3bSJohannes Berg 
75699448a8cSJohannes Berg 	/* This should not be hit with new TX path */
75799448a8cSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
75899448a8cSJohannes Berg 		return -ENOSPC;
75999448a8cSJohannes Berg 
76099448a8cSJohannes Berg 	/* Start by looking for a free queue */
76199448a8cSJohannes Berg 	for (i = minq; i <= maxq; i++)
7621c14089eSJohannes Berg 		if (mvm->queue_info[i].tid_bitmap == 0 &&
76399448a8cSJohannes Berg 		    mvm->queue_info[i].status == IWL_MVM_QUEUE_FREE)
76499448a8cSJohannes Berg 			return i;
76599448a8cSJohannes Berg 
76699448a8cSJohannes Berg 	return -ENOSPC;
76799448a8cSJohannes Berg }
76899448a8cSJohannes Berg 
769cfbc6c4cSSara Sharon static int iwl_mvm_tvqm_enable_txq(struct iwl_mvm *mvm,
77099448a8cSJohannes Berg 				   u8 sta_id, u8 tid, unsigned int timeout)
77199448a8cSJohannes Berg {
772d5399f11SMordechay Goodstein 	int queue, size;
77399448a8cSJohannes Berg 
77499448a8cSJohannes Berg 	if (tid == IWL_MAX_TID_COUNT) {
77599448a8cSJohannes Berg 		tid = IWL_MGMT_TID;
776ff911dcaSShaul Triebitz 		size = max_t(u32, IWL_MGMT_QUEUE_SIZE,
777ff911dcaSShaul Triebitz 			     mvm->trans->cfg->min_txq_size);
778d5399f11SMordechay Goodstein 	} else {
779d5399f11SMordechay Goodstein 		struct ieee80211_sta *sta;
780d5399f11SMordechay Goodstein 
781d5399f11SMordechay Goodstein 		rcu_read_lock();
782d5399f11SMordechay Goodstein 		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
783d5399f11SMordechay Goodstein 
784d5399f11SMordechay Goodstein 		/* this queue isn't used for traffic (cab_queue) */
785d5399f11SMordechay Goodstein 		if (IS_ERR_OR_NULL(sta)) {
786d5399f11SMordechay Goodstein 			size = IWL_MGMT_QUEUE_SIZE;
787046d2e7cSSriram R 		} else if (sta->deflink.he_cap.has_he) {
788d5399f11SMordechay Goodstein 			/* support for 256 ba size */
789d5399f11SMordechay Goodstein 			size = IWL_DEFAULT_QUEUE_SIZE_HE;
790d5399f11SMordechay Goodstein 		} else {
791d5399f11SMordechay Goodstein 			size = IWL_DEFAULT_QUEUE_SIZE;
79299448a8cSJohannes Berg 		}
79399448a8cSJohannes Berg 
794d5399f11SMordechay Goodstein 		rcu_read_unlock();
795d5399f11SMordechay Goodstein 	}
796d5399f11SMordechay Goodstein 
797d5399f11SMordechay Goodstein 	/* take the min with bc tbl entries allowed */
798d5399f11SMordechay Goodstein 	size = min_t(u32, size, mvm->trans->txqs.bc_tbl_size / sizeof(u16));
799d5399f11SMordechay Goodstein 
800d5399f11SMordechay Goodstein 	/* size needs to be power of 2 values for calculating read/write pointers */
801d5399f11SMordechay Goodstein 	size = rounddown_pow_of_two(size);
802d5399f11SMordechay Goodstein 
80392f78d4bSJohannes Berg 	do {
804227f2597SJohannes Berg 		queue = iwl_trans_txq_alloc(mvm->trans, 0, BIT(sta_id),
80585b17a33SJohannes Berg 					    tid, size, timeout);
80692f78d4bSJohannes Berg 
80792f78d4bSJohannes Berg 		if (queue < 0)
80899448a8cSJohannes Berg 			IWL_DEBUG_TX_QUEUES(mvm,
80992f78d4bSJohannes Berg 					    "Failed allocating TXQ of size %d for sta %d tid %d, ret: %d\n",
81092f78d4bSJohannes Berg 					    size, sta_id, tid, queue);
81192f78d4bSJohannes Berg 		size /= 2;
81292f78d4bSJohannes Berg 	} while (queue < 0 && size >= 16);
81392f78d4bSJohannes Berg 
81492f78d4bSJohannes Berg 	if (queue < 0)
81599448a8cSJohannes Berg 		return queue;
81699448a8cSJohannes Berg 
81799448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d for sta %d tid %d\n",
81899448a8cSJohannes Berg 			    queue, sta_id, tid);
81999448a8cSJohannes Berg 
82099448a8cSJohannes Berg 	return queue;
82199448a8cSJohannes Berg }
82299448a8cSJohannes Berg 
823310181ecSSara Sharon static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
824310181ecSSara Sharon 					struct ieee80211_sta *sta, u8 ac,
825310181ecSSara Sharon 					int tid)
826310181ecSSara Sharon {
827310181ecSSara Sharon 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
828cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq =
829cfbc6c4cSSara Sharon 		iwl_mvm_txq_from_tid(sta, tid);
830310181ecSSara Sharon 	unsigned int wdg_timeout =
831310181ecSSara Sharon 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
832310181ecSSara Sharon 	int queue = -1;
833310181ecSSara Sharon 
834310181ecSSara Sharon 	lockdep_assert_held(&mvm->mutex);
835310181ecSSara Sharon 
836310181ecSSara Sharon 	IWL_DEBUG_TX_QUEUES(mvm,
837310181ecSSara Sharon 			    "Allocating queue for sta %d on tid %d\n",
838310181ecSSara Sharon 			    mvmsta->sta_id, tid);
839cfbc6c4cSSara Sharon 	queue = iwl_mvm_tvqm_enable_txq(mvm, mvmsta->sta_id, tid, wdg_timeout);
840310181ecSSara Sharon 	if (queue < 0)
841310181ecSSara Sharon 		return queue;
842310181ecSSara Sharon 
843cfbc6c4cSSara Sharon 	mvmtxq->txq_id = queue;
844cfbc6c4cSSara Sharon 	mvm->tvqm_info[queue].txq_tid = tid;
845cfbc6c4cSSara Sharon 	mvm->tvqm_info[queue].sta_id = mvmsta->sta_id;
846cfbc6c4cSSara Sharon 
847310181ecSSara Sharon 	IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
848310181ecSSara Sharon 
849310181ecSSara Sharon 	spin_lock_bh(&mvmsta->lock);
850310181ecSSara Sharon 	mvmsta->tid_data[tid].txq_id = queue;
851310181ecSSara Sharon 	spin_unlock_bh(&mvmsta->lock);
852310181ecSSara Sharon 
853310181ecSSara Sharon 	return 0;
854310181ecSSara Sharon }
855310181ecSSara Sharon 
856cfbc6c4cSSara Sharon static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm,
857cfbc6c4cSSara Sharon 				       struct ieee80211_sta *sta,
858cfbc6c4cSSara Sharon 				       int queue, u8 sta_id, u8 tid)
85999448a8cSJohannes Berg {
86099448a8cSJohannes Berg 	bool enable_queue = true;
86199448a8cSJohannes Berg 
86299448a8cSJohannes Berg 	/* Make sure this TID isn't already enabled */
86399448a8cSJohannes Berg 	if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) {
86499448a8cSJohannes Berg 		IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n",
86599448a8cSJohannes Berg 			queue, tid);
86699448a8cSJohannes Berg 		return false;
86799448a8cSJohannes Berg 	}
86899448a8cSJohannes Berg 
86999448a8cSJohannes Berg 	/* Update mappings and refcounts */
8701c14089eSJohannes Berg 	if (mvm->queue_info[queue].tid_bitmap)
87199448a8cSJohannes Berg 		enable_queue = false;
87299448a8cSJohannes Berg 
87399448a8cSJohannes Berg 	mvm->queue_info[queue].tid_bitmap |= BIT(tid);
87499448a8cSJohannes Berg 	mvm->queue_info[queue].ra_sta_id = sta_id;
87599448a8cSJohannes Berg 
87699448a8cSJohannes Berg 	if (enable_queue) {
87799448a8cSJohannes Berg 		if (tid != IWL_MAX_TID_COUNT)
87899448a8cSJohannes Berg 			mvm->queue_info[queue].mac80211_ac =
87999448a8cSJohannes Berg 				tid_to_mac80211_ac[tid];
88099448a8cSJohannes Berg 		else
88199448a8cSJohannes Berg 			mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO;
88299448a8cSJohannes Berg 
88399448a8cSJohannes Berg 		mvm->queue_info[queue].txq_tid = tid;
88499448a8cSJohannes Berg 	}
88599448a8cSJohannes Berg 
886cfbc6c4cSSara Sharon 	if (sta) {
887cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
888cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_tid(sta, tid);
889cfbc6c4cSSara Sharon 
890cfbc6c4cSSara Sharon 		mvmtxq->txq_id = queue;
891cfbc6c4cSSara Sharon 	}
892cfbc6c4cSSara Sharon 
89399448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
894cfbc6c4cSSara Sharon 			    "Enabling TXQ #%d tids=0x%x\n",
895cfbc6c4cSSara Sharon 			    queue, mvm->queue_info[queue].tid_bitmap);
89699448a8cSJohannes Berg 
89799448a8cSJohannes Berg 	return enable_queue;
89899448a8cSJohannes Berg }
89999448a8cSJohannes Berg 
900cfbc6c4cSSara Sharon static bool iwl_mvm_enable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
901cfbc6c4cSSara Sharon 			       int queue, u16 ssn,
90299448a8cSJohannes Berg 			       const struct iwl_trans_txq_scd_cfg *cfg,
90399448a8cSJohannes Berg 			       unsigned int wdg_timeout)
90499448a8cSJohannes Berg {
90599448a8cSJohannes Berg 	struct iwl_scd_txq_cfg_cmd cmd = {
90699448a8cSJohannes Berg 		.scd_queue = queue,
90799448a8cSJohannes Berg 		.action = SCD_CFG_ENABLE_QUEUE,
90899448a8cSJohannes Berg 		.window = cfg->frame_limit,
90999448a8cSJohannes Berg 		.sta_id = cfg->sta_id,
91099448a8cSJohannes Berg 		.ssn = cpu_to_le16(ssn),
91199448a8cSJohannes Berg 		.tx_fifo = cfg->fifo,
91299448a8cSJohannes Berg 		.aggregate = cfg->aggregate,
91399448a8cSJohannes Berg 		.tid = cfg->tid,
91499448a8cSJohannes Berg 	};
91599448a8cSJohannes Berg 	bool inc_ssn;
91699448a8cSJohannes Berg 
91799448a8cSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
91899448a8cSJohannes Berg 		return false;
91999448a8cSJohannes Berg 
92099448a8cSJohannes Berg 	/* Send the enabling command if we need to */
921cfbc6c4cSSara Sharon 	if (!iwl_mvm_update_txq_mapping(mvm, sta, queue, cfg->sta_id, cfg->tid))
92299448a8cSJohannes Berg 		return false;
92399448a8cSJohannes Berg 
92499448a8cSJohannes Berg 	inc_ssn = iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn,
92599448a8cSJohannes Berg 					   NULL, wdg_timeout);
92699448a8cSJohannes Berg 	if (inc_ssn)
92799448a8cSJohannes Berg 		le16_add_cpu(&cmd.ssn, 1);
92899448a8cSJohannes Berg 
92999448a8cSJohannes Berg 	WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd),
93099448a8cSJohannes Berg 	     "Failed to configure queue %d on FIFO %d\n", queue, cfg->fifo);
93199448a8cSJohannes Berg 
93299448a8cSJohannes Berg 	return inc_ssn;
93399448a8cSJohannes Berg }
93499448a8cSJohannes Berg 
935b3a87f11SJohannes Berg static void iwl_mvm_change_queue_tid(struct iwl_mvm *mvm, int queue)
93619aefa45SLiad Kaufman {
93719aefa45SLiad Kaufman 	struct iwl_scd_txq_cfg_cmd cmd = {
93819aefa45SLiad Kaufman 		.scd_queue = queue,
93919aefa45SLiad Kaufman 		.action = SCD_CFG_UPDATE_QUEUE_TID,
94019aefa45SLiad Kaufman 	};
94119aefa45SLiad Kaufman 	int tid;
94219aefa45SLiad Kaufman 	unsigned long tid_bitmap;
94319aefa45SLiad Kaufman 	int ret;
94419aefa45SLiad Kaufman 
94519aefa45SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
94619aefa45SLiad Kaufman 
947bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
948bb49701bSSara Sharon 		return;
949bb49701bSSara Sharon 
95019aefa45SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
95119aefa45SLiad Kaufman 
95219aefa45SLiad Kaufman 	if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
95319aefa45SLiad Kaufman 		return;
95419aefa45SLiad Kaufman 
95519aefa45SLiad Kaufman 	/* Find any TID for queue */
95619aefa45SLiad Kaufman 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
95719aefa45SLiad Kaufman 	cmd.tid = tid;
95819aefa45SLiad Kaufman 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
95919aefa45SLiad Kaufman 
96019aefa45SLiad Kaufman 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
961341ca402SLiad Kaufman 	if (ret) {
96219aefa45SLiad Kaufman 		IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
96319aefa45SLiad Kaufman 			queue, ret);
964341ca402SLiad Kaufman 		return;
965341ca402SLiad Kaufman 	}
966341ca402SLiad Kaufman 
967341ca402SLiad Kaufman 	mvm->queue_info[queue].txq_tid = tid;
96819aefa45SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
96919aefa45SLiad Kaufman 			    queue, tid);
97019aefa45SLiad Kaufman }
97119aefa45SLiad Kaufman 
9729f9af3d7SLiad Kaufman static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
9739f9af3d7SLiad Kaufman {
9749f9af3d7SLiad Kaufman 	struct ieee80211_sta *sta;
9759f9af3d7SLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
976806911daSSharon Dvir 	u8 sta_id;
9779f9af3d7SLiad Kaufman 	int tid = -1;
9789f9af3d7SLiad Kaufman 	unsigned long tid_bitmap;
9799f9af3d7SLiad Kaufman 	unsigned int wdg_timeout;
9809f9af3d7SLiad Kaufman 	int ssn;
9819f9af3d7SLiad Kaufman 	int ret = true;
9829f9af3d7SLiad Kaufman 
983bb49701bSSara Sharon 	/* queue sharing is disabled on new TX path */
984bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
985bb49701bSSara Sharon 		return;
986bb49701bSSara Sharon 
9879f9af3d7SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
9889f9af3d7SLiad Kaufman 
9899f9af3d7SLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
9909f9af3d7SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
9919f9af3d7SLiad Kaufman 
9929f9af3d7SLiad Kaufman 	/* Find TID for queue, and make sure it is the only one on the queue */
9939f9af3d7SLiad Kaufman 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
9949f9af3d7SLiad Kaufman 	if (tid_bitmap != BIT(tid)) {
9959f9af3d7SLiad Kaufman 		IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
9969f9af3d7SLiad Kaufman 			queue, tid_bitmap);
9979f9af3d7SLiad Kaufman 		return;
9989f9af3d7SLiad Kaufman 	}
9999f9af3d7SLiad Kaufman 
10009f9af3d7SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
10019f9af3d7SLiad Kaufman 			    tid);
10029f9af3d7SLiad Kaufman 
10039f9af3d7SLiad Kaufman 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
10049f9af3d7SLiad Kaufman 					lockdep_is_held(&mvm->mutex));
10059f9af3d7SLiad Kaufman 
10069f9af3d7SLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
10079f9af3d7SLiad Kaufman 		return;
10089f9af3d7SLiad Kaufman 
10099f9af3d7SLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
10109f9af3d7SLiad Kaufman 	wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
10119f9af3d7SLiad Kaufman 
10129f9af3d7SLiad Kaufman 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
10139f9af3d7SLiad Kaufman 
1014cfbc6c4cSSara Sharon 	ret = iwl_mvm_redirect_queue(mvm, queue, tid,
10159f9af3d7SLiad Kaufman 				     tid_to_mac80211_ac[tid], ssn,
1016cfbc6c4cSSara Sharon 				     wdg_timeout, true,
1017cfbc6c4cSSara Sharon 				     iwl_mvm_txq_from_tid(sta, tid));
10189f9af3d7SLiad Kaufman 	if (ret) {
10199f9af3d7SLiad Kaufman 		IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
10209f9af3d7SLiad Kaufman 		return;
10219f9af3d7SLiad Kaufman 	}
10229f9af3d7SLiad Kaufman 
10239f9af3d7SLiad Kaufman 	/* If aggs should be turned back on - do it */
10249f9af3d7SLiad Kaufman 	if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
10259cd70e80SEmmanuel Grumbach 		struct iwl_mvm_add_sta_cmd cmd = {0};
10269f9af3d7SLiad Kaufman 
10279f9af3d7SLiad Kaufman 		mvmsta->tid_disable_agg &= ~BIT(tid);
10289f9af3d7SLiad Kaufman 
10299f9af3d7SLiad Kaufman 		cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
10309f9af3d7SLiad Kaufman 		cmd.sta_id = mvmsta->sta_id;
10319f9af3d7SLiad Kaufman 		cmd.add_modify = STA_MODE_MODIFY;
10329f9af3d7SLiad Kaufman 		cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
10339f9af3d7SLiad Kaufman 		cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
10349f9af3d7SLiad Kaufman 		cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
10359f9af3d7SLiad Kaufman 
10369f9af3d7SLiad Kaufman 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
10379f9af3d7SLiad Kaufman 					   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
10389f9af3d7SLiad Kaufman 		if (!ret) {
10399f9af3d7SLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm,
10409f9af3d7SLiad Kaufman 					    "TXQ #%d is now aggregated again\n",
10419f9af3d7SLiad Kaufman 					    queue);
10429f9af3d7SLiad Kaufman 
10439f9af3d7SLiad Kaufman 			/* Mark queue intenally as aggregating again */
10449f9af3d7SLiad Kaufman 			iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
10459f9af3d7SLiad Kaufman 		}
10469f9af3d7SLiad Kaufman 	}
10479f9af3d7SLiad Kaufman 
10489f9af3d7SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
10499f9af3d7SLiad Kaufman }
10509f9af3d7SLiad Kaufman 
105199448a8cSJohannes Berg /*
105299448a8cSJohannes Berg  * Remove inactive TIDs of a given queue.
105399448a8cSJohannes Berg  * If all queue TIDs are inactive - mark the queue as inactive
105499448a8cSJohannes Berg  * If only some the queue TIDs are inactive - unmap them from the queue
1055724fe771SJohannes Berg  *
1056724fe771SJohannes Berg  * Returns %true if all TIDs were removed and the queue could be reused.
105799448a8cSJohannes Berg  */
1058724fe771SJohannes Berg static bool iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
105999448a8cSJohannes Berg 					 struct iwl_mvm_sta *mvmsta, int queue,
106090d2d94cSJohannes Berg 					 unsigned long tid_bitmap,
1061b3a87f11SJohannes Berg 					 unsigned long *unshare_queues,
1062b3a87f11SJohannes Berg 					 unsigned long *changetid_queues)
106399448a8cSJohannes Berg {
1064af3cdfd3SJakub Kicinski 	unsigned int tid;
106599448a8cSJohannes Berg 
106699448a8cSJohannes Berg 	lockdep_assert_held(&mvmsta->lock);
1067f3f240f9SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
106899448a8cSJohannes Berg 
106999448a8cSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1070724fe771SJohannes Berg 		return false;
107199448a8cSJohannes Berg 
107299448a8cSJohannes Berg 	/* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
107399448a8cSJohannes Berg 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
107499448a8cSJohannes Berg 		/* If some TFDs are still queued - don't mark TID as inactive */
107599448a8cSJohannes Berg 		if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid]))
107699448a8cSJohannes Berg 			tid_bitmap &= ~BIT(tid);
107799448a8cSJohannes Berg 
107899448a8cSJohannes Berg 		/* Don't mark as inactive any TID that has an active BA */
107999448a8cSJohannes Berg 		if (mvmsta->tid_data[tid].state != IWL_AGG_OFF)
108099448a8cSJohannes Berg 			tid_bitmap &= ~BIT(tid);
108199448a8cSJohannes Berg 	}
108299448a8cSJohannes Berg 
1083724fe771SJohannes Berg 	/* If all TIDs in the queue are inactive - return it can be reused */
108499448a8cSJohannes Berg 	if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
1085724fe771SJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Queue %d is inactive\n", queue);
1086724fe771SJohannes Berg 		return true;
108799448a8cSJohannes Berg 	}
108899448a8cSJohannes Berg 
108999448a8cSJohannes Berg 	/*
109099448a8cSJohannes Berg 	 * If we are here, this is a shared queue and not all TIDs timed-out.
109199448a8cSJohannes Berg 	 * Remove the ones that did.
109299448a8cSJohannes Berg 	 */
109399448a8cSJohannes Berg 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1094d4530f63SJohannes Berg 		u16 q_tid_bitmap;
109599448a8cSJohannes Berg 
109699448a8cSJohannes Berg 		mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
109799448a8cSJohannes Berg 		mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
109899448a8cSJohannes Berg 
1099d4530f63SJohannes Berg 		q_tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1100b3a87f11SJohannes Berg 
1101b3a87f11SJohannes Berg 		/*
1102b3a87f11SJohannes Berg 		 * We need to take into account a situation in which a TXQ was
1103b3a87f11SJohannes Berg 		 * allocated to TID x, and then turned shared by adding TIDs y
1104b3a87f11SJohannes Berg 		 * and z. If TID x becomes inactive and is removed from the TXQ,
1105b3a87f11SJohannes Berg 		 * ownership must be given to one of the remaining TIDs.
1106b3a87f11SJohannes Berg 		 * This is mainly because if TID x continues - a new queue can't
1107b3a87f11SJohannes Berg 		 * be allocated for it as long as it is an owner of another TXQ.
1108b3a87f11SJohannes Berg 		 *
1109b3a87f11SJohannes Berg 		 * Mark this queue in the right bitmap, we'll send the command
1110b3a87f11SJohannes Berg 		 * to the firmware later.
1111b3a87f11SJohannes Berg 		 */
1112d4530f63SJohannes Berg 		if (!(q_tid_bitmap & BIT(mvm->queue_info[queue].txq_tid)))
1113b3a87f11SJohannes Berg 			set_bit(queue, changetid_queues);
1114b3a87f11SJohannes Berg 
111599448a8cSJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm,
111699448a8cSJohannes Berg 				    "Removing inactive TID %d from shared Q:%d\n",
111799448a8cSJohannes Berg 				    tid, queue);
111899448a8cSJohannes Berg 	}
111999448a8cSJohannes Berg 
112099448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
112199448a8cSJohannes Berg 			    "TXQ #%d left with tid bitmap 0x%x\n", queue,
112299448a8cSJohannes Berg 			    mvm->queue_info[queue].tid_bitmap);
112399448a8cSJohannes Berg 
112499448a8cSJohannes Berg 	/*
112599448a8cSJohannes Berg 	 * There may be different TIDs with the same mac queues, so make
112699448a8cSJohannes Berg 	 * sure all TIDs have existing corresponding mac queues enabled
112799448a8cSJohannes Berg 	 */
112899448a8cSJohannes Berg 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
112999448a8cSJohannes Berg 
113099448a8cSJohannes Berg 	/* If the queue is marked as shared - "unshare" it */
11311c14089eSJohannes Berg 	if (hweight16(mvm->queue_info[queue].tid_bitmap) == 1 &&
113299448a8cSJohannes Berg 	    mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
113399448a8cSJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
113499448a8cSJohannes Berg 				    queue);
113590d2d94cSJohannes Berg 		set_bit(queue, unshare_queues);
113699448a8cSJohannes Berg 	}
1137724fe771SJohannes Berg 
1138724fe771SJohannes Berg 	return false;
113999448a8cSJohannes Berg }
114099448a8cSJohannes Berg 
1141724fe771SJohannes Berg /*
1142724fe771SJohannes Berg  * Check for inactivity - this includes checking if any queue
1143724fe771SJohannes Berg  * can be unshared and finding one (and only one) that can be
1144724fe771SJohannes Berg  * reused.
1145724fe771SJohannes Berg  * This function is also invoked as a sort of clean-up task,
1146724fe771SJohannes Berg  * in which case @alloc_for_sta is IWL_MVM_INVALID_STA.
1147724fe771SJohannes Berg  *
1148724fe771SJohannes Berg  * Returns the queue number, or -ENOSPC.
1149724fe771SJohannes Berg  */
1150724fe771SJohannes Berg static int iwl_mvm_inactivity_check(struct iwl_mvm *mvm, u8 alloc_for_sta)
115199448a8cSJohannes Berg {
115299448a8cSJohannes Berg 	unsigned long now = jiffies;
115390d2d94cSJohannes Berg 	unsigned long unshare_queues = 0;
1154b3a87f11SJohannes Berg 	unsigned long changetid_queues = 0;
1155724fe771SJohannes Berg 	int i, ret, free_queue = -ENOSPC;
1156cfbc6c4cSSara Sharon 	struct ieee80211_sta *queue_owner  = NULL;
115799448a8cSJohannes Berg 
1158df2a2245SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
1159df2a2245SJohannes Berg 
116099448a8cSJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm))
1161724fe771SJohannes Berg 		return -ENOSPC;
116299448a8cSJohannes Berg 
116399448a8cSJohannes Berg 	rcu_read_lock();
116499448a8cSJohannes Berg 
1165459ab045SJohannes Berg 	/* we skip the CMD queue below by starting at 1 */
1166459ab045SJohannes Berg 	BUILD_BUG_ON(IWL_MVM_DQA_CMD_QUEUE != 0);
1167459ab045SJohannes Berg 
1168459ab045SJohannes Berg 	for (i = 1; i < IWL_MAX_HW_QUEUES; i++) {
116999448a8cSJohannes Berg 		struct ieee80211_sta *sta;
117099448a8cSJohannes Berg 		struct iwl_mvm_sta *mvmsta;
117199448a8cSJohannes Berg 		u8 sta_id;
117299448a8cSJohannes Berg 		int tid;
117399448a8cSJohannes Berg 		unsigned long inactive_tid_bitmap = 0;
117499448a8cSJohannes Berg 		unsigned long queue_tid_bitmap;
117599448a8cSJohannes Berg 
117699448a8cSJohannes Berg 		queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
1177459ab045SJohannes Berg 		if (!queue_tid_bitmap)
1178459ab045SJohannes Berg 			continue;
117999448a8cSJohannes Berg 
118099448a8cSJohannes Berg 		/* If TXQ isn't in active use anyway - nothing to do here... */
118199448a8cSJohannes Berg 		if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
1182459ab045SJohannes Berg 		    mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED)
118399448a8cSJohannes Berg 			continue;
118499448a8cSJohannes Berg 
118599448a8cSJohannes Berg 		/* Check to see if there are inactive TIDs on this queue */
118699448a8cSJohannes Berg 		for_each_set_bit(tid, &queue_tid_bitmap,
118799448a8cSJohannes Berg 				 IWL_MAX_TID_COUNT + 1) {
118899448a8cSJohannes Berg 			if (time_after(mvm->queue_info[i].last_frame_time[tid] +
118999448a8cSJohannes Berg 				       IWL_MVM_DQA_QUEUE_TIMEOUT, now))
119099448a8cSJohannes Berg 				continue;
119199448a8cSJohannes Berg 
119299448a8cSJohannes Berg 			inactive_tid_bitmap |= BIT(tid);
119399448a8cSJohannes Berg 		}
119499448a8cSJohannes Berg 
119599448a8cSJohannes Berg 		/* If all TIDs are active - finish check on this queue */
119699448a8cSJohannes Berg 		if (!inactive_tid_bitmap)
119799448a8cSJohannes Berg 			continue;
119899448a8cSJohannes Berg 
119999448a8cSJohannes Berg 		/*
120099448a8cSJohannes Berg 		 * If we are here - the queue hadn't been served recently and is
120199448a8cSJohannes Berg 		 * in use
120299448a8cSJohannes Berg 		 */
120399448a8cSJohannes Berg 
120499448a8cSJohannes Berg 		sta_id = mvm->queue_info[i].ra_sta_id;
120599448a8cSJohannes Berg 		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
120699448a8cSJohannes Berg 
120799448a8cSJohannes Berg 		/*
120899448a8cSJohannes Berg 		 * If the STA doesn't exist anymore, it isn't an error. It could
120999448a8cSJohannes Berg 		 * be that it was removed since getting the queues, and in this
121099448a8cSJohannes Berg 		 * case it should've inactivated its queues anyway.
121199448a8cSJohannes Berg 		 */
121299448a8cSJohannes Berg 		if (IS_ERR_OR_NULL(sta))
121399448a8cSJohannes Berg 			continue;
121499448a8cSJohannes Berg 
121599448a8cSJohannes Berg 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
121699448a8cSJohannes Berg 
1217f3f240f9SJohannes Berg 		spin_lock_bh(&mvmsta->lock);
1218724fe771SJohannes Berg 		ret = iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
121990d2d94cSJohannes Berg 						   inactive_tid_bitmap,
1220b3a87f11SJohannes Berg 						   &unshare_queues,
1221b3a87f11SJohannes Berg 						   &changetid_queues);
1222e6d419f9SJohannes Berg 		if (ret && free_queue < 0) {
1223cfbc6c4cSSara Sharon 			queue_owner = sta;
1224e6d419f9SJohannes Berg 			free_queue = i;
1225cfbc6c4cSSara Sharon 		}
1226459ab045SJohannes Berg 		/* only unlock sta lock - we still need the queue info lock */
1227f3f240f9SJohannes Berg 		spin_unlock_bh(&mvmsta->lock);
122899448a8cSJohannes Berg 	}
122999448a8cSJohannes Berg 
1230df2a2245SJohannes Berg 
1231df2a2245SJohannes Berg 	/* Reconfigure queues requiring reconfiguation */
123290d2d94cSJohannes Berg 	for_each_set_bit(i, &unshare_queues, IWL_MAX_HW_QUEUES)
123390d2d94cSJohannes Berg 		iwl_mvm_unshare_queue(mvm, i);
1234b3a87f11SJohannes Berg 	for_each_set_bit(i, &changetid_queues, IWL_MAX_HW_QUEUES)
1235b3a87f11SJohannes Berg 		iwl_mvm_change_queue_tid(mvm, i);
1236724fe771SJohannes Berg 
1237fbb1461aSJohannes Berg 	rcu_read_unlock();
1238fbb1461aSJohannes Berg 
1239724fe771SJohannes Berg 	if (free_queue >= 0 && alloc_for_sta != IWL_MVM_INVALID_STA) {
1240cfbc6c4cSSara Sharon 		ret = iwl_mvm_free_inactive_queue(mvm, free_queue, queue_owner,
1241724fe771SJohannes Berg 						  alloc_for_sta);
1242fbb1461aSJohannes Berg 		if (ret)
1243724fe771SJohannes Berg 			return ret;
1244724fe771SJohannes Berg 	}
1245724fe771SJohannes Berg 
1246724fe771SJohannes Berg 	return free_queue;
124799448a8cSJohannes Berg }
124899448a8cSJohannes Berg 
1249c20e08b0SJohannes Berg static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
1250cfbc6c4cSSara Sharon 				   struct ieee80211_sta *sta, u8 ac, int tid)
1251c20e08b0SJohannes Berg {
1252c20e08b0SJohannes Berg 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1253c20e08b0SJohannes Berg 	struct iwl_trans_txq_scd_cfg cfg = {
1254c20e08b0SJohannes Berg 		.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
1255c20e08b0SJohannes Berg 		.sta_id = mvmsta->sta_id,
1256c20e08b0SJohannes Berg 		.tid = tid,
1257c20e08b0SJohannes Berg 		.frame_limit = IWL_FRAME_LIMIT,
1258c20e08b0SJohannes Berg 	};
1259c20e08b0SJohannes Berg 	unsigned int wdg_timeout =
1260c20e08b0SJohannes Berg 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1261c20e08b0SJohannes Berg 	int queue = -1;
1262c6ce1c74SJohannes Berg 	u16 queue_tmp;
1263c20e08b0SJohannes Berg 	unsigned long disable_agg_tids = 0;
1264c20e08b0SJohannes Berg 	enum iwl_mvm_agg_state queue_state;
1265c20e08b0SJohannes Berg 	bool shared_queue = false, inc_ssn;
1266c20e08b0SJohannes Berg 	int ssn;
1267c20e08b0SJohannes Berg 	unsigned long tfd_queue_mask;
1268c20e08b0SJohannes Berg 	int ret;
1269c20e08b0SJohannes Berg 
1270c20e08b0SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
1271c20e08b0SJohannes Berg 
1272c20e08b0SJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm))
1273c20e08b0SJohannes Berg 		return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
1274c20e08b0SJohannes Berg 
1275c20e08b0SJohannes Berg 	spin_lock_bh(&mvmsta->lock);
1276c20e08b0SJohannes Berg 	tfd_queue_mask = mvmsta->tfd_queue_msk;
127735739348SSara Sharon 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1278c20e08b0SJohannes Berg 	spin_unlock_bh(&mvmsta->lock);
1279c20e08b0SJohannes Berg 
1280cfbc6c4cSSara Sharon 	if (tid == IWL_MAX_TID_COUNT) {
1281c20e08b0SJohannes Berg 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1282c20e08b0SJohannes Berg 						IWL_MVM_DQA_MIN_MGMT_QUEUE,
1283c20e08b0SJohannes Berg 						IWL_MVM_DQA_MAX_MGMT_QUEUE);
1284c20e08b0SJohannes Berg 		if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
1285c20e08b0SJohannes Berg 			IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
1286c20e08b0SJohannes Berg 					    queue);
1287c20e08b0SJohannes Berg 
1288c20e08b0SJohannes Berg 		/* If no such queue is found, we'll use a DATA queue instead */
1289c20e08b0SJohannes Berg 	}
1290c20e08b0SJohannes Berg 
1291c20e08b0SJohannes Berg 	if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
1292c20e08b0SJohannes Berg 	    (mvm->queue_info[mvmsta->reserved_queue].status ==
1293724fe771SJohannes Berg 			IWL_MVM_QUEUE_RESERVED)) {
1294c20e08b0SJohannes Berg 		queue = mvmsta->reserved_queue;
1295c20e08b0SJohannes Berg 		mvm->queue_info[queue].reserved = true;
1296c20e08b0SJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
1297c20e08b0SJohannes Berg 	}
1298c20e08b0SJohannes Berg 
1299c20e08b0SJohannes Berg 	if (queue < 0)
1300c20e08b0SJohannes Berg 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1301c20e08b0SJohannes Berg 						IWL_MVM_DQA_MIN_DATA_QUEUE,
1302c20e08b0SJohannes Berg 						IWL_MVM_DQA_MAX_DATA_QUEUE);
1303724fe771SJohannes Berg 	if (queue < 0) {
1304724fe771SJohannes Berg 		/* try harder - perhaps kill an inactive queue */
1305724fe771SJohannes Berg 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1306c20e08b0SJohannes Berg 	}
1307c20e08b0SJohannes Berg 
1308c20e08b0SJohannes Berg 	/* No free queue - we'll have to share */
1309c20e08b0SJohannes Berg 	if (queue <= 0) {
1310c20e08b0SJohannes Berg 		queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
1311c20e08b0SJohannes Berg 		if (queue > 0) {
1312c20e08b0SJohannes Berg 			shared_queue = true;
1313c20e08b0SJohannes Berg 			mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
1314c20e08b0SJohannes Berg 		}
1315c20e08b0SJohannes Berg 	}
1316c20e08b0SJohannes Berg 
1317c20e08b0SJohannes Berg 	/*
1318c20e08b0SJohannes Berg 	 * Mark TXQ as ready, even though it hasn't been fully configured yet,
1319c20e08b0SJohannes Berg 	 * to make sure no one else takes it.
1320c20e08b0SJohannes Berg 	 * This will allow avoiding re-acquiring the lock at the end of the
1321c20e08b0SJohannes Berg 	 * configuration. On error we'll mark it back as free.
1322c20e08b0SJohannes Berg 	 */
1323c20e08b0SJohannes Berg 	if (queue > 0 && !shared_queue)
1324c20e08b0SJohannes Berg 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1325c20e08b0SJohannes Berg 
1326c20e08b0SJohannes Berg 	/* This shouldn't happen - out of queues */
1327c20e08b0SJohannes Berg 	if (WARN_ON(queue <= 0)) {
1328c20e08b0SJohannes Berg 		IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
1329c20e08b0SJohannes Berg 			tid, cfg.sta_id);
1330c20e08b0SJohannes Berg 		return queue;
1331c20e08b0SJohannes Berg 	}
1332c20e08b0SJohannes Berg 
1333c20e08b0SJohannes Berg 	/*
1334c20e08b0SJohannes Berg 	 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
1335c20e08b0SJohannes Berg 	 * but for configuring the SCD to send A-MPDUs we need to mark the queue
1336c20e08b0SJohannes Berg 	 * as aggregatable.
1337c20e08b0SJohannes Berg 	 * Mark all DATA queues as allowing to be aggregated at some point
1338c20e08b0SJohannes Berg 	 */
1339c20e08b0SJohannes Berg 	cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1340c20e08b0SJohannes Berg 			 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1341c20e08b0SJohannes Berg 
1342c20e08b0SJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
1343c20e08b0SJohannes Berg 			    "Allocating %squeue #%d to sta %d on tid %d\n",
1344c20e08b0SJohannes Berg 			    shared_queue ? "shared " : "", queue,
1345c20e08b0SJohannes Berg 			    mvmsta->sta_id, tid);
1346c20e08b0SJohannes Berg 
1347c20e08b0SJohannes Berg 	if (shared_queue) {
1348c20e08b0SJohannes Berg 		/* Disable any open aggs on this queue */
1349c20e08b0SJohannes Berg 		disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
1350c20e08b0SJohannes Berg 
1351c20e08b0SJohannes Berg 		if (disable_agg_tids) {
1352c20e08b0SJohannes Berg 			IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
1353c20e08b0SJohannes Berg 					    queue);
1354c20e08b0SJohannes Berg 			iwl_mvm_invalidate_sta_queue(mvm, queue,
1355c20e08b0SJohannes Berg 						     disable_agg_tids, false);
1356c20e08b0SJohannes Berg 		}
1357c20e08b0SJohannes Berg 	}
1358c20e08b0SJohannes Berg 
1359cfbc6c4cSSara Sharon 	inc_ssn = iwl_mvm_enable_txq(mvm, sta, queue, ssn, &cfg, wdg_timeout);
1360c20e08b0SJohannes Berg 
1361c20e08b0SJohannes Berg 	/*
1362c20e08b0SJohannes Berg 	 * Mark queue as shared in transport if shared
1363c20e08b0SJohannes Berg 	 * Note this has to be done after queue enablement because enablement
1364c20e08b0SJohannes Berg 	 * can also set this value, and there is no indication there to shared
1365c20e08b0SJohannes Berg 	 * queues
1366c20e08b0SJohannes Berg 	 */
1367c20e08b0SJohannes Berg 	if (shared_queue)
1368c20e08b0SJohannes Berg 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
1369c20e08b0SJohannes Berg 
1370c20e08b0SJohannes Berg 	spin_lock_bh(&mvmsta->lock);
1371c20e08b0SJohannes Berg 	/*
1372c20e08b0SJohannes Berg 	 * This looks racy, but it is not. We have only one packet for
1373c20e08b0SJohannes Berg 	 * this ra/tid in our Tx path since we stop the Qdisc when we
1374c20e08b0SJohannes Berg 	 * need to allocate a new TFD queue.
1375c20e08b0SJohannes Berg 	 */
137635739348SSara Sharon 	if (inc_ssn) {
1377c20e08b0SJohannes Berg 		mvmsta->tid_data[tid].seq_number += 0x10;
137835739348SSara Sharon 		ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
137935739348SSara Sharon 	}
1380c20e08b0SJohannes Berg 	mvmsta->tid_data[tid].txq_id = queue;
1381c20e08b0SJohannes Berg 	mvmsta->tfd_queue_msk |= BIT(queue);
1382c20e08b0SJohannes Berg 	queue_state = mvmsta->tid_data[tid].state;
1383c20e08b0SJohannes Berg 
1384c20e08b0SJohannes Berg 	if (mvmsta->reserved_queue == queue)
1385c20e08b0SJohannes Berg 		mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
1386c20e08b0SJohannes Berg 	spin_unlock_bh(&mvmsta->lock);
1387c20e08b0SJohannes Berg 
1388c20e08b0SJohannes Berg 	if (!shared_queue) {
1389c20e08b0SJohannes Berg 		ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
1390c20e08b0SJohannes Berg 		if (ret)
1391c20e08b0SJohannes Berg 			goto out_err;
1392c20e08b0SJohannes Berg 
1393c20e08b0SJohannes Berg 		/* If we need to re-enable aggregations... */
1394c20e08b0SJohannes Berg 		if (queue_state == IWL_AGG_ON) {
1395c20e08b0SJohannes Berg 			ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1396c20e08b0SJohannes Berg 			if (ret)
1397c20e08b0SJohannes Berg 				goto out_err;
1398c20e08b0SJohannes Berg 		}
1399c20e08b0SJohannes Berg 	} else {
1400c20e08b0SJohannes Berg 		/* Redirect queue, if needed */
1401cfbc6c4cSSara Sharon 		ret = iwl_mvm_redirect_queue(mvm, queue, tid, ac, ssn,
1402cfbc6c4cSSara Sharon 					     wdg_timeout, false,
1403cfbc6c4cSSara Sharon 					     iwl_mvm_txq_from_tid(sta, tid));
1404c20e08b0SJohannes Berg 		if (ret)
1405c20e08b0SJohannes Berg 			goto out_err;
1406c20e08b0SJohannes Berg 	}
1407c20e08b0SJohannes Berg 
1408c20e08b0SJohannes Berg 	return 0;
1409c20e08b0SJohannes Berg 
1410c20e08b0SJohannes Berg out_err:
1411c6ce1c74SJohannes Berg 	queue_tmp = queue;
1412c5a976cfSJohannes Berg 	iwl_mvm_disable_txq(mvm, sta, mvmsta->sta_id, &queue_tmp, tid);
1413c20e08b0SJohannes Berg 
1414c20e08b0SJohannes Berg 	return ret;
1415c20e08b0SJohannes Berg }
1416c20e08b0SJohannes Berg 
141724afba76SLiad Kaufman void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
141824afba76SLiad Kaufman {
141924afba76SLiad Kaufman 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
142024afba76SLiad Kaufman 					   add_stream_wk);
142124afba76SLiad Kaufman 
142224afba76SLiad Kaufman 	mutex_lock(&mvm->mutex);
142324afba76SLiad Kaufman 
1424724fe771SJohannes Berg 	iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
14259f9af3d7SLiad Kaufman 
1426cfbc6c4cSSara Sharon 	while (!list_empty(&mvm->add_stream_txqs)) {
1427cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq;
1428cfbc6c4cSSara Sharon 		struct ieee80211_txq *txq;
1429cfbc6c4cSSara Sharon 		u8 tid;
143024afba76SLiad Kaufman 
1431cfbc6c4cSSara Sharon 		mvmtxq = list_first_entry(&mvm->add_stream_txqs,
1432cfbc6c4cSSara Sharon 					  struct iwl_mvm_txq, list);
143324afba76SLiad Kaufman 
1434cfbc6c4cSSara Sharon 		txq = container_of((void *)mvmtxq, struct ieee80211_txq,
1435cfbc6c4cSSara Sharon 				   drv_priv);
1436cfbc6c4cSSara Sharon 		tid = txq->tid;
1437cfbc6c4cSSara Sharon 		if (tid == IEEE80211_NUM_TIDS)
1438cfbc6c4cSSara Sharon 			tid = IWL_MAX_TID_COUNT;
1439cfbc6c4cSSara Sharon 
14403d1d87abSJohannes Berg 		/*
14413d1d87abSJohannes Berg 		 * We can't really do much here, but if this fails we can't
14423d1d87abSJohannes Berg 		 * transmit anyway - so just don't transmit the frame etc.
14433d1d87abSJohannes Berg 		 * and let them back up ... we've tried our best to allocate
14443d1d87abSJohannes Berg 		 * a queue in the function itself.
14453d1d87abSJohannes Berg 		 */
14463d1d87abSJohannes Berg 		if (iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, tid)) {
14473d1d87abSJohannes Berg 			list_del_init(&mvmtxq->list);
14483d1d87abSJohannes Berg 			continue;
14493d1d87abSJohannes Berg 		}
14503d1d87abSJohannes Berg 
1451cfbc6c4cSSara Sharon 		list_del_init(&mvmtxq->list);
1452f5ae2f93SJohannes Berg 		local_bh_disable();
1453cfbc6c4cSSara Sharon 		iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1454f5ae2f93SJohannes Berg 		local_bh_enable();
145524afba76SLiad Kaufman 	}
145624afba76SLiad Kaufman 
145724afba76SLiad Kaufman 	mutex_unlock(&mvm->mutex);
145824afba76SLiad Kaufman }
145924afba76SLiad Kaufman 
146024afba76SLiad Kaufman static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1461d5216a28SLiad Kaufman 				      struct ieee80211_sta *sta,
1462d5216a28SLiad Kaufman 				      enum nl80211_iftype vif_type)
146324afba76SLiad Kaufman {
146424afba76SLiad Kaufman 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
146524afba76SLiad Kaufman 	int queue;
146624afba76SLiad Kaufman 
1467396952eeSSara Sharon 	/* queue reserving is disabled on new TX path */
1468396952eeSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1469396952eeSSara Sharon 		return 0;
1470396952eeSSara Sharon 
1471724fe771SJohannes Berg 	/* run the general cleanup/unsharing of queues */
1472724fe771SJohannes Berg 	iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
14739794c64fSLiad Kaufman 
147424afba76SLiad Kaufman 	/* Make sure we have free resources for this STA */
1475d5216a28SLiad Kaufman 	if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
14761c14089eSJohannes Berg 	    !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].tid_bitmap &&
1477cf961e16SLiad Kaufman 	    (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1478cf961e16SLiad Kaufman 	     IWL_MVM_QUEUE_FREE))
1479d5216a28SLiad Kaufman 		queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1480d5216a28SLiad Kaufman 	else
14819794c64fSLiad Kaufman 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
14829794c64fSLiad Kaufman 						IWL_MVM_DQA_MIN_DATA_QUEUE,
148324afba76SLiad Kaufman 						IWL_MVM_DQA_MAX_DATA_QUEUE);
148424afba76SLiad Kaufman 	if (queue < 0) {
1485724fe771SJohannes Berg 		/* try again - this time kick out a queue if needed */
1486724fe771SJohannes Berg 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1487724fe771SJohannes Berg 		if (queue < 0) {
148824afba76SLiad Kaufman 			IWL_ERR(mvm, "No available queues for new station\n");
148924afba76SLiad Kaufman 			return -ENOSPC;
1490724fe771SJohannes Berg 		}
149124afba76SLiad Kaufman 	}
1492cf961e16SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
149324afba76SLiad Kaufman 
149424afba76SLiad Kaufman 	mvmsta->reserved_queue = queue;
149524afba76SLiad Kaufman 
149624afba76SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
149724afba76SLiad Kaufman 			    queue, mvmsta->sta_id);
149824afba76SLiad Kaufman 
149924afba76SLiad Kaufman 	return 0;
150024afba76SLiad Kaufman }
150124afba76SLiad Kaufman 
15028d98ae6eSLiad Kaufman /*
15038d98ae6eSLiad Kaufman  * In DQA mode, after a HW restart the queues should be allocated as before, in
15048d98ae6eSLiad Kaufman  * order to avoid race conditions when there are shared queues. This function
15058d98ae6eSLiad Kaufman  * does the re-mapping and queue allocation.
15068d98ae6eSLiad Kaufman  *
15078d98ae6eSLiad Kaufman  * Note that re-enabling aggregations isn't done in this function.
15088d98ae6eSLiad Kaufman  */
15098d98ae6eSLiad Kaufman static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1510cfbc6c4cSSara Sharon 						 struct ieee80211_sta *sta)
15118d98ae6eSLiad Kaufman {
1512cfbc6c4cSSara Sharon 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1513cfbc6c4cSSara Sharon 	unsigned int wdg =
15148d98ae6eSLiad Kaufman 		iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
15158d98ae6eSLiad Kaufman 	int i;
15168d98ae6eSLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
15178d98ae6eSLiad Kaufman 		.sta_id = mvm_sta->sta_id,
15188d98ae6eSLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
15198d98ae6eSLiad Kaufman 	};
15208d98ae6eSLiad Kaufman 
152103c902bfSJohannes Berg 	/* Make sure reserved queue is still marked as such (if allocated) */
152203c902bfSJohannes Berg 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
15238d98ae6eSLiad Kaufman 		mvm->queue_info[mvm_sta->reserved_queue].status =
15248d98ae6eSLiad Kaufman 			IWL_MVM_QUEUE_RESERVED;
15258d98ae6eSLiad Kaufman 
15268d98ae6eSLiad Kaufman 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
15278d98ae6eSLiad Kaufman 		struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
15288d98ae6eSLiad Kaufman 		int txq_id = tid_data->txq_id;
15298d98ae6eSLiad Kaufman 		int ac;
15308d98ae6eSLiad Kaufman 
15316862fceeSSara Sharon 		if (txq_id == IWL_MVM_INVALID_QUEUE)
15328d98ae6eSLiad Kaufman 			continue;
15338d98ae6eSLiad Kaufman 
15348d98ae6eSLiad Kaufman 		ac = tid_to_mac80211_ac[i];
15358d98ae6eSLiad Kaufman 
1536310181ecSSara Sharon 		if (iwl_mvm_has_new_tx_api(mvm)) {
1537310181ecSSara Sharon 			IWL_DEBUG_TX_QUEUES(mvm,
1538310181ecSSara Sharon 					    "Re-mapping sta %d tid %d\n",
1539310181ecSSara Sharon 					    mvm_sta->sta_id, i);
1540cfbc6c4cSSara Sharon 			txq_id = iwl_mvm_tvqm_enable_txq(mvm, mvm_sta->sta_id,
1541cfbc6c4cSSara Sharon 							 i, wdg);
154291cf5dedSJohannes Berg 			/*
154391cf5dedSJohannes Berg 			 * on failures, just set it to IWL_MVM_INVALID_QUEUE
154491cf5dedSJohannes Berg 			 * to try again later, we have no other good way of
154591cf5dedSJohannes Berg 			 * failing here
154691cf5dedSJohannes Berg 			 */
154791cf5dedSJohannes Berg 			if (txq_id < 0)
154891cf5dedSJohannes Berg 				txq_id = IWL_MVM_INVALID_QUEUE;
1549310181ecSSara Sharon 			tid_data->txq_id = txq_id;
15505d39051aSLiad Kaufman 
15515d39051aSLiad Kaufman 			/*
15525d39051aSLiad Kaufman 			 * Since we don't set the seq number after reset, and HW
15535d39051aSLiad Kaufman 			 * sets it now, FW reset will cause the seq num to start
15545d39051aSLiad Kaufman 			 * at 0 again, so driver will need to update it
15555d39051aSLiad Kaufman 			 * internally as well, so it keeps in sync with real val
15565d39051aSLiad Kaufman 			 */
15575d39051aSLiad Kaufman 			tid_data->seq_number = 0;
1558310181ecSSara Sharon 		} else {
1559310181ecSSara Sharon 			u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1560310181ecSSara Sharon 
15618d98ae6eSLiad Kaufman 			cfg.tid = i;
1562cf6c6ea3SEmmanuel Grumbach 			cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
15638d98ae6eSLiad Kaufman 			cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1564310181ecSSara Sharon 					 txq_id ==
1565310181ecSSara Sharon 					 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
15668d98ae6eSLiad Kaufman 
15678d98ae6eSLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm,
15688d98ae6eSLiad Kaufman 					    "Re-mapping sta %d tid %d to queue %d\n",
15698d98ae6eSLiad Kaufman 					    mvm_sta->sta_id, i, txq_id);
15708d98ae6eSLiad Kaufman 
1571cfbc6c4cSSara Sharon 			iwl_mvm_enable_txq(mvm, sta, txq_id, seq, &cfg, wdg);
15728d98ae6eSLiad Kaufman 			mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
15738d98ae6eSLiad Kaufman 		}
157434e10860SSara Sharon 	}
15758d98ae6eSLiad Kaufman }
15768d98ae6eSLiad Kaufman 
1577732d06e9SShaul Triebitz static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1578732d06e9SShaul Triebitz 				      struct iwl_mvm_int_sta *sta,
1579732d06e9SShaul Triebitz 				      const u8 *addr,
1580732d06e9SShaul Triebitz 				      u16 mac_id, u16 color)
1581732d06e9SShaul Triebitz {
1582732d06e9SShaul Triebitz 	struct iwl_mvm_add_sta_cmd cmd;
1583732d06e9SShaul Triebitz 	int ret;
15843f497de9SLuca Coelho 	u32 status = ADD_STA_SUCCESS;
1585732d06e9SShaul Triebitz 
1586732d06e9SShaul Triebitz 	lockdep_assert_held(&mvm->mutex);
1587732d06e9SShaul Triebitz 
1588732d06e9SShaul Triebitz 	memset(&cmd, 0, sizeof(cmd));
1589732d06e9SShaul Triebitz 	cmd.sta_id = sta->sta_id;
15902c2c3647SNathan Errera 
1591971cbe50SJohannes Berg 	if (iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA, 0) >= 12 &&
15922c2c3647SNathan Errera 	    sta->type == IWL_STA_AUX_ACTIVITY)
15932c2c3647SNathan Errera 		cmd.mac_id_n_color = cpu_to_le32(mac_id);
15942c2c3647SNathan Errera 	else
1595732d06e9SShaul Triebitz 		cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1596732d06e9SShaul Triebitz 								     color));
15972c2c3647SNathan Errera 
1598732d06e9SShaul Triebitz 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1599732d06e9SShaul Triebitz 		cmd.station_type = sta->type;
1600732d06e9SShaul Triebitz 
1601732d06e9SShaul Triebitz 	if (!iwl_mvm_has_new_tx_api(mvm))
1602732d06e9SShaul Triebitz 		cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1603732d06e9SShaul Triebitz 	cmd.tid_disable_tx = cpu_to_le16(0xffff);
1604732d06e9SShaul Triebitz 
1605732d06e9SShaul Triebitz 	if (addr)
1606732d06e9SShaul Triebitz 		memcpy(cmd.addr, addr, ETH_ALEN);
1607732d06e9SShaul Triebitz 
1608732d06e9SShaul Triebitz 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1609732d06e9SShaul Triebitz 					  iwl_mvm_add_sta_cmd_size(mvm),
1610732d06e9SShaul Triebitz 					  &cmd, &status);
1611732d06e9SShaul Triebitz 	if (ret)
1612732d06e9SShaul Triebitz 		return ret;
1613732d06e9SShaul Triebitz 
1614732d06e9SShaul Triebitz 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1615732d06e9SShaul Triebitz 	case ADD_STA_SUCCESS:
1616732d06e9SShaul Triebitz 		IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1617732d06e9SShaul Triebitz 		return 0;
1618732d06e9SShaul Triebitz 	default:
1619732d06e9SShaul Triebitz 		ret = -EIO;
1620732d06e9SShaul Triebitz 		IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1621732d06e9SShaul Triebitz 			status);
1622732d06e9SShaul Triebitz 		break;
1623732d06e9SShaul Triebitz 	}
1624732d06e9SShaul Triebitz 	return ret;
1625732d06e9SShaul Triebitz }
1626732d06e9SShaul Triebitz 
1627e705c121SKalle Valo int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1628e705c121SKalle Valo 		    struct ieee80211_vif *vif,
1629e705c121SKalle Valo 		    struct ieee80211_sta *sta)
1630e705c121SKalle Valo {
1631e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1632e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1633a571f5f6SSara Sharon 	struct iwl_mvm_rxq_dup_data *dup_data;
1634e705c121SKalle Valo 	int i, ret, sta_id;
1635732d06e9SShaul Triebitz 	bool sta_update = false;
1636732d06e9SShaul Triebitz 	unsigned int sta_flags = 0;
1637e705c121SKalle Valo 
1638e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1639e705c121SKalle Valo 
1640e705c121SKalle Valo 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1641e705c121SKalle Valo 		sta_id = iwl_mvm_find_free_sta_id(mvm,
1642e705c121SKalle Valo 						  ieee80211_vif_type_p2p(vif));
1643e705c121SKalle Valo 	else
1644e705c121SKalle Valo 		sta_id = mvm_sta->sta_id;
1645e705c121SKalle Valo 
16460ae98812SSara Sharon 	if (sta_id == IWL_MVM_INVALID_STA)
1647e705c121SKalle Valo 		return -ENOSPC;
1648e705c121SKalle Valo 
1649e705c121SKalle Valo 	spin_lock_init(&mvm_sta->lock);
1650e705c121SKalle Valo 
1651c8f54701SJohannes Berg 	/* if this is a HW restart re-alloc existing queues */
1652c8f54701SJohannes Berg 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1653732d06e9SShaul Triebitz 		struct iwl_mvm_int_sta tmp_sta = {
1654732d06e9SShaul Triebitz 			.sta_id = sta_id,
1655732d06e9SShaul Triebitz 			.type = mvm_sta->sta_type,
1656732d06e9SShaul Triebitz 		};
1657732d06e9SShaul Triebitz 
1658732d06e9SShaul Triebitz 		/*
1659732d06e9SShaul Triebitz 		 * First add an empty station since allocating
1660732d06e9SShaul Triebitz 		 * a queue requires a valid station
1661732d06e9SShaul Triebitz 		 */
1662732d06e9SShaul Triebitz 		ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1663732d06e9SShaul Triebitz 						 mvmvif->id, mvmvif->color);
1664732d06e9SShaul Triebitz 		if (ret)
1665732d06e9SShaul Triebitz 			goto err;
1666732d06e9SShaul Triebitz 
1667cfbc6c4cSSara Sharon 		iwl_mvm_realloc_queues_after_restart(mvm, sta);
1668732d06e9SShaul Triebitz 		sta_update = true;
1669732d06e9SShaul Triebitz 		sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
16708d98ae6eSLiad Kaufman 		goto update_fw;
16718d98ae6eSLiad Kaufman 	}
16728d98ae6eSLiad Kaufman 
1673e705c121SKalle Valo 	mvm_sta->sta_id = sta_id;
1674e705c121SKalle Valo 	mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1675e705c121SKalle Valo 						      mvmvif->color);
1676e705c121SKalle Valo 	mvm_sta->vif = vif;
1677286ca8ebSLuca Coelho 	if (!mvm->trans->trans_cfg->gen2)
1678e705c121SKalle Valo 		mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1679a58bb468SLiad Kaufman 	else
1680a58bb468SLiad Kaufman 		mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
1681e705c121SKalle Valo 	mvm_sta->tx_protection = 0;
1682e705c121SKalle Valo 	mvm_sta->tt_tx_protection = false;
1683ced19f26SSara Sharon 	mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
1684e705c121SKalle Valo 
1685e705c121SKalle Valo 	/* HW restart, don't assume the memory has been zeroed */
1686e705c121SKalle Valo 	mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1687e705c121SKalle Valo 	mvm_sta->tfd_queue_msk = 0;
1688e705c121SKalle Valo 
1689e705c121SKalle Valo 	/* for HW restart - reset everything but the sequence number */
169024afba76SLiad Kaufman 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1691e705c121SKalle Valo 		u16 seq = mvm_sta->tid_data[i].seq_number;
1692e705c121SKalle Valo 		memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1693e705c121SKalle Valo 		mvm_sta->tid_data[i].seq_number = seq;
169424afba76SLiad Kaufman 
169524afba76SLiad Kaufman 		/*
169624afba76SLiad Kaufman 		 * Mark all queues for this STA as unallocated and defer TX
169724afba76SLiad Kaufman 		 * frames until the queue is allocated
169824afba76SLiad Kaufman 		 */
16996862fceeSSara Sharon 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1700e705c121SKalle Valo 	}
1701cfbc6c4cSSara Sharon 
1702cfbc6c4cSSara Sharon 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1703cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
1704cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1705cfbc6c4cSSara Sharon 
1706cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1707cfbc6c4cSSara Sharon 		INIT_LIST_HEAD(&mvmtxq->list);
1708fba8248eSSara Sharon 		atomic_set(&mvmtxq->tx_request, 0);
1709cfbc6c4cSSara Sharon 	}
1710cfbc6c4cSSara Sharon 
1711e705c121SKalle Valo 	mvm_sta->agg_tids = 0;
1712e705c121SKalle Valo 
1713a571f5f6SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) &&
1714a571f5f6SSara Sharon 	    !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
171592c4dca6SJohannes Berg 		int q;
171692c4dca6SJohannes Berg 
1717a571f5f6SSara Sharon 		dup_data = kcalloc(mvm->trans->num_rx_queues,
171892c4dca6SJohannes Berg 				   sizeof(*dup_data), GFP_KERNEL);
1719a571f5f6SSara Sharon 		if (!dup_data)
1720a571f5f6SSara Sharon 			return -ENOMEM;
172192c4dca6SJohannes Berg 		/*
172292c4dca6SJohannes Berg 		 * Initialize all the last_seq values to 0xffff which can never
172392c4dca6SJohannes Berg 		 * compare equal to the frame's seq_ctrl in the check in
172492c4dca6SJohannes Berg 		 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
172592c4dca6SJohannes Berg 		 * number and fragmented packets don't reach that function.
172692c4dca6SJohannes Berg 		 *
172792c4dca6SJohannes Berg 		 * This thus allows receiving a packet with seqno 0 and the
172892c4dca6SJohannes Berg 		 * retry bit set as the very first packet on a new TID.
172992c4dca6SJohannes Berg 		 */
173092c4dca6SJohannes Berg 		for (q = 0; q < mvm->trans->num_rx_queues; q++)
173192c4dca6SJohannes Berg 			memset(dup_data[q].last_seq, 0xff,
173292c4dca6SJohannes Berg 			       sizeof(dup_data[q].last_seq));
1733a571f5f6SSara Sharon 		mvm_sta->dup_data = dup_data;
1734a571f5f6SSara Sharon 	}
1735a571f5f6SSara Sharon 
1736c8f54701SJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm)) {
1737d5216a28SLiad Kaufman 		ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1738d5216a28SLiad Kaufman 						 ieee80211_vif_type_p2p(vif));
173924afba76SLiad Kaufman 		if (ret)
174024afba76SLiad Kaufman 			goto err;
174124afba76SLiad Kaufman 	}
174224afba76SLiad Kaufman 
17439f66a397SGregory Greenman 	/*
17449f66a397SGregory Greenman 	 * if rs is registered with mac80211, then "add station" will be handled
17459f66a397SGregory Greenman 	 * via the corresponding ops, otherwise need to notify rate scaling here
17469f66a397SGregory Greenman 	 */
17474243edb4SEmmanuel Grumbach 	if (iwl_mvm_has_tlc_offload(mvm))
17489f66a397SGregory Greenman 		iwl_mvm_rs_add_sta(mvm, mvm_sta);
17490f8084cdSMordechay Goodstein 	else
1750f5d88fa3SGregory Greenman 		spin_lock_init(&mvm_sta->lq_sta.rs_drv.pers.lock);
17519f66a397SGregory Greenman 
17520dde2440SAvraham Stern 	iwl_mvm_toggle_tx_ant(mvm, &mvm_sta->tx_ant);
17530dde2440SAvraham Stern 
17548d98ae6eSLiad Kaufman update_fw:
1755732d06e9SShaul Triebitz 	ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
1756e705c121SKalle Valo 	if (ret)
1757e705c121SKalle Valo 		goto err;
1758e705c121SKalle Valo 
1759e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION) {
1760e705c121SKalle Valo 		if (!sta->tdls) {
17610ae98812SSara Sharon 			WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
1762e705c121SKalle Valo 			mvmvif->ap_sta_id = sta_id;
1763e705c121SKalle Valo 		} else {
17640ae98812SSara Sharon 			WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
1765e705c121SKalle Valo 		}
1766e705c121SKalle Valo 	}
1767e705c121SKalle Valo 
1768e705c121SKalle Valo 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1769e705c121SKalle Valo 
1770e705c121SKalle Valo 	return 0;
1771e705c121SKalle Valo 
1772e705c121SKalle Valo err:
1773e705c121SKalle Valo 	return ret;
1774e705c121SKalle Valo }
1775e705c121SKalle Valo 
1776e705c121SKalle Valo int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1777e705c121SKalle Valo 		      bool drain)
1778e705c121SKalle Valo {
1779e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
1780e705c121SKalle Valo 	int ret;
1781e705c121SKalle Valo 	u32 status;
1782e705c121SKalle Valo 
1783e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1784e705c121SKalle Valo 
1785e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1786e705c121SKalle Valo 	cmd.sta_id = mvmsta->sta_id;
1787e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
1788e705c121SKalle Valo 	cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1789e705c121SKalle Valo 	cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1790e705c121SKalle Valo 
1791e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
1792854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1793854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
1794e705c121SKalle Valo 					  &cmd, &status);
1795e705c121SKalle Valo 	if (ret)
1796e705c121SKalle Valo 		return ret;
1797e705c121SKalle Valo 
1798837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1799e705c121SKalle Valo 	case ADD_STA_SUCCESS:
1800e705c121SKalle Valo 		IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1801e705c121SKalle Valo 			       mvmsta->sta_id);
1802e705c121SKalle Valo 		break;
1803e705c121SKalle Valo 	default:
1804e705c121SKalle Valo 		ret = -EIO;
1805e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1806e705c121SKalle Valo 			mvmsta->sta_id);
1807e705c121SKalle Valo 		break;
1808e705c121SKalle Valo 	}
1809e705c121SKalle Valo 
1810e705c121SKalle Valo 	return ret;
1811e705c121SKalle Valo }
1812e705c121SKalle Valo 
1813e705c121SKalle Valo /*
1814e705c121SKalle Valo  * Remove a station from the FW table. Before sending the command to remove
1815e705c121SKalle Valo  * the station validate that the station is indeed known to the driver (sanity
1816e705c121SKalle Valo  * only).
1817e705c121SKalle Valo  */
1818e705c121SKalle Valo static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1819e705c121SKalle Valo {
1820e705c121SKalle Valo 	struct ieee80211_sta *sta;
1821e705c121SKalle Valo 	struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1822e705c121SKalle Valo 		.sta_id = sta_id,
1823e705c121SKalle Valo 	};
1824e705c121SKalle Valo 	int ret;
1825e705c121SKalle Valo 
1826e705c121SKalle Valo 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1827e705c121SKalle Valo 					lockdep_is_held(&mvm->mutex));
1828e705c121SKalle Valo 
1829e705c121SKalle Valo 	/* Note: internal stations are marked as error values */
1830e705c121SKalle Valo 	if (!sta) {
1831e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid station id\n");
1832e705c121SKalle Valo 		return -EINVAL;
1833e705c121SKalle Valo 	}
1834e705c121SKalle Valo 
1835e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1836e705c121SKalle Valo 				   sizeof(rm_sta_cmd), &rm_sta_cmd);
1837e705c121SKalle Valo 	if (ret) {
1838e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1839e705c121SKalle Valo 		return ret;
1840e705c121SKalle Valo 	}
1841e705c121SKalle Valo 
1842e705c121SKalle Valo 	return 0;
1843e705c121SKalle Valo }
1844e705c121SKalle Valo 
184524afba76SLiad Kaufman static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
184624afba76SLiad Kaufman 				       struct ieee80211_vif *vif,
1847cfbc6c4cSSara Sharon 				       struct ieee80211_sta *sta)
184824afba76SLiad Kaufman {
1849cfbc6c4cSSara Sharon 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
185024afba76SLiad Kaufman 	int i;
185124afba76SLiad Kaufman 
185224afba76SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
185324afba76SLiad Kaufman 
185424afba76SLiad Kaufman 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
18556862fceeSSara Sharon 		if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
185624afba76SLiad Kaufman 			continue;
185724afba76SLiad Kaufman 
1858c5a976cfSJohannes Berg 		iwl_mvm_disable_txq(mvm, sta, mvm_sta->sta_id,
1859c5a976cfSJohannes Berg 				    &mvm_sta->tid_data[i].txq_id, i);
18606862fceeSSara Sharon 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
186124afba76SLiad Kaufman 	}
1862cfbc6c4cSSara Sharon 
1863cfbc6c4cSSara Sharon 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1864cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
1865cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1866cfbc6c4cSSara Sharon 
1867cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
186814a3aacfSJose Ignacio Tornos Martinez 		list_del_init(&mvmtxq->list);
1869cfbc6c4cSSara Sharon 	}
187024afba76SLiad Kaufman }
187124afba76SLiad Kaufman 
1872d6d517b7SSara Sharon int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1873d6d517b7SSara Sharon 				  struct iwl_mvm_sta *mvm_sta)
1874d6d517b7SSara Sharon {
1875bec9522aSSharon Dvir 	int i;
1876d6d517b7SSara Sharon 
1877d6d517b7SSara Sharon 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1878d6d517b7SSara Sharon 		u16 txq_id;
1879bec9522aSSharon Dvir 		int ret;
1880d6d517b7SSara Sharon 
1881d6d517b7SSara Sharon 		spin_lock_bh(&mvm_sta->lock);
1882d6d517b7SSara Sharon 		txq_id = mvm_sta->tid_data[i].txq_id;
1883d6d517b7SSara Sharon 		spin_unlock_bh(&mvm_sta->lock);
1884d6d517b7SSara Sharon 
1885d6d517b7SSara Sharon 		if (txq_id == IWL_MVM_INVALID_QUEUE)
1886d6d517b7SSara Sharon 			continue;
1887d6d517b7SSara Sharon 
1888d6d517b7SSara Sharon 		ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1889d6d517b7SSara Sharon 		if (ret)
1890bec9522aSSharon Dvir 			return ret;
1891d6d517b7SSara Sharon 	}
1892d6d517b7SSara Sharon 
1893bec9522aSSharon Dvir 	return 0;
1894d6d517b7SSara Sharon }
1895d6d517b7SSara Sharon 
1896e705c121SKalle Valo int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1897e705c121SKalle Valo 		   struct ieee80211_vif *vif,
1898e705c121SKalle Valo 		   struct ieee80211_sta *sta)
1899e705c121SKalle Valo {
1900e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1901e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
190294c3e614SSara Sharon 	u8 sta_id = mvm_sta->sta_id;
1903e705c121SKalle Valo 	int ret;
1904e705c121SKalle Valo 
1905e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1906e705c121SKalle Valo 
1907a571f5f6SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
1908a571f5f6SSara Sharon 		kfree(mvm_sta->dup_data);
1909a571f5f6SSara Sharon 
1910e705c121SKalle Valo 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1911e705c121SKalle Valo 	if (ret)
1912e705c121SKalle Valo 		return ret;
1913c8f54701SJohannes Berg 
1914e705c121SKalle Valo 	/* flush its queues here since we are freeing mvm_sta */
1915f9084775SNathan Errera 	ret = iwl_mvm_flush_sta(mvm, mvm_sta, false);
1916e705c121SKalle Valo 	if (ret)
1917e705c121SKalle Valo 		return ret;
1918d6d517b7SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
1919d6d517b7SSara Sharon 		ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1920d6d517b7SSara Sharon 	} else {
1921d6d517b7SSara Sharon 		u32 q_mask = mvm_sta->tfd_queue_msk;
1922d6d517b7SSara Sharon 
1923a1a57877SSara Sharon 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1924d6d517b7SSara Sharon 						     q_mask);
1925d6d517b7SSara Sharon 	}
1926e705c121SKalle Valo 	if (ret)
1927e705c121SKalle Valo 		return ret;
1928c8f54701SJohannes Berg 
1929e705c121SKalle Valo 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1930e705c121SKalle Valo 
1931cfbc6c4cSSara Sharon 	iwl_mvm_disable_sta_queues(mvm, vif, sta);
193256214749SLiad Kaufman 
193356214749SLiad Kaufman 	/* If there is a TXQ still marked as reserved - free it */
1934c8f54701SJohannes Berg 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1935a0315deaSLiad Kaufman 		u8 reserved_txq = mvm_sta->reserved_queue;
1936a0315deaSLiad Kaufman 		enum iwl_mvm_queue_status *status;
1937a0315deaSLiad Kaufman 
1938a0315deaSLiad Kaufman 		/*
1939a0315deaSLiad Kaufman 		 * If no traffic has gone through the reserved TXQ - it
1940a0315deaSLiad Kaufman 		 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1941a0315deaSLiad Kaufman 		 * should be manually marked as free again
1942a0315deaSLiad Kaufman 		 */
1943a0315deaSLiad Kaufman 		status = &mvm->queue_info[reserved_txq].status;
1944a0315deaSLiad Kaufman 		if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1945a0315deaSLiad Kaufman 			 (*status != IWL_MVM_QUEUE_FREE),
1946a0315deaSLiad Kaufman 			 "sta_id %d reserved txq %d status %d",
1947f3f240f9SJohannes Berg 			 sta_id, reserved_txq, *status))
1948a0315deaSLiad Kaufman 			return -EINVAL;
1949a0315deaSLiad Kaufman 
1950a0315deaSLiad Kaufman 		*status = IWL_MVM_QUEUE_FREE;
1951a0315deaSLiad Kaufman 	}
1952a0315deaSLiad Kaufman 
1953e3118ad7SLiad Kaufman 	if (vif->type == NL80211_IFTYPE_STATION &&
195494c3e614SSara Sharon 	    mvmvif->ap_sta_id == sta_id) {
1955e3118ad7SLiad Kaufman 		/* if associated - we can't remove the AP STA now */
1956f276e20bSJohannes Berg 		if (vif->cfg.assoc)
1957e705c121SKalle Valo 			return ret;
1958e705c121SKalle Valo 
19595c75a208SJohannes Berg 		/* first remove remaining keys */
19605c75a208SJohannes Berg 		iwl_mvm_sec_key_remove_ap(mvm, vif);
19615c75a208SJohannes Berg 
1962e705c121SKalle Valo 		/* unassoc - go ahead - remove the AP STA now */
19630ae98812SSara Sharon 		mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1964e705c121SKalle Valo 	}
1965e705c121SKalle Valo 
1966e705c121SKalle Valo 	/*
1967e705c121SKalle Valo 	 * This shouldn't happen - the TDLS channel switch should be canceled
1968e705c121SKalle Valo 	 * before the STA is removed.
1969e705c121SKalle Valo 	 */
197094c3e614SSara Sharon 	if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
19710ae98812SSara Sharon 		mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1972e705c121SKalle Valo 		cancel_delayed_work(&mvm->tdls_cs.dwork);
1973e705c121SKalle Valo 	}
1974e705c121SKalle Valo 
1975e705c121SKalle Valo 	/*
1976e705c121SKalle Valo 	 * Make sure that the tx response code sees the station as -EBUSY and
1977e705c121SKalle Valo 	 * calls the drain worker.
1978e705c121SKalle Valo 	 */
1979e705c121SKalle Valo 	spin_lock_bh(&mvm_sta->lock);
1980e705c121SKalle Valo 	spin_unlock_bh(&mvm_sta->lock);
1981e705c121SKalle Valo 
1982e705c121SKalle Valo 	ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1983e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1984e705c121SKalle Valo 
1985e705c121SKalle Valo 	return ret;
1986e705c121SKalle Valo }
1987e705c121SKalle Valo 
1988e705c121SKalle Valo int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1989e705c121SKalle Valo 		      struct ieee80211_vif *vif,
1990e705c121SKalle Valo 		      u8 sta_id)
1991e705c121SKalle Valo {
1992e705c121SKalle Valo 	int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1993e705c121SKalle Valo 
1994e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1995e705c121SKalle Valo 
1996e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1997e705c121SKalle Valo 	return ret;
1998e705c121SKalle Valo }
1999e705c121SKalle Valo 
20000e39eb03SChaya Rachel Ivgi int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
2001e705c121SKalle Valo 			     struct iwl_mvm_int_sta *sta,
2002ced19f26SSara Sharon 			     u32 qmask, enum nl80211_iftype iftype,
2003ced19f26SSara Sharon 			     enum iwl_sta_type type)
2004e705c121SKalle Valo {
2005df65c8d1SAvraham Stern 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
2006df65c8d1SAvraham Stern 	    sta->sta_id == IWL_MVM_INVALID_STA) {
2007e705c121SKalle Valo 		sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
20080ae98812SSara Sharon 		if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
2009e705c121SKalle Valo 			return -ENOSPC;
2010e705c121SKalle Valo 	}
2011e705c121SKalle Valo 
2012e705c121SKalle Valo 	sta->tfd_queue_msk = qmask;
2013ced19f26SSara Sharon 	sta->type = type;
2014e705c121SKalle Valo 
2015e705c121SKalle Valo 	/* put a non-NULL value so iterating over the stations won't stop */
2016e705c121SKalle Valo 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
2017e705c121SKalle Valo 	return 0;
2018e705c121SKalle Valo }
2019e705c121SKalle Valo 
202026d6c16bSSara Sharon void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
2021e705c121SKalle Valo {
2022e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
2023e705c121SKalle Valo 	memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
20240ae98812SSara Sharon 	sta->sta_id = IWL_MVM_INVALID_STA;
2025e705c121SKalle Valo }
2026e705c121SKalle Valo 
202791cf5dedSJohannes Berg static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 queue,
2028b13f43a4SEmmanuel Grumbach 					  u8 sta_id, u8 fifo)
2029e705c121SKalle Valo {
20309617040eSEmmanuel Grumbach 	unsigned int wdg_timeout =
20319617040eSEmmanuel Grumbach 		mvm->trans->trans_cfg->base_params->wd_timeout;
203228d0793eSLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
2033b13f43a4SEmmanuel Grumbach 		.fifo = fifo,
2034b13f43a4SEmmanuel Grumbach 		.sta_id = sta_id,
203528d0793eSLiad Kaufman 		.tid = IWL_MAX_TID_COUNT,
203628d0793eSLiad Kaufman 		.aggregate = false,
203728d0793eSLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
203828d0793eSLiad Kaufman 	};
203928d0793eSLiad Kaufman 
204091cf5dedSJohannes Berg 	WARN_ON(iwl_mvm_has_new_tx_api(mvm));
204191cf5dedSJohannes Berg 
204291cf5dedSJohannes Berg 	iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
204328d0793eSLiad Kaufman }
204491cf5dedSJohannes Berg 
204591cf5dedSJohannes Berg static int iwl_mvm_enable_aux_snif_queue_tvqm(struct iwl_mvm *mvm, u8 sta_id)
204691cf5dedSJohannes Berg {
20479617040eSEmmanuel Grumbach 	unsigned int wdg_timeout =
20489617040eSEmmanuel Grumbach 		mvm->trans->trans_cfg->base_params->wd_timeout;
204991cf5dedSJohannes Berg 
205091cf5dedSJohannes Berg 	WARN_ON(!iwl_mvm_has_new_tx_api(mvm));
205191cf5dedSJohannes Berg 
205291cf5dedSJohannes Berg 	return iwl_mvm_tvqm_enable_txq(mvm, sta_id, IWL_MAX_TID_COUNT,
205391cf5dedSJohannes Berg 				       wdg_timeout);
205491cf5dedSJohannes Berg }
205591cf5dedSJohannes Berg 
205691cf5dedSJohannes Berg static int iwl_mvm_add_int_sta_with_queue(struct iwl_mvm *mvm, int macidx,
2057be82ecd3SAvraham Stern 					  int maccolor, u8 *addr,
205891cf5dedSJohannes Berg 					  struct iwl_mvm_int_sta *sta,
205991cf5dedSJohannes Berg 					  u16 *queue, int fifo)
206091cf5dedSJohannes Berg {
206191cf5dedSJohannes Berg 	int ret;
206291cf5dedSJohannes Berg 
206391cf5dedSJohannes Berg 	/* Map queue to fifo - needs to happen before adding station */
206491cf5dedSJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm))
206591cf5dedSJohannes Berg 		iwl_mvm_enable_aux_snif_queue(mvm, *queue, sta->sta_id, fifo);
206691cf5dedSJohannes Berg 
2067be82ecd3SAvraham Stern 	ret = iwl_mvm_add_int_sta_common(mvm, sta, addr, macidx, maccolor);
206891cf5dedSJohannes Berg 	if (ret) {
206991cf5dedSJohannes Berg 		if (!iwl_mvm_has_new_tx_api(mvm))
2070c5a976cfSJohannes Berg 			iwl_mvm_disable_txq(mvm, NULL, sta->sta_id, queue,
207164ff7eb0SJohannes Berg 					    IWL_MAX_TID_COUNT);
207291cf5dedSJohannes Berg 		return ret;
207391cf5dedSJohannes Berg 	}
207491cf5dedSJohannes Berg 
207591cf5dedSJohannes Berg 	/*
207691cf5dedSJohannes Berg 	 * For 22000 firmware and on we cannot add queue to a station unknown
207791cf5dedSJohannes Berg 	 * to firmware so enable queue here - after the station was added
207891cf5dedSJohannes Berg 	 */
207991cf5dedSJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm)) {
208091cf5dedSJohannes Berg 		int txq;
208191cf5dedSJohannes Berg 
208291cf5dedSJohannes Berg 		txq = iwl_mvm_enable_aux_snif_queue_tvqm(mvm, sta->sta_id);
208391cf5dedSJohannes Berg 		if (txq < 0) {
208491cf5dedSJohannes Berg 			iwl_mvm_rm_sta_common(mvm, sta->sta_id);
208591cf5dedSJohannes Berg 			return txq;
208691cf5dedSJohannes Berg 		}
208791cf5dedSJohannes Berg 
208891cf5dedSJohannes Berg 		*queue = txq;
208991cf5dedSJohannes Berg 	}
209091cf5dedSJohannes Berg 
209191cf5dedSJohannes Berg 	return 0;
2092c5a719eeSSara Sharon }
2093c5a719eeSSara Sharon 
20942c2c3647SNathan Errera int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm, u32 lmac_id)
2095c5a719eeSSara Sharon {
2096c5a719eeSSara Sharon 	int ret;
2097c5a719eeSSara Sharon 
2098c5a719eeSSara Sharon 	lockdep_assert_held(&mvm->mutex);
2099c5a719eeSSara Sharon 
2100c5a719eeSSara Sharon 	/* Allocate aux station and assign to it the aux queue */
2101c5a719eeSSara Sharon 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
2102ced19f26SSara Sharon 				       NL80211_IFTYPE_UNSPECIFIED,
2103ced19f26SSara Sharon 				       IWL_STA_AUX_ACTIVITY);
2104c5a719eeSSara Sharon 	if (ret)
2105c5a719eeSSara Sharon 		return ret;
2106c5a719eeSSara Sharon 
21072c2c3647SNathan Errera 	/*
21082c2c3647SNathan Errera 	 * In CDB NICs we need to specify which lmac to use for aux activity
21092c2c3647SNathan Errera 	 * using the mac_id argument place to send lmac_id to the function
21102c2c3647SNathan Errera 	 */
21112c2c3647SNathan Errera 	ret = iwl_mvm_add_int_sta_with_queue(mvm, lmac_id, 0, NULL,
211291cf5dedSJohannes Berg 					     &mvm->aux_sta, &mvm->aux_queue,
2113b13f43a4SEmmanuel Grumbach 					     IWL_MVM_TX_FIFO_MCAST);
2114c5a719eeSSara Sharon 	if (ret) {
2115e705c121SKalle Valo 		iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2116e705c121SKalle Valo 		return ret;
2117e705c121SKalle Valo 	}
2118e705c121SKalle Valo 
2119c5a719eeSSara Sharon 	return 0;
2120c5a719eeSSara Sharon }
2121c5a719eeSSara Sharon 
21220e39eb03SChaya Rachel Ivgi int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
21230e39eb03SChaya Rachel Ivgi {
21240e39eb03SChaya Rachel Ivgi 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
21250e39eb03SChaya Rachel Ivgi 
21260e39eb03SChaya Rachel Ivgi 	lockdep_assert_held(&mvm->mutex);
2127b13f43a4SEmmanuel Grumbach 
212891cf5dedSJohannes Berg 	return iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
2129be82ecd3SAvraham Stern 					      NULL, &mvm->snif_sta,
2130be82ecd3SAvraham Stern 					      &mvm->snif_queue,
2131b13f43a4SEmmanuel Grumbach 					      IWL_MVM_TX_FIFO_BE);
21320e39eb03SChaya Rachel Ivgi }
21330e39eb03SChaya Rachel Ivgi 
21340e39eb03SChaya Rachel Ivgi int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
21350e39eb03SChaya Rachel Ivgi {
21360e39eb03SChaya Rachel Ivgi 	int ret;
21370e39eb03SChaya Rachel Ivgi 
21380e39eb03SChaya Rachel Ivgi 	lockdep_assert_held(&mvm->mutex);
21390e39eb03SChaya Rachel Ivgi 
2140e223e42aSGregory Greenman 	if (WARN_ON_ONCE(mvm->snif_sta.sta_id == IWL_MVM_INVALID_STA))
2141e223e42aSGregory Greenman 		return -EINVAL;
2142e223e42aSGregory Greenman 
2143c5a976cfSJohannes Berg 	iwl_mvm_disable_txq(mvm, NULL, mvm->snif_sta.sta_id,
2144c5a976cfSJohannes Berg 			    &mvm->snif_queue, IWL_MAX_TID_COUNT);
21450e39eb03SChaya Rachel Ivgi 	ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
21460e39eb03SChaya Rachel Ivgi 	if (ret)
21470e39eb03SChaya Rachel Ivgi 		IWL_WARN(mvm, "Failed sending remove station\n");
21480e39eb03SChaya Rachel Ivgi 
21490e39eb03SChaya Rachel Ivgi 	return ret;
21500e39eb03SChaya Rachel Ivgi }
21510e39eb03SChaya Rachel Ivgi 
2152f327236dSSharon int iwl_mvm_rm_aux_sta(struct iwl_mvm *mvm)
2153f327236dSSharon {
2154f327236dSSharon 	int ret;
2155f327236dSSharon 
2156f327236dSSharon 	lockdep_assert_held(&mvm->mutex);
2157f327236dSSharon 
2158e223e42aSGregory Greenman 	if (WARN_ON_ONCE(mvm->aux_sta.sta_id == IWL_MVM_INVALID_STA))
2159e223e42aSGregory Greenman 		return -EINVAL;
2160e223e42aSGregory Greenman 
2161c5a976cfSJohannes Berg 	iwl_mvm_disable_txq(mvm, NULL, mvm->aux_sta.sta_id,
2162c5a976cfSJohannes Berg 			    &mvm->aux_queue, IWL_MAX_TID_COUNT);
2163f327236dSSharon 	ret = iwl_mvm_rm_sta_common(mvm, mvm->aux_sta.sta_id);
2164f327236dSSharon 	if (ret)
2165f327236dSSharon 		IWL_WARN(mvm, "Failed sending remove station\n");
2166f327236dSSharon 	iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2167f327236dSSharon 
2168f327236dSSharon 	return ret;
2169f327236dSSharon }
2170f327236dSSharon 
21710e39eb03SChaya Rachel Ivgi void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
21720e39eb03SChaya Rachel Ivgi {
21730e39eb03SChaya Rachel Ivgi 	iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
21740e39eb03SChaya Rachel Ivgi }
21750e39eb03SChaya Rachel Ivgi 
2176e705c121SKalle Valo /*
2177e705c121SKalle Valo  * Send the add station command for the vif's broadcast station.
2178e705c121SKalle Valo  * Assumes that the station was already allocated.
2179e705c121SKalle Valo  *
2180e705c121SKalle Valo  * @mvm: the mvm component
2181e705c121SKalle Valo  * @vif: the interface to which the broadcast station is added
2182e705c121SKalle Valo  * @bsta: the broadcast station to add.
2183e705c121SKalle Valo  */
2184e705c121SKalle Valo int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2185e705c121SKalle Valo {
2186e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2187e705c121SKalle Valo 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2188e705c121SKalle Valo 	static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2189e705c121SKalle Valo 	const u8 *baddr = _baddr;
21907daa7624SJohannes Berg 	int queue;
2191df88c08dSLiad Kaufman 	int ret;
2192c5a719eeSSara Sharon 	unsigned int wdg_timeout =
2193c5a719eeSSara Sharon 		iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2194de24f638SLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
2195de24f638SLiad Kaufman 		.fifo = IWL_MVM_TX_FIFO_VO,
2196de24f638SLiad Kaufman 		.sta_id = mvmvif->bcast_sta.sta_id,
2197de24f638SLiad Kaufman 		.tid = IWL_MAX_TID_COUNT,
2198de24f638SLiad Kaufman 		.aggregate = false,
2199de24f638SLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
2200de24f638SLiad Kaufman 	};
2201de24f638SLiad Kaufman 
2202c5a719eeSSara Sharon 	lockdep_assert_held(&mvm->mutex);
2203c5a719eeSSara Sharon 
2204c8f54701SJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm)) {
22054d339989SLiad Kaufman 		if (vif->type == NL80211_IFTYPE_AP ||
2206f8510d67SNathan Chancellor 		    vif->type == NL80211_IFTYPE_ADHOC) {
220749f71713SSara Sharon 			queue = mvm->probe_queue;
2208f8510d67SNathan Chancellor 		} else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
220949f71713SSara Sharon 			queue = mvm->p2p_dev_queue;
2210f8510d67SNathan Chancellor 		} else {
2211f8510d67SNathan Chancellor 			WARN(1, "Missing required TXQ for adding bcast STA\n");
2212de24f638SLiad Kaufman 			return -EINVAL;
2213f8510d67SNathan Chancellor 		}
2214de24f638SLiad Kaufman 
2215df88c08dSLiad Kaufman 		bsta->tfd_queue_msk |= BIT(queue);
2216c5a719eeSSara Sharon 
2217cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2218de24f638SLiad Kaufman 	}
2219de24f638SLiad Kaufman 
2220e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_ADHOC)
2221e705c121SKalle Valo 		baddr = vif->bss_conf.bssid;
2222e705c121SKalle Valo 
22230ae98812SSara Sharon 	if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
2224e705c121SKalle Valo 		return -ENOSPC;
2225e705c121SKalle Valo 
2226df88c08dSLiad Kaufman 	ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2227e705c121SKalle Valo 					 mvmvif->id, mvmvif->color);
2228df88c08dSLiad Kaufman 	if (ret)
2229df88c08dSLiad Kaufman 		return ret;
2230df88c08dSLiad Kaufman 
2231df88c08dSLiad Kaufman 	/*
22322f7a3863SLuca Coelho 	 * For 22000 firmware and on we cannot add queue to a station unknown
2233c5a719eeSSara Sharon 	 * to firmware so enable queue here - after the station was added
2234df88c08dSLiad Kaufman 	 */
2235310181ecSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
2236cfbc6c4cSSara Sharon 		queue = iwl_mvm_tvqm_enable_txq(mvm, bsta->sta_id,
2237310181ecSSara Sharon 						IWL_MAX_TID_COUNT,
2238c5a719eeSSara Sharon 						wdg_timeout);
223991cf5dedSJohannes Berg 		if (queue < 0) {
224091cf5dedSJohannes Berg 			iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
224191cf5dedSJohannes Berg 			return queue;
224291cf5dedSJohannes Berg 		}
22437daa7624SJohannes Berg 
22447b758a11SLuca Coelho 		if (vif->type == NL80211_IFTYPE_AP ||
22457b758a11SLuca Coelho 		    vif->type == NL80211_IFTYPE_ADHOC)
2246310181ecSSara Sharon 			mvm->probe_queue = queue;
2247310181ecSSara Sharon 		else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
2248310181ecSSara Sharon 			mvm->p2p_dev_queue = queue;
2249310181ecSSara Sharon 	}
2250df88c08dSLiad Kaufman 
2251df88c08dSLiad Kaufman 	return 0;
2252df88c08dSLiad Kaufman }
2253df88c08dSLiad Kaufman 
2254df88c08dSLiad Kaufman static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2255df88c08dSLiad Kaufman 					  struct ieee80211_vif *vif)
2256df88c08dSLiad Kaufman {
2257df88c08dSLiad Kaufman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2258c6ce1c74SJohannes Berg 	u16 *queueptr, queue;
2259df88c08dSLiad Kaufman 
2260df88c08dSLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
2261df88c08dSLiad Kaufman 
2262f9084775SNathan Errera 	iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true);
2263d49394a1SSara Sharon 
2264d167e81aSMordechai Goodstein 	switch (vif->type) {
2265d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_AP:
2266d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_ADHOC:
2267c6ce1c74SJohannes Berg 		queueptr = &mvm->probe_queue;
2268d167e81aSMordechai Goodstein 		break;
2269d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_P2P_DEVICE:
2270c6ce1c74SJohannes Berg 		queueptr = &mvm->p2p_dev_queue;
2271d167e81aSMordechai Goodstein 		break;
2272d167e81aSMordechai Goodstein 	default:
2273d167e81aSMordechai Goodstein 		WARN(1, "Can't free bcast queue on vif type %d\n",
2274d167e81aSMordechai Goodstein 		     vif->type);
2275d167e81aSMordechai Goodstein 		return;
2276df88c08dSLiad Kaufman 	}
2277df88c08dSLiad Kaufman 
2278c6ce1c74SJohannes Berg 	queue = *queueptr;
2279c5a976cfSJohannes Berg 	iwl_mvm_disable_txq(mvm, NULL, mvmvif->bcast_sta.sta_id,
2280c5a976cfSJohannes Berg 			    queueptr, IWL_MAX_TID_COUNT);
2281d167e81aSMordechai Goodstein 	if (iwl_mvm_has_new_tx_api(mvm))
2282d167e81aSMordechai Goodstein 		return;
2283d167e81aSMordechai Goodstein 
2284d167e81aSMordechai Goodstein 	WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
2285d167e81aSMordechai Goodstein 	mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
2286e705c121SKalle Valo }
2287e705c121SKalle Valo 
2288e705c121SKalle Valo /* Send the FW a request to remove the station from it's internal data
2289e705c121SKalle Valo  * structures, but DO NOT remove the entry from the local data structures. */
2290e705c121SKalle Valo int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2291e705c121SKalle Valo {
2292e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2293e705c121SKalle Valo 	int ret;
2294e705c121SKalle Valo 
2295e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2296e705c121SKalle Valo 
2297df88c08dSLiad Kaufman 	iwl_mvm_free_bcast_sta_queues(mvm, vif);
2298df88c08dSLiad Kaufman 
2299e705c121SKalle Valo 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
2300e705c121SKalle Valo 	if (ret)
2301e705c121SKalle Valo 		IWL_WARN(mvm, "Failed sending remove station\n");
2302e705c121SKalle Valo 	return ret;
2303e705c121SKalle Valo }
2304e705c121SKalle Valo 
2305e705c121SKalle Valo int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2306e705c121SKalle Valo {
2307e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2308e705c121SKalle Valo 
2309e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2310e705c121SKalle Valo 
2311c8f54701SJohannes Berg 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, 0,
2312ced19f26SSara Sharon 					ieee80211_vif_type_p2p(vif),
2313ced19f26SSara Sharon 					IWL_STA_GENERAL_PURPOSE);
2314e705c121SKalle Valo }
2315e705c121SKalle Valo 
2316e705c121SKalle Valo /* Allocate a new station entry for the broadcast station to the given vif,
2317e705c121SKalle Valo  * and send it to the FW.
2318e705c121SKalle Valo  * Note that each P2P mac should have its own broadcast station.
2319e705c121SKalle Valo  *
2320e705c121SKalle Valo  * @mvm: the mvm component
2321e705c121SKalle Valo  * @vif: the interface to which the broadcast station is added
2322e705c121SKalle Valo  * @bsta: the broadcast station to add. */
2323d197358bSLuca Coelho int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2324e705c121SKalle Valo {
2325e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2326e705c121SKalle Valo 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2327e705c121SKalle Valo 	int ret;
2328e705c121SKalle Valo 
2329e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2330e705c121SKalle Valo 
2331e705c121SKalle Valo 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2332e705c121SKalle Valo 	if (ret)
2333e705c121SKalle Valo 		return ret;
2334e705c121SKalle Valo 
2335e705c121SKalle Valo 	ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2336e705c121SKalle Valo 
2337e705c121SKalle Valo 	if (ret)
2338e705c121SKalle Valo 		iwl_mvm_dealloc_int_sta(mvm, bsta);
2339e705c121SKalle Valo 
2340e705c121SKalle Valo 	return ret;
2341e705c121SKalle Valo }
2342e705c121SKalle Valo 
2343e705c121SKalle Valo void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2344e705c121SKalle Valo {
2345e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2346e705c121SKalle Valo 
2347e705c121SKalle Valo 	iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2348e705c121SKalle Valo }
2349e705c121SKalle Valo 
2350e705c121SKalle Valo /*
2351e705c121SKalle Valo  * Send the FW a request to remove the station from it's internal data
2352e705c121SKalle Valo  * structures, and in addition remove it from the local data structure.
2353e705c121SKalle Valo  */
2354d197358bSLuca Coelho int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2355e705c121SKalle Valo {
2356e705c121SKalle Valo 	int ret;
2357e705c121SKalle Valo 
2358e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2359e705c121SKalle Valo 
2360e705c121SKalle Valo 	ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2361e705c121SKalle Valo 
2362e705c121SKalle Valo 	iwl_mvm_dealloc_bcast_sta(mvm, vif);
2363e705c121SKalle Valo 
2364e705c121SKalle Valo 	return ret;
2365e705c121SKalle Valo }
2366e705c121SKalle Valo 
236726d6c16bSSara Sharon /*
236826d6c16bSSara Sharon  * Allocate a new station entry for the multicast station to the given vif,
236926d6c16bSSara Sharon  * and send it to the FW.
237026d6c16bSSara Sharon  * Note that each AP/GO mac should have its own multicast station.
237126d6c16bSSara Sharon  *
237226d6c16bSSara Sharon  * @mvm: the mvm component
237326d6c16bSSara Sharon  * @vif: the interface to which the multicast station is added
237426d6c16bSSara Sharon  */
237526d6c16bSSara Sharon int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
237626d6c16bSSara Sharon {
237726d6c16bSSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
237826d6c16bSSara Sharon 	struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
237926d6c16bSSara Sharon 	static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
238026d6c16bSSara Sharon 	const u8 *maddr = _maddr;
238126d6c16bSSara Sharon 	struct iwl_trans_txq_scd_cfg cfg = {
2382192a7e1fSJohannes Berg 		.fifo = vif->type == NL80211_IFTYPE_AP ?
2383192a7e1fSJohannes Berg 			IWL_MVM_TX_FIFO_MCAST : IWL_MVM_TX_FIFO_BE,
238426d6c16bSSara Sharon 		.sta_id = msta->sta_id,
23856508de03SIlan Peer 		.tid = 0,
238626d6c16bSSara Sharon 		.aggregate = false,
238726d6c16bSSara Sharon 		.frame_limit = IWL_FRAME_LIMIT,
238826d6c16bSSara Sharon 	};
238926d6c16bSSara Sharon 	unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
239026d6c16bSSara Sharon 	int ret;
239126d6c16bSSara Sharon 
239226d6c16bSSara Sharon 	lockdep_assert_held(&mvm->mutex);
239326d6c16bSSara Sharon 
2394ee48b722SLiad Kaufman 	if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2395ee48b722SLiad Kaufman 		    vif->type != NL80211_IFTYPE_ADHOC))
239626d6c16bSSara Sharon 		return -ENOTSUPP;
239726d6c16bSSara Sharon 
2398ced19f26SSara Sharon 	/*
2399fc07bd8cSSara Sharon 	 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2400fc07bd8cSSara Sharon 	 * invalid, so make sure we use the queue we want.
2401fc07bd8cSSara Sharon 	 * Note that this is done here as we want to avoid making DQA
2402fc07bd8cSSara Sharon 	 * changes in mac80211 layer.
2403fc07bd8cSSara Sharon 	 */
2404cfbc6c4cSSara Sharon 	if (vif->type == NL80211_IFTYPE_ADHOC)
2405cfbc6c4cSSara Sharon 		mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2406fc07bd8cSSara Sharon 
2407fc07bd8cSSara Sharon 	/*
2408ced19f26SSara Sharon 	 * While in previous FWs we had to exclude cab queue from TFD queue
2409ced19f26SSara Sharon 	 * mask, now it is needed as any other queue.
2410ced19f26SSara Sharon 	 */
2411ced19f26SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm) &&
2412ced19f26SSara Sharon 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2413cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2414cfbc6c4cSSara Sharon 				   timeout);
2415cfbc6c4cSSara Sharon 		msta->tfd_queue_msk |= BIT(mvmvif->cab_queue);
2416ced19f26SSara Sharon 	}
241726d6c16bSSara Sharon 	ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
241826d6c16bSSara Sharon 					 mvmvif->id, mvmvif->color);
241991cf5dedSJohannes Berg 	if (ret)
242091cf5dedSJohannes Berg 		goto err;
242126d6c16bSSara Sharon 
242226d6c16bSSara Sharon 	/*
242326d6c16bSSara Sharon 	 * Enable cab queue after the ADD_STA command is sent.
24242f7a3863SLuca Coelho 	 * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
2425ced19f26SSara Sharon 	 * command with unknown station id, and for FW that doesn't support
2426ced19f26SSara Sharon 	 * station API since the cab queue is not included in the
2427ced19f26SSara Sharon 	 * tfd_queue_mask.
242826d6c16bSSara Sharon 	 */
2429310181ecSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
2430cfbc6c4cSSara Sharon 		int queue = iwl_mvm_tvqm_enable_txq(mvm, msta->sta_id,
24316508de03SIlan Peer 						    0,
243226d6c16bSSara Sharon 						    timeout);
243391cf5dedSJohannes Berg 		if (queue < 0) {
243491cf5dedSJohannes Berg 			ret = queue;
243591cf5dedSJohannes Berg 			goto err;
243691cf5dedSJohannes Berg 		}
2437e2af3fabSSara Sharon 		mvmvif->cab_queue = queue;
2438ced19f26SSara Sharon 	} else if (!fw_has_api(&mvm->fw->ucode_capa,
2439fc07bd8cSSara Sharon 			       IWL_UCODE_TLV_API_STA_TYPE))
2440cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2441cfbc6c4cSSara Sharon 				   timeout);
244226d6c16bSSara Sharon 
244326d6c16bSSara Sharon 	return 0;
244491cf5dedSJohannes Berg err:
244591cf5dedSJohannes Berg 	iwl_mvm_dealloc_int_sta(mvm, msta);
244691cf5dedSJohannes Berg 	return ret;
244726d6c16bSSara Sharon }
244826d6c16bSSara Sharon 
244928916a16SEmmanuel Grumbach static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
245028916a16SEmmanuel Grumbach 				    struct ieee80211_key_conf *keyconf,
245128916a16SEmmanuel Grumbach 				    bool mcast)
245228916a16SEmmanuel Grumbach {
245328916a16SEmmanuel Grumbach 	union {
245428916a16SEmmanuel Grumbach 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
245528916a16SEmmanuel Grumbach 		struct iwl_mvm_add_sta_key_cmd cmd;
245628916a16SEmmanuel Grumbach 	} u = {};
245728916a16SEmmanuel Grumbach 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
245828916a16SEmmanuel Grumbach 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
245928916a16SEmmanuel Grumbach 	__le16 key_flags;
246028916a16SEmmanuel Grumbach 	int ret, size;
246128916a16SEmmanuel Grumbach 	u32 status;
246228916a16SEmmanuel Grumbach 
246328916a16SEmmanuel Grumbach 	/* This is a valid situation for GTK removal */
246428916a16SEmmanuel Grumbach 	if (sta_id == IWL_MVM_INVALID_STA)
246528916a16SEmmanuel Grumbach 		return 0;
246628916a16SEmmanuel Grumbach 
246728916a16SEmmanuel Grumbach 	key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
246828916a16SEmmanuel Grumbach 				 STA_KEY_FLG_KEYID_MSK);
246928916a16SEmmanuel Grumbach 	key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
247028916a16SEmmanuel Grumbach 	key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
247128916a16SEmmanuel Grumbach 
247228916a16SEmmanuel Grumbach 	if (mcast)
247328916a16SEmmanuel Grumbach 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
247428916a16SEmmanuel Grumbach 
247528916a16SEmmanuel Grumbach 	/*
247628916a16SEmmanuel Grumbach 	 * The fields assigned here are in the same location at the start
247728916a16SEmmanuel Grumbach 	 * of the command, so we can do this union trick.
247828916a16SEmmanuel Grumbach 	 */
247928916a16SEmmanuel Grumbach 	u.cmd.common.key_flags = key_flags;
248028916a16SEmmanuel Grumbach 	u.cmd.common.key_offset = keyconf->hw_key_idx;
248128916a16SEmmanuel Grumbach 	u.cmd.common.sta_id = sta_id;
248228916a16SEmmanuel Grumbach 
248328916a16SEmmanuel Grumbach 	size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
248428916a16SEmmanuel Grumbach 
248528916a16SEmmanuel Grumbach 	status = ADD_STA_SUCCESS;
248628916a16SEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
248728916a16SEmmanuel Grumbach 					  &status);
248828916a16SEmmanuel Grumbach 
248928916a16SEmmanuel Grumbach 	switch (status) {
249028916a16SEmmanuel Grumbach 	case ADD_STA_SUCCESS:
249128916a16SEmmanuel Grumbach 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
249228916a16SEmmanuel Grumbach 		break;
249328916a16SEmmanuel Grumbach 	default:
249428916a16SEmmanuel Grumbach 		ret = -EIO;
249528916a16SEmmanuel Grumbach 		IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
249628916a16SEmmanuel Grumbach 		break;
249728916a16SEmmanuel Grumbach 	}
249828916a16SEmmanuel Grumbach 
249928916a16SEmmanuel Grumbach 	return ret;
250028916a16SEmmanuel Grumbach }
250128916a16SEmmanuel Grumbach 
250226d6c16bSSara Sharon /*
250326d6c16bSSara Sharon  * Send the FW a request to remove the station from it's internal data
250426d6c16bSSara Sharon  * structures, and in addition remove it from the local data structure.
250526d6c16bSSara Sharon  */
250626d6c16bSSara Sharon int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
250726d6c16bSSara Sharon {
250826d6c16bSSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
250926d6c16bSSara Sharon 	int ret;
251026d6c16bSSara Sharon 
251126d6c16bSSara Sharon 	lockdep_assert_held(&mvm->mutex);
251226d6c16bSSara Sharon 
2513f9084775SNathan Errera 	iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true);
2514d49394a1SSara Sharon 
2515c5a976cfSJohannes Berg 	iwl_mvm_disable_txq(mvm, NULL, mvmvif->mcast_sta.sta_id,
2516c5a976cfSJohannes Berg 			    &mvmvif->cab_queue, 0);
251726d6c16bSSara Sharon 
251826d6c16bSSara Sharon 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
251926d6c16bSSara Sharon 	if (ret)
252026d6c16bSSara Sharon 		IWL_WARN(mvm, "Failed sending remove station\n");
252126d6c16bSSara Sharon 
252226d6c16bSSara Sharon 	return ret;
252326d6c16bSSara Sharon }
252426d6c16bSSara Sharon 
2525b915c101SSara Sharon static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
252610b2b201SSara Sharon {
25275e1688ceSJohannes Berg 	struct iwl_mvm_delba_data notif = {
25285e1688ceSJohannes Berg 		.baid = baid,
2529b915c101SSara Sharon 	};
25305e1688ceSJohannes Berg 
25315e1688ceSJohannes Berg 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_NOTIF_DEL_BA, true,
25325e1688ceSJohannes Berg 					&notif, sizeof(notif));
253310b2b201SSara Sharon };
253410b2b201SSara Sharon 
2535b915c101SSara Sharon static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2536b915c101SSara Sharon 				 struct iwl_mvm_baid_data *data)
2537b915c101SSara Sharon {
2538b915c101SSara Sharon 	int i;
2539b915c101SSara Sharon 
2540b915c101SSara Sharon 	iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2541b915c101SSara Sharon 
2542b915c101SSara Sharon 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2543b915c101SSara Sharon 		int j;
2544b915c101SSara Sharon 		struct iwl_mvm_reorder_buffer *reorder_buf =
2545b915c101SSara Sharon 			&data->reorder_buf[i];
2546dfdddd92SJohannes Berg 		struct iwl_mvm_reorder_buf_entry *entries =
2547dfdddd92SJohannes Berg 			&data->entries[i * data->entries_per_queue];
2548b915c101SSara Sharon 
25490690405fSSara Sharon 		spin_lock_bh(&reorder_buf->lock);
25500690405fSSara Sharon 		if (likely(!reorder_buf->num_stored)) {
25510690405fSSara Sharon 			spin_unlock_bh(&reorder_buf->lock);
2552b915c101SSara Sharon 			continue;
25530690405fSSara Sharon 		}
2554b915c101SSara Sharon 
2555b915c101SSara Sharon 		/*
2556b915c101SSara Sharon 		 * This shouldn't happen in regular DELBA since the internal
2557b915c101SSara Sharon 		 * delBA notification should trigger a release of all frames in
2558b915c101SSara Sharon 		 * the reorder buffer.
2559b915c101SSara Sharon 		 */
2560b915c101SSara Sharon 		WARN_ON(1);
2561b915c101SSara Sharon 
2562b915c101SSara Sharon 		for (j = 0; j < reorder_buf->buf_size; j++)
2563dfdddd92SJohannes Berg 			__skb_queue_purge(&entries[j].e.frames);
25640690405fSSara Sharon 		/*
25650690405fSSara Sharon 		 * Prevent timer re-arm. This prevents a very far fetched case
25660690405fSSara Sharon 		 * where we timed out on the notification. There may be prior
25670690405fSSara Sharon 		 * RX frames pending in the RX queue before the notification
25680690405fSSara Sharon 		 * that might get processed between now and the actual deletion
25690690405fSSara Sharon 		 * and we would re-arm the timer although we are deleting the
25700690405fSSara Sharon 		 * reorder buffer.
25710690405fSSara Sharon 		 */
25720690405fSSara Sharon 		reorder_buf->removed = true;
25730690405fSSara Sharon 		spin_unlock_bh(&reorder_buf->lock);
25740690405fSSara Sharon 		del_timer_sync(&reorder_buf->reorder_timer);
2575b915c101SSara Sharon 	}
2576b915c101SSara Sharon }
2577b915c101SSara Sharon 
2578b915c101SSara Sharon static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2579b915c101SSara Sharon 					struct iwl_mvm_baid_data *data,
2580514c3069SLuca Coelho 					u16 ssn, u16 buf_size)
2581b915c101SSara Sharon {
2582b915c101SSara Sharon 	int i;
2583b915c101SSara Sharon 
2584b915c101SSara Sharon 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2585b915c101SSara Sharon 		struct iwl_mvm_reorder_buffer *reorder_buf =
2586b915c101SSara Sharon 			&data->reorder_buf[i];
2587dfdddd92SJohannes Berg 		struct iwl_mvm_reorder_buf_entry *entries =
2588dfdddd92SJohannes Berg 			&data->entries[i * data->entries_per_queue];
2589b915c101SSara Sharon 		int j;
2590b915c101SSara Sharon 
2591b915c101SSara Sharon 		reorder_buf->num_stored = 0;
2592b915c101SSara Sharon 		reorder_buf->head_sn = ssn;
2593b915c101SSara Sharon 		reorder_buf->buf_size = buf_size;
25940690405fSSara Sharon 		/* rx reorder timer */
25958cef5344SKees Cook 		timer_setup(&reorder_buf->reorder_timer,
25968cef5344SKees Cook 			    iwl_mvm_reorder_timer_expired, 0);
25970690405fSSara Sharon 		spin_lock_init(&reorder_buf->lock);
25980690405fSSara Sharon 		reorder_buf->mvm = mvm;
2599b915c101SSara Sharon 		reorder_buf->queue = i;
26005d43eab6SSara Sharon 		reorder_buf->valid = false;
2601b915c101SSara Sharon 		for (j = 0; j < reorder_buf->buf_size; j++)
2602dfdddd92SJohannes Berg 			__skb_queue_head_init(&entries[j].e.frames);
2603b915c101SSara Sharon 	}
260410b2b201SSara Sharon }
260510b2b201SSara Sharon 
260697f70c56SJohannes Berg static int iwl_mvm_fw_baid_op_sta(struct iwl_mvm *mvm,
260797f70c56SJohannes Berg 				  struct iwl_mvm_sta *mvm_sta,
260897f70c56SJohannes Berg 				  bool start, int tid, u16 ssn,
260997f70c56SJohannes Berg 				  u16 buf_size)
26100f3a4e48SJohannes Berg {
26110f3a4e48SJohannes Berg 	struct iwl_mvm_add_sta_cmd cmd = {
26120f3a4e48SJohannes Berg 		.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
26130f3a4e48SJohannes Berg 		.sta_id = mvm_sta->sta_id,
26140f3a4e48SJohannes Berg 		.add_modify = STA_MODE_MODIFY,
26150f3a4e48SJohannes Berg 	};
26160f3a4e48SJohannes Berg 	u32 status;
26170f3a4e48SJohannes Berg 	int ret;
26180f3a4e48SJohannes Berg 
26190f3a4e48SJohannes Berg 	if (start) {
26200f3a4e48SJohannes Berg 		cmd.add_immediate_ba_tid = tid;
26210f3a4e48SJohannes Berg 		cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
26220f3a4e48SJohannes Berg 		cmd.rx_ba_window = cpu_to_le16(buf_size);
26230f3a4e48SJohannes Berg 		cmd.modify_mask = STA_MODIFY_ADD_BA_TID;
26240f3a4e48SJohannes Berg 	} else {
26250f3a4e48SJohannes Berg 		cmd.remove_immediate_ba_tid = tid;
26260f3a4e48SJohannes Berg 		cmd.modify_mask = STA_MODIFY_REMOVE_BA_TID;
26270f3a4e48SJohannes Berg 	}
26280f3a4e48SJohannes Berg 
26290f3a4e48SJohannes Berg 	status = ADD_STA_SUCCESS;
26300f3a4e48SJohannes Berg 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
26310f3a4e48SJohannes Berg 					  iwl_mvm_add_sta_cmd_size(mvm),
26320f3a4e48SJohannes Berg 					  &cmd, &status);
26330f3a4e48SJohannes Berg 	if (ret)
26340f3a4e48SJohannes Berg 		return ret;
26350f3a4e48SJohannes Berg 
26360f3a4e48SJohannes Berg 	switch (status & IWL_ADD_STA_STATUS_MASK) {
26370f3a4e48SJohannes Berg 	case ADD_STA_SUCCESS:
26380f3a4e48SJohannes Berg 		IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
26390f3a4e48SJohannes Berg 			     start ? "start" : "stopp");
26400f3a4e48SJohannes Berg 		if (WARN_ON(start && iwl_mvm_has_new_rx_api(mvm) &&
26410f3a4e48SJohannes Berg 			    !(status & IWL_ADD_STA_BAID_VALID_MASK)))
26420f3a4e48SJohannes Berg 			return -EINVAL;
26430f3a4e48SJohannes Berg 		return u32_get_bits(status, IWL_ADD_STA_BAID_MASK);
26440f3a4e48SJohannes Berg 	case ADD_STA_IMMEDIATE_BA_FAILURE:
26450f3a4e48SJohannes Berg 		IWL_WARN(mvm, "RX BA Session refused by fw\n");
26460f3a4e48SJohannes Berg 		return -ENOSPC;
26470f3a4e48SJohannes Berg 	default:
26480f3a4e48SJohannes Berg 		IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
26490f3a4e48SJohannes Berg 			start ? "start" : "stopp", status);
26500f3a4e48SJohannes Berg 		return -EIO;
26510f3a4e48SJohannes Berg 	}
26520f3a4e48SJohannes Berg }
26530f3a4e48SJohannes Berg 
265497f70c56SJohannes Berg static int iwl_mvm_fw_baid_op_cmd(struct iwl_mvm *mvm,
265597f70c56SJohannes Berg 				  struct iwl_mvm_sta *mvm_sta,
265697f70c56SJohannes Berg 				  bool start, int tid, u16 ssn,
265797f70c56SJohannes Berg 				  u16 buf_size, int baid)
265897f70c56SJohannes Berg {
265997f70c56SJohannes Berg 	struct iwl_rx_baid_cfg_cmd cmd = {
266097f70c56SJohannes Berg 		.action = start ? cpu_to_le32(IWL_RX_BAID_ACTION_ADD) :
266197f70c56SJohannes Berg 				  cpu_to_le32(IWL_RX_BAID_ACTION_REMOVE),
266297f70c56SJohannes Berg 	};
266397f70c56SJohannes Berg 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, RX_BAID_ALLOCATION_CONFIG_CMD);
266497f70c56SJohannes Berg 	int ret;
266597f70c56SJohannes Berg 
266697f70c56SJohannes Berg 	BUILD_BUG_ON(sizeof(struct iwl_rx_baid_cfg_resp) != sizeof(baid));
266797f70c56SJohannes Berg 
266897f70c56SJohannes Berg 	if (start) {
266997f70c56SJohannes Berg 		cmd.alloc.sta_id_mask = cpu_to_le32(BIT(mvm_sta->sta_id));
267097f70c56SJohannes Berg 		cmd.alloc.tid = tid;
267197f70c56SJohannes Berg 		cmd.alloc.ssn = cpu_to_le16(ssn);
267297f70c56SJohannes Berg 		cmd.alloc.win_size = cpu_to_le16(buf_size);
267397f70c56SJohannes Berg 		baid = -EIO;
267426de4c8bSJohannes Berg 	} else if (iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 1) == 1) {
267526de4c8bSJohannes Berg 		cmd.remove_v1.baid = cpu_to_le32(baid);
267626de4c8bSJohannes Berg 		BUILD_BUG_ON(sizeof(cmd.remove_v1) > sizeof(cmd.remove));
267797f70c56SJohannes Berg 	} else {
267826de4c8bSJohannes Berg 		cmd.remove.sta_id_mask = cpu_to_le32(BIT(mvm_sta->sta_id));
267926de4c8bSJohannes Berg 		cmd.remove.tid = cpu_to_le32(tid);
268097f70c56SJohannes Berg 	}
268197f70c56SJohannes Berg 
268297f70c56SJohannes Berg 	ret = iwl_mvm_send_cmd_pdu_status(mvm, cmd_id, sizeof(cmd),
268397f70c56SJohannes Berg 					  &cmd, &baid);
268497f70c56SJohannes Berg 	if (ret)
268597f70c56SJohannes Berg 		return ret;
268697f70c56SJohannes Berg 
268797f70c56SJohannes Berg 	if (!start) {
268897f70c56SJohannes Berg 		/* ignore firmware baid on remove */
268997f70c56SJohannes Berg 		baid = 0;
269097f70c56SJohannes Berg 	}
269197f70c56SJohannes Berg 
269297f70c56SJohannes Berg 	IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
269397f70c56SJohannes Berg 		     start ? "start" : "stopp");
269497f70c56SJohannes Berg 
269597f70c56SJohannes Berg 	if (baid < 0 || baid >= ARRAY_SIZE(mvm->baid_map))
269697f70c56SJohannes Berg 		return -EINVAL;
269797f70c56SJohannes Berg 
269897f70c56SJohannes Berg 	return baid;
269997f70c56SJohannes Berg }
270097f70c56SJohannes Berg 
270197f70c56SJohannes Berg static int iwl_mvm_fw_baid_op(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvm_sta,
270297f70c56SJohannes Berg 			      bool start, int tid, u16 ssn, u16 buf_size,
270397f70c56SJohannes Berg 			      int baid)
270497f70c56SJohannes Berg {
270597f70c56SJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
270697f70c56SJohannes Berg 			IWL_UCODE_TLV_CAPA_BAID_ML_SUPPORT))
270797f70c56SJohannes Berg 		return iwl_mvm_fw_baid_op_cmd(mvm, mvm_sta, start,
270897f70c56SJohannes Berg 					      tid, ssn, buf_size, baid);
270997f70c56SJohannes Berg 
271097f70c56SJohannes Berg 	return iwl_mvm_fw_baid_op_sta(mvm, mvm_sta, start,
271197f70c56SJohannes Berg 				      tid, ssn, buf_size);
271297f70c56SJohannes Berg }
271397f70c56SJohannes Berg 
2714e705c121SKalle Valo int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2715514c3069SLuca Coelho 		       int tid, u16 ssn, bool start, u16 buf_size, u16 timeout)
2716e705c121SKalle Valo {
2717e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
271810b2b201SSara Sharon 	struct iwl_mvm_baid_data *baid_data = NULL;
27190f3a4e48SJohannes Berg 	int ret, baid;
2720ded5ded3SEmmanuel Grumbach 	u32 max_ba_id_sessions = iwl_mvm_has_new_tx_api(mvm) ? IWL_MAX_BAID :
2721ded5ded3SEmmanuel Grumbach 							       IWL_MAX_BAID_OLD;
2722e705c121SKalle Valo 
2723e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2724e705c121SKalle Valo 
2725ded5ded3SEmmanuel Grumbach 	if (start && mvm->rx_ba_sessions >= max_ba_id_sessions) {
2726e705c121SKalle Valo 		IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2727e705c121SKalle Valo 		return -ENOSPC;
2728e705c121SKalle Valo 	}
2729e705c121SKalle Valo 
273010b2b201SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) && start) {
2731dfdddd92SJohannes Berg 		u16 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2732dfdddd92SJohannes Berg 
2733dfdddd92SJohannes Berg 		/* sparse doesn't like the __align() so don't check */
2734dfdddd92SJohannes Berg #ifndef __CHECKER__
2735dfdddd92SJohannes Berg 		/*
2736dfdddd92SJohannes Berg 		 * The division below will be OK if either the cache line size
2737dfdddd92SJohannes Berg 		 * can be divided by the entry size (ALIGN will round up) or if
2738dfdddd92SJohannes Berg 		 * if the entry size can be divided by the cache line size, in
2739dfdddd92SJohannes Berg 		 * which case the ALIGN() will do nothing.
2740dfdddd92SJohannes Berg 		 */
2741dfdddd92SJohannes Berg 		BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2742dfdddd92SJohannes Berg 			     sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2743dfdddd92SJohannes Berg #endif
2744dfdddd92SJohannes Berg 
2745dfdddd92SJohannes Berg 		/*
2746dfdddd92SJohannes Berg 		 * Upward align the reorder buffer size to fill an entire cache
2747dfdddd92SJohannes Berg 		 * line for each queue, to avoid sharing cache lines between
2748dfdddd92SJohannes Berg 		 * different queues.
2749dfdddd92SJohannes Berg 		 */
2750dfdddd92SJohannes Berg 		reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2751dfdddd92SJohannes Berg 
275210b2b201SSara Sharon 		/*
275310b2b201SSara Sharon 		 * Allocate here so if allocation fails we can bail out early
275410b2b201SSara Sharon 		 * before starting the BA session in the firmware
275510b2b201SSara Sharon 		 */
2756b915c101SSara Sharon 		baid_data = kzalloc(sizeof(*baid_data) +
2757b915c101SSara Sharon 				    mvm->trans->num_rx_queues *
2758dfdddd92SJohannes Berg 				    reorder_buf_size,
2759b915c101SSara Sharon 				    GFP_KERNEL);
276010b2b201SSara Sharon 		if (!baid_data)
276110b2b201SSara Sharon 			return -ENOMEM;
2762dfdddd92SJohannes Berg 
2763dfdddd92SJohannes Berg 		/*
2764dfdddd92SJohannes Berg 		 * This division is why we need the above BUILD_BUG_ON(),
2765dfdddd92SJohannes Berg 		 * if that doesn't hold then this will not be right.
2766dfdddd92SJohannes Berg 		 */
2767dfdddd92SJohannes Berg 		baid_data->entries_per_queue =
2768dfdddd92SJohannes Berg 			reorder_buf_size / sizeof(baid_data->entries[0]);
276910b2b201SSara Sharon 	}
277010b2b201SSara Sharon 
277197f70c56SJohannes Berg 	if (iwl_mvm_has_new_rx_api(mvm) && !start) {
277297f70c56SJohannes Berg 		baid = mvm_sta->tid_to_baid[tid];
277397f70c56SJohannes Berg 	} else {
277497f70c56SJohannes Berg 		/* we don't really need it in this case */
277597f70c56SJohannes Berg 		baid = -1;
277697f70c56SJohannes Berg 	}
277797f70c56SJohannes Berg 
27783538c809SLuca Coelho 	/* Don't send command to remove (start=0) BAID during restart */
27793538c809SLuca Coelho 	if (start || !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
278097f70c56SJohannes Berg 		baid = iwl_mvm_fw_baid_op(mvm, mvm_sta, start, tid, ssn, buf_size,
278197f70c56SJohannes Berg 					  baid);
27823538c809SLuca Coelho 
27830f3a4e48SJohannes Berg 	if (baid < 0) {
27840f3a4e48SJohannes Berg 		ret = baid;
278510b2b201SSara Sharon 		goto out_free;
2786e705c121SKalle Valo 	}
2787e705c121SKalle Valo 
278810b2b201SSara Sharon 	if (start) {
2789e705c121SKalle Valo 		mvm->rx_ba_sessions++;
279010b2b201SSara Sharon 
279110b2b201SSara Sharon 		if (!iwl_mvm_has_new_rx_api(mvm))
279210b2b201SSara Sharon 			return 0;
279310b2b201SSara Sharon 
279410b2b201SSara Sharon 		baid_data->baid = baid;
279510b2b201SSara Sharon 		baid_data->timeout = timeout;
279610b2b201SSara Sharon 		baid_data->last_rx = jiffies;
27978cef5344SKees Cook 		baid_data->rcu_ptr = &mvm->baid_map[baid];
27988cef5344SKees Cook 		timer_setup(&baid_data->session_timer,
27998cef5344SKees Cook 			    iwl_mvm_rx_agg_session_expired, 0);
280010b2b201SSara Sharon 		baid_data->mvm = mvm;
280110b2b201SSara Sharon 		baid_data->tid = tid;
280210b2b201SSara Sharon 		baid_data->sta_id = mvm_sta->sta_id;
280310b2b201SSara Sharon 
280410b2b201SSara Sharon 		mvm_sta->tid_to_baid[tid] = baid;
280510b2b201SSara Sharon 		if (timeout)
280610b2b201SSara Sharon 			mod_timer(&baid_data->session_timer,
280710b2b201SSara Sharon 				  TU_TO_EXP_TIME(timeout * 2));
280810b2b201SSara Sharon 
28093f1c4c58SSara Sharon 		iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size);
281010b2b201SSara Sharon 		/*
281110b2b201SSara Sharon 		 * protect the BA data with RCU to cover a case where our
281210b2b201SSara Sharon 		 * internal RX sync mechanism will timeout (not that it's
281310b2b201SSara Sharon 		 * supposed to happen) and we will free the session data while
281410b2b201SSara Sharon 		 * RX is being processed in parallel
281510b2b201SSara Sharon 		 */
281635263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
281735263a03SSara Sharon 			     mvm_sta->sta_id, tid, baid);
281810b2b201SSara Sharon 		WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
281910b2b201SSara Sharon 		rcu_assign_pointer(mvm->baid_map[baid], baid_data);
282060dec523SSara Sharon 	} else  {
28210f3a4e48SJohannes Berg 		baid = mvm_sta->tid_to_baid[tid];
282210b2b201SSara Sharon 
282360dec523SSara Sharon 		if (mvm->rx_ba_sessions > 0)
2824e705c121SKalle Valo 			/* check that restart flow didn't zero the counter */
2825e705c121SKalle Valo 			mvm->rx_ba_sessions--;
282610b2b201SSara Sharon 		if (!iwl_mvm_has_new_rx_api(mvm))
282710b2b201SSara Sharon 			return 0;
2828e705c121SKalle Valo 
282910b2b201SSara Sharon 		if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
283010b2b201SSara Sharon 			return -EINVAL;
283110b2b201SSara Sharon 
283210b2b201SSara Sharon 		baid_data = rcu_access_pointer(mvm->baid_map[baid]);
283310b2b201SSara Sharon 		if (WARN_ON(!baid_data))
283410b2b201SSara Sharon 			return -EINVAL;
283510b2b201SSara Sharon 
283610b2b201SSara Sharon 		/* synchronize all rx queues so we can safely delete */
2837b915c101SSara Sharon 		iwl_mvm_free_reorder(mvm, baid_data);
2838292a089dSSteven Rostedt (Google) 		timer_shutdown_sync(&baid_data->session_timer);
283910b2b201SSara Sharon 		RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
284010b2b201SSara Sharon 		kfree_rcu(baid_data, rcu_head);
284135263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
28422438d430SJohannes Berg 
28432438d430SJohannes Berg 		/*
28442438d430SJohannes Berg 		 * After we've deleted it, do another queue sync
28452438d430SJohannes Berg 		 * so if an IWL_MVM_RXQ_NSSN_SYNC was concurrently
28462438d430SJohannes Berg 		 * running it won't find a new session in the old
28472438d430SJohannes Berg 		 * BAID. It can find the NULL pointer for the BAID,
28482438d430SJohannes Berg 		 * but we must not have it find a different session.
28492438d430SJohannes Berg 		 */
28502438d430SJohannes Berg 		iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY,
28512438d430SJohannes Berg 						true, NULL, 0);
285210b2b201SSara Sharon 	}
285310b2b201SSara Sharon 	return 0;
285410b2b201SSara Sharon 
285510b2b201SSara Sharon out_free:
285610b2b201SSara Sharon 	kfree(baid_data);
2857e705c121SKalle Valo 	return ret;
2858e705c121SKalle Valo }
2859e705c121SKalle Valo 
28609794c64fSLiad Kaufman int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2861e705c121SKalle Valo 		       int tid, u8 queue, bool start)
2862e705c121SKalle Valo {
2863e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2864e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
2865e705c121SKalle Valo 	int ret;
2866e705c121SKalle Valo 	u32 status;
2867e705c121SKalle Valo 
2868e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2869e705c121SKalle Valo 
2870e705c121SKalle Valo 	if (start) {
2871e705c121SKalle Valo 		mvm_sta->tfd_queue_msk |= BIT(queue);
2872e705c121SKalle Valo 		mvm_sta->tid_disable_agg &= ~BIT(tid);
2873e705c121SKalle Valo 	} else {
2874cf961e16SLiad Kaufman 		/* In DQA-mode the queue isn't removed on agg termination */
2875e705c121SKalle Valo 		mvm_sta->tid_disable_agg |= BIT(tid);
2876e705c121SKalle Valo 	}
2877e705c121SKalle Valo 
2878e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2879e705c121SKalle Valo 	cmd.sta_id = mvm_sta->sta_id;
2880e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
2881bb49701bSSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm))
2882bb49701bSSara Sharon 		cmd.modify_mask = STA_MODIFY_QUEUES;
2883bb49701bSSara Sharon 	cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
2884e705c121SKalle Valo 	cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2885e705c121SKalle Valo 	cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2886e705c121SKalle Valo 
2887e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
2888854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2889854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
2890e705c121SKalle Valo 					  &cmd, &status);
2891e705c121SKalle Valo 	if (ret)
2892e705c121SKalle Valo 		return ret;
2893e705c121SKalle Valo 
2894837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2895e705c121SKalle Valo 	case ADD_STA_SUCCESS:
2896e705c121SKalle Valo 		break;
2897e705c121SKalle Valo 	default:
2898e705c121SKalle Valo 		ret = -EIO;
2899e705c121SKalle Valo 		IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2900e705c121SKalle Valo 			start ? "start" : "stopp", status);
2901e705c121SKalle Valo 		break;
2902e705c121SKalle Valo 	}
2903e705c121SKalle Valo 
2904e705c121SKalle Valo 	return ret;
2905e705c121SKalle Valo }
2906e705c121SKalle Valo 
2907e705c121SKalle Valo const u8 tid_to_mac80211_ac[] = {
2908e705c121SKalle Valo 	IEEE80211_AC_BE,
2909e705c121SKalle Valo 	IEEE80211_AC_BK,
2910e705c121SKalle Valo 	IEEE80211_AC_BK,
2911e705c121SKalle Valo 	IEEE80211_AC_BE,
2912e705c121SKalle Valo 	IEEE80211_AC_VI,
2913e705c121SKalle Valo 	IEEE80211_AC_VI,
2914e705c121SKalle Valo 	IEEE80211_AC_VO,
2915e705c121SKalle Valo 	IEEE80211_AC_VO,
29169794c64fSLiad Kaufman 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2917e705c121SKalle Valo };
2918e705c121SKalle Valo 
2919e705c121SKalle Valo static const u8 tid_to_ucode_ac[] = {
2920e705c121SKalle Valo 	AC_BE,
2921e705c121SKalle Valo 	AC_BK,
2922e705c121SKalle Valo 	AC_BK,
2923e705c121SKalle Valo 	AC_BE,
2924e705c121SKalle Valo 	AC_VI,
2925e705c121SKalle Valo 	AC_VI,
2926e705c121SKalle Valo 	AC_VO,
2927e705c121SKalle Valo 	AC_VO,
2928e705c121SKalle Valo };
2929e705c121SKalle Valo 
2930e705c121SKalle Valo int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2931e705c121SKalle Valo 			     struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2932e705c121SKalle Valo {
2933e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2934e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data;
2935dd32162dSLiad Kaufman 	u16 normalized_ssn;
2936b0d795a9SMordechay Goodstein 	u16 txq_id;
2937e705c121SKalle Valo 	int ret;
2938e705c121SKalle Valo 
2939e705c121SKalle Valo 	if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2940e705c121SKalle Valo 		return -EINVAL;
2941e705c121SKalle Valo 
2942bd800e41SNaftali Goldstein 	if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
2943bd800e41SNaftali Goldstein 	    mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2944bd800e41SNaftali Goldstein 		IWL_ERR(mvm,
2945bd800e41SNaftali Goldstein 			"Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
2946e705c121SKalle Valo 			mvmsta->tid_data[tid].state);
2947e705c121SKalle Valo 		return -ENXIO;
2948e705c121SKalle Valo 	}
2949e705c121SKalle Valo 
2950e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2951e705c121SKalle Valo 
2952bd8f3fc6SLiad Kaufman 	if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE &&
2953bd8f3fc6SLiad Kaufman 	    iwl_mvm_has_new_tx_api(mvm)) {
2954bd8f3fc6SLiad Kaufman 		u8 ac = tid_to_mac80211_ac[tid];
2955bd8f3fc6SLiad Kaufman 
2956bd8f3fc6SLiad Kaufman 		ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
2957bd8f3fc6SLiad Kaufman 		if (ret)
2958bd8f3fc6SLiad Kaufman 			return ret;
2959bd8f3fc6SLiad Kaufman 	}
2960bd8f3fc6SLiad Kaufman 
2961e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2962e705c121SKalle Valo 
2963cf961e16SLiad Kaufman 	/*
2964cf961e16SLiad Kaufman 	 * Note the possible cases:
29654a6d2e52SAvraham Stern 	 *  1. An enabled TXQ - TXQ needs to become agg'ed
29664a6d2e52SAvraham Stern 	 *  2. The TXQ hasn't yet been enabled, so find a free one and mark
29674a6d2e52SAvraham Stern 	 *	it as reserved
2968cf961e16SLiad Kaufman 	 */
2969cf961e16SLiad Kaufman 	txq_id = mvmsta->tid_data[tid].txq_id;
297034e10860SSara Sharon 	if (txq_id == IWL_MVM_INVALID_QUEUE) {
2971b0d795a9SMordechay Goodstein 		ret = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2972c8f54701SJohannes Berg 					      IWL_MVM_DQA_MIN_DATA_QUEUE,
2973c8f54701SJohannes Berg 					      IWL_MVM_DQA_MAX_DATA_QUEUE);
2974b0d795a9SMordechay Goodstein 		if (ret < 0) {
2975e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to allocate agg queue\n");
2976f3f240f9SJohannes Berg 			goto out;
2977e705c121SKalle Valo 		}
2978cf961e16SLiad Kaufman 
2979b0d795a9SMordechay Goodstein 		txq_id = ret;
2980b0d795a9SMordechay Goodstein 
2981cf961e16SLiad Kaufman 		/* TXQ hasn't yet been enabled, so mark it only as reserved */
2982cf961e16SLiad Kaufman 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2983b0d795a9SMordechay Goodstein 	} else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) {
2984b0d795a9SMordechay Goodstein 		ret = -ENXIO;
2985b0d795a9SMordechay Goodstein 		IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n",
2986b0d795a9SMordechay Goodstein 			tid, IWL_MAX_HW_QUEUES - 1);
2987b0d795a9SMordechay Goodstein 		goto out;
2988b0d795a9SMordechay Goodstein 
29894a6d2e52SAvraham Stern 	} else if (unlikely(mvm->queue_info[txq_id].status ==
29904a6d2e52SAvraham Stern 			    IWL_MVM_QUEUE_SHARED)) {
29914a6d2e52SAvraham Stern 		ret = -ENXIO;
29924a6d2e52SAvraham Stern 		IWL_DEBUG_TX_QUEUES(mvm,
29934a6d2e52SAvraham Stern 				    "Can't start tid %d agg on shared queue!\n",
29944a6d2e52SAvraham Stern 				    tid);
2995f3f240f9SJohannes Berg 		goto out;
2996cf961e16SLiad Kaufman 	}
29979f9af3d7SLiad Kaufman 
2998cf961e16SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm,
2999cf961e16SLiad Kaufman 			    "AGG for tid %d will be on queue #%d\n",
3000cf961e16SLiad Kaufman 			    tid, txq_id);
3001cf961e16SLiad Kaufman 
3002e705c121SKalle Valo 	tid_data = &mvmsta->tid_data[tid];
3003e705c121SKalle Valo 	tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3004e705c121SKalle Valo 	tid_data->txq_id = txq_id;
3005e705c121SKalle Valo 	*ssn = tid_data->ssn;
3006e705c121SKalle Valo 
3007e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm,
3008e705c121SKalle Valo 			    "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
3009e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->ssn,
3010e705c121SKalle Valo 			    tid_data->next_reclaimed);
3011e705c121SKalle Valo 
3012dd32162dSLiad Kaufman 	/*
30132f7a3863SLuca Coelho 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
3014dd32162dSLiad Kaufman 	 * to align the wrap around of ssn so we compare relevant values.
3015dd32162dSLiad Kaufman 	 */
3016dd32162dSLiad Kaufman 	normalized_ssn = tid_data->ssn;
3017286ca8ebSLuca Coelho 	if (mvm->trans->trans_cfg->gen2)
3018dd32162dSLiad Kaufman 		normalized_ssn &= 0xff;
3019dd32162dSLiad Kaufman 
3020dd32162dSLiad Kaufman 	if (normalized_ssn == tid_data->next_reclaimed) {
3021e705c121SKalle Valo 		tid_data->state = IWL_AGG_STARTING;
30222ce113deSJohannes Berg 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
3023e705c121SKalle Valo 	} else {
3024e705c121SKalle Valo 		tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
30257e0ca723SMordechay Goodstein 		ret = IEEE80211_AMPDU_TX_START_DELAY_ADDBA;
30262ce113deSJohannes Berg 	}
3027e705c121SKalle Valo 
30289f9af3d7SLiad Kaufman out:
3029e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3030e705c121SKalle Valo 
3031e705c121SKalle Valo 	return ret;
3032e705c121SKalle Valo }
3033e705c121SKalle Valo 
3034e705c121SKalle Valo int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3035514c3069SLuca Coelho 			    struct ieee80211_sta *sta, u16 tid, u16 buf_size,
3036bb81bb68SEmmanuel Grumbach 			    bool amsdu)
3037e705c121SKalle Valo {
3038e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3039e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3040e705c121SKalle Valo 	unsigned int wdg_timeout =
3041e705c121SKalle Valo 		iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
3042eea76c36SEmmanuel Grumbach 	int queue, ret;
3043cf961e16SLiad Kaufman 	bool alloc_queue = true;
30449f9af3d7SLiad Kaufman 	enum iwl_mvm_queue_status queue_status;
3045e705c121SKalle Valo 	u16 ssn;
3046e705c121SKalle Valo 
3047eea76c36SEmmanuel Grumbach 	struct iwl_trans_txq_scd_cfg cfg = {
3048eea76c36SEmmanuel Grumbach 		.sta_id = mvmsta->sta_id,
3049eea76c36SEmmanuel Grumbach 		.tid = tid,
3050eea76c36SEmmanuel Grumbach 		.frame_limit = buf_size,
3051eea76c36SEmmanuel Grumbach 		.aggregate = true,
3052eea76c36SEmmanuel Grumbach 	};
3053eea76c36SEmmanuel Grumbach 
3054ecaf71deSGregory Greenman 	/*
3055ecaf71deSGregory Greenman 	 * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
3056ecaf71deSGregory Greenman 	 * manager, so this function should never be called in this case.
3057ecaf71deSGregory Greenman 	 */
30584243edb4SEmmanuel Grumbach 	if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
3059ecaf71deSGregory Greenman 		return -EINVAL;
3060ecaf71deSGregory Greenman 
3061e705c121SKalle Valo 	BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
3062e705c121SKalle Valo 		     != IWL_MAX_TID_COUNT);
3063e705c121SKalle Valo 
3064e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3065e705c121SKalle Valo 	ssn = tid_data->ssn;
3066e705c121SKalle Valo 	queue = tid_data->txq_id;
3067e705c121SKalle Valo 	tid_data->state = IWL_AGG_ON;
3068e705c121SKalle Valo 	mvmsta->agg_tids |= BIT(tid);
3069e705c121SKalle Valo 	tid_data->ssn = 0xffff;
3070bb81bb68SEmmanuel Grumbach 	tid_data->amsdu_in_ampdu_allowed = amsdu;
3071e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3072e705c121SKalle Valo 
307334e10860SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
307434e10860SSara Sharon 		/*
30750ec9257bSSara Sharon 		 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
30760ec9257bSSara Sharon 		 * would have failed, so if we are here there is no need to
30770ec9257bSSara Sharon 		 * allocate a queue.
30780ec9257bSSara Sharon 		 * However, if aggregation size is different than the default
30790ec9257bSSara Sharon 		 * size, the scheduler should be reconfigured.
30800ec9257bSSara Sharon 		 * We cannot do this with the new TX API, so return unsupported
30810ec9257bSSara Sharon 		 * for now, until it will be offloaded to firmware..
30820ec9257bSSara Sharon 		 * Note that if SCD default value changes - this condition
30830ec9257bSSara Sharon 		 * should be updated as well.
308434e10860SSara Sharon 		 */
30850ec9257bSSara Sharon 		if (buf_size < IWL_FRAME_LIMIT)
308634e10860SSara Sharon 			return -ENOTSUPP;
308734e10860SSara Sharon 
308834e10860SSara Sharon 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
308934e10860SSara Sharon 		if (ret)
309034e10860SSara Sharon 			return -EIO;
309134e10860SSara Sharon 		goto out;
309234e10860SSara Sharon 	}
309334e10860SSara Sharon 
3094eea76c36SEmmanuel Grumbach 	cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
3095e705c121SKalle Valo 
30969f9af3d7SLiad Kaufman 	queue_status = mvm->queue_info[queue].status;
30979f9af3d7SLiad Kaufman 
3098cf961e16SLiad Kaufman 	/* Maybe there is no need to even alloc a queue... */
3099cf961e16SLiad Kaufman 	if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
3100cf961e16SLiad Kaufman 		alloc_queue = false;
3101cf961e16SLiad Kaufman 
3102cf961e16SLiad Kaufman 	/*
3103cf961e16SLiad Kaufman 	 * Only reconfig the SCD for the queue if the window size has
3104cf961e16SLiad Kaufman 	 * changed from current (become smaller)
3105cf961e16SLiad Kaufman 	 */
31060ec9257bSSara Sharon 	if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
3107cf961e16SLiad Kaufman 		/*
3108cf961e16SLiad Kaufman 		 * If reconfiguring an existing queue, it first must be
3109cf961e16SLiad Kaufman 		 * drained
3110cf961e16SLiad Kaufman 		 */
3111a1a57877SSara Sharon 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
3112cf961e16SLiad Kaufman 						     BIT(queue));
3113cf961e16SLiad Kaufman 		if (ret) {
3114cf961e16SLiad Kaufman 			IWL_ERR(mvm,
3115cf961e16SLiad Kaufman 				"Error draining queue before reconfig\n");
3116cf961e16SLiad Kaufman 			return ret;
3117cf961e16SLiad Kaufman 		}
3118cf961e16SLiad Kaufman 
3119cf961e16SLiad Kaufman 		ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
3120cf961e16SLiad Kaufman 					   mvmsta->sta_id, tid,
3121cf961e16SLiad Kaufman 					   buf_size, ssn);
3122cf961e16SLiad Kaufman 		if (ret) {
3123cf961e16SLiad Kaufman 			IWL_ERR(mvm,
3124cf961e16SLiad Kaufman 				"Error reconfiguring TXQ #%d\n", queue);
3125cf961e16SLiad Kaufman 			return ret;
3126cf961e16SLiad Kaufman 		}
3127cf961e16SLiad Kaufman 	}
3128cf961e16SLiad Kaufman 
3129cf961e16SLiad Kaufman 	if (alloc_queue)
3130cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, sta, queue, ssn,
3131cf961e16SLiad Kaufman 				   &cfg, wdg_timeout);
3132e705c121SKalle Valo 
31339f9af3d7SLiad Kaufman 	/* Send ADD_STA command to enable aggs only if the queue isn't shared */
31349f9af3d7SLiad Kaufman 	if (queue_status != IWL_MVM_QUEUE_SHARED) {
3135e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
3136e705c121SKalle Valo 		if (ret)
3137e705c121SKalle Valo 			return -EIO;
31389f9af3d7SLiad Kaufman 	}
3139e705c121SKalle Valo 
3140e705c121SKalle Valo 	/* No need to mark as reserved */
3141cf961e16SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
3142e705c121SKalle Valo 
314334e10860SSara Sharon out:
3144e705c121SKalle Valo 	/*
3145e705c121SKalle Valo 	 * Even though in theory the peer could have different
3146e705c121SKalle Valo 	 * aggregation reorder buffer sizes for different sessions,
3147e705c121SKalle Valo 	 * our ucode doesn't allow for that and has a global limit
3148e705c121SKalle Valo 	 * for each station. Therefore, use the minimum of all the
3149e705c121SKalle Valo 	 * aggregation sessions and our default value.
3150e705c121SKalle Valo 	 */
3151e705c121SKalle Valo 	mvmsta->max_agg_bufsize =
3152e705c121SKalle Valo 		min(mvmsta->max_agg_bufsize, buf_size);
3153ecaf71deSGregory Greenman 	mvmsta->lq_sta.rs_drv.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
3154e705c121SKalle Valo 
3155e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
3156e705c121SKalle Valo 		     sta->addr, tid);
3157e705c121SKalle Valo 
3158cd4d6b0bSGregory Greenman 	return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.rs_drv.lq);
3159e705c121SKalle Valo }
3160e705c121SKalle Valo 
316134e10860SSara Sharon static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
316234e10860SSara Sharon 					struct iwl_mvm_sta *mvmsta,
31634b387906SAvraham Stern 					struct iwl_mvm_tid_data *tid_data)
316434e10860SSara Sharon {
31654b387906SAvraham Stern 	u16 txq_id = tid_data->txq_id;
31664b387906SAvraham Stern 
3167f3f240f9SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
3168f3f240f9SJohannes Berg 
316934e10860SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm))
317034e10860SSara Sharon 		return;
317134e10860SSara Sharon 
317234e10860SSara Sharon 	/*
317334e10860SSara Sharon 	 * The TXQ is marked as reserved only if no traffic came through yet
317434e10860SSara Sharon 	 * This means no traffic has been sent on this TID (agg'd or not), so
317534e10860SSara Sharon 	 * we no longer have use for the queue. Since it hasn't even been
317634e10860SSara Sharon 	 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
317734e10860SSara Sharon 	 * free.
317834e10860SSara Sharon 	 */
31794b387906SAvraham Stern 	if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
318034e10860SSara Sharon 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
31814b387906SAvraham Stern 		tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
31824b387906SAvraham Stern 	}
318334e10860SSara Sharon }
318434e10860SSara Sharon 
3185e705c121SKalle Valo int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3186e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid)
3187e705c121SKalle Valo {
3188e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3189e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3190e705c121SKalle Valo 	u16 txq_id;
3191e705c121SKalle Valo 	int err;
3192e705c121SKalle Valo 
3193e705c121SKalle Valo 	/*
3194e705c121SKalle Valo 	 * If mac80211 is cleaning its state, then say that we finished since
3195e705c121SKalle Valo 	 * our state has been cleared anyway.
3196e705c121SKalle Valo 	 */
3197e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3198e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3199e705c121SKalle Valo 		return 0;
3200e705c121SKalle Valo 	}
3201e705c121SKalle Valo 
3202e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3203e705c121SKalle Valo 
3204e705c121SKalle Valo 	txq_id = tid_data->txq_id;
3205e705c121SKalle Valo 
3206e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
3207e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
3208e705c121SKalle Valo 
3209e705c121SKalle Valo 	mvmsta->agg_tids &= ~BIT(tid);
3210e705c121SKalle Valo 
32114b387906SAvraham Stern 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3212e705c121SKalle Valo 
3213e705c121SKalle Valo 	switch (tid_data->state) {
3214e705c121SKalle Valo 	case IWL_AGG_ON:
3215e705c121SKalle Valo 		tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3216e705c121SKalle Valo 
3217e705c121SKalle Valo 		IWL_DEBUG_TX_QUEUES(mvm,
3218e705c121SKalle Valo 				    "ssn = %d, next_recl = %d\n",
3219e705c121SKalle Valo 				    tid_data->ssn, tid_data->next_reclaimed);
3220e705c121SKalle Valo 
3221e705c121SKalle Valo 		tid_data->ssn = 0xffff;
3222e705c121SKalle Valo 		tid_data->state = IWL_AGG_OFF;
3223e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
3224e705c121SKalle Valo 
3225e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3226e705c121SKalle Valo 
3227e705c121SKalle Valo 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3228e705c121SKalle Valo 		return 0;
3229e705c121SKalle Valo 	case IWL_AGG_STARTING:
3230e705c121SKalle Valo 	case IWL_EMPTYING_HW_QUEUE_ADDBA:
3231e705c121SKalle Valo 		/*
3232e705c121SKalle Valo 		 * The agg session has been stopped before it was set up. This
3233e705c121SKalle Valo 		 * can happen when the AddBA timer times out for example.
3234e705c121SKalle Valo 		 */
3235e705c121SKalle Valo 
3236e705c121SKalle Valo 		/* No barriers since we are under mutex */
3237e705c121SKalle Valo 		lockdep_assert_held(&mvm->mutex);
3238e705c121SKalle Valo 
3239e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3240e705c121SKalle Valo 		tid_data->state = IWL_AGG_OFF;
3241e705c121SKalle Valo 		err = 0;
3242e705c121SKalle Valo 		break;
3243e705c121SKalle Valo 	default:
3244e705c121SKalle Valo 		IWL_ERR(mvm,
3245e705c121SKalle Valo 			"Stopping AGG while state not ON or starting for %d on %d (%d)\n",
3246e705c121SKalle Valo 			mvmsta->sta_id, tid, tid_data->state);
3247e705c121SKalle Valo 		IWL_ERR(mvm,
3248e705c121SKalle Valo 			"\ttid_data->txq_id = %d\n", tid_data->txq_id);
3249e705c121SKalle Valo 		err = -EINVAL;
3250e705c121SKalle Valo 	}
3251e705c121SKalle Valo 
3252e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3253e705c121SKalle Valo 
3254e705c121SKalle Valo 	return err;
3255e705c121SKalle Valo }
3256e705c121SKalle Valo 
3257e705c121SKalle Valo int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3258e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid)
3259e705c121SKalle Valo {
3260e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3261e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3262e705c121SKalle Valo 	u16 txq_id;
3263e705c121SKalle Valo 	enum iwl_mvm_agg_state old_state;
3264e705c121SKalle Valo 
3265e705c121SKalle Valo 	/*
3266e705c121SKalle Valo 	 * First set the agg state to OFF to avoid calling
3267e705c121SKalle Valo 	 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
3268e705c121SKalle Valo 	 */
3269e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3270e705c121SKalle Valo 	txq_id = tid_data->txq_id;
3271e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
3272e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
3273e705c121SKalle Valo 	old_state = tid_data->state;
3274e705c121SKalle Valo 	tid_data->state = IWL_AGG_OFF;
3275e705c121SKalle Valo 	mvmsta->agg_tids &= ~BIT(tid);
3276e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3277e705c121SKalle Valo 
32784b387906SAvraham Stern 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3279e705c121SKalle Valo 
3280e705c121SKalle Valo 	if (old_state >= IWL_AGG_ON) {
3281e705c121SKalle Valo 		iwl_mvm_drain_sta(mvm, mvmsta, true);
3282d167e81aSMordechai Goodstein 
3283d167e81aSMordechai Goodstein 		if (iwl_mvm_has_new_tx_api(mvm)) {
3284d167e81aSMordechai Goodstein 			if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
3285d4e3a341SMordechay Goodstein 						   BIT(tid)))
3286d167e81aSMordechai Goodstein 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3287d167e81aSMordechai Goodstein 			iwl_trans_wait_txq_empty(mvm->trans, txq_id);
3288d167e81aSMordechai Goodstein 		} else {
3289d4e3a341SMordechay Goodstein 			if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id)))
3290e705c121SKalle Valo 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
32910b90964aSSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
3292d167e81aSMordechai Goodstein 		}
3293d6d517b7SSara Sharon 
3294e705c121SKalle Valo 		iwl_mvm_drain_sta(mvm, mvmsta, false);
3295e705c121SKalle Valo 
3296e705c121SKalle Valo 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3297e705c121SKalle Valo 	}
3298e705c121SKalle Valo 
3299e705c121SKalle Valo 	return 0;
3300e705c121SKalle Valo }
3301e705c121SKalle Valo 
3302e705c121SKalle Valo static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
3303e705c121SKalle Valo {
3304e705c121SKalle Valo 	int i, max = -1, max_offs = -1;
3305e705c121SKalle Valo 
3306e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3307e705c121SKalle Valo 
3308e705c121SKalle Valo 	/* Pick the unused key offset with the highest 'deleted'
3309e705c121SKalle Valo 	 * counter. Every time a key is deleted, all the counters
3310e705c121SKalle Valo 	 * are incremented and the one that was just deleted is
3311e705c121SKalle Valo 	 * reset to zero. Thus, the highest counter is the one
3312e705c121SKalle Valo 	 * that was deleted longest ago. Pick that one.
3313e705c121SKalle Valo 	 */
3314e705c121SKalle Valo 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3315e705c121SKalle Valo 		if (test_bit(i, mvm->fw_key_table))
3316e705c121SKalle Valo 			continue;
3317e705c121SKalle Valo 		if (mvm->fw_key_deleted[i] > max) {
3318e705c121SKalle Valo 			max = mvm->fw_key_deleted[i];
3319e705c121SKalle Valo 			max_offs = i;
3320e705c121SKalle Valo 		}
3321e705c121SKalle Valo 	}
3322e705c121SKalle Valo 
3323e705c121SKalle Valo 	if (max_offs < 0)
3324e705c121SKalle Valo 		return STA_KEY_IDX_INVALID;
3325e705c121SKalle Valo 
3326e705c121SKalle Valo 	return max_offs;
3327e705c121SKalle Valo }
3328e705c121SKalle Valo 
33295f7a1847SJohannes Berg static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
33304615fd15SEmmanuel Grumbach 					       struct ieee80211_vif *vif,
3331e705c121SKalle Valo 					       struct ieee80211_sta *sta)
3332e705c121SKalle Valo {
3333e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3334e705c121SKalle Valo 
33355f7a1847SJohannes Berg 	if (sta)
33365f7a1847SJohannes Berg 		return iwl_mvm_sta_from_mac80211(sta);
3337e705c121SKalle Valo 
3338e705c121SKalle Valo 	/*
3339e705c121SKalle Valo 	 * The device expects GTKs for station interfaces to be
3340e705c121SKalle Valo 	 * installed as GTKs for the AP station. If we have no
3341e705c121SKalle Valo 	 * station ID, then use AP's station ID.
3342e705c121SKalle Valo 	 */
3343e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
33440ae98812SSara Sharon 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
33454615fd15SEmmanuel Grumbach 		u8 sta_id = mvmvif->ap_sta_id;
33464615fd15SEmmanuel Grumbach 
33477d6a1ab6SEmmanuel Grumbach 		sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
33487d6a1ab6SEmmanuel Grumbach 					    lockdep_is_held(&mvm->mutex));
33497d6a1ab6SEmmanuel Grumbach 
33504615fd15SEmmanuel Grumbach 		/*
33514615fd15SEmmanuel Grumbach 		 * It is possible that the 'sta' parameter is NULL,
33524615fd15SEmmanuel Grumbach 		 * for example when a GTK is removed - the sta_id will then
33534615fd15SEmmanuel Grumbach 		 * be the AP ID, and no station was passed by mac80211.
33544615fd15SEmmanuel Grumbach 		 */
33557d6a1ab6SEmmanuel Grumbach 		if (IS_ERR_OR_NULL(sta))
33567d6a1ab6SEmmanuel Grumbach 			return NULL;
33577d6a1ab6SEmmanuel Grumbach 
33587d6a1ab6SEmmanuel Grumbach 		return iwl_mvm_sta_from_mac80211(sta);
33594615fd15SEmmanuel Grumbach 	}
3360e705c121SKalle Valo 
33615f7a1847SJohannes Berg 	return NULL;
3362e705c121SKalle Valo }
3363e705c121SKalle Valo 
3364a6dfbd04SJohannes Berg static int iwl_mvm_pn_cmp(const u8 *pn1, const u8 *pn2, int len)
3365a6dfbd04SJohannes Berg {
3366a6dfbd04SJohannes Berg 	int i;
3367a6dfbd04SJohannes Berg 
3368a6dfbd04SJohannes Berg 	for (i = len - 1; i >= 0; i--) {
3369a6dfbd04SJohannes Berg 		if (pn1[i] > pn2[i])
3370a6dfbd04SJohannes Berg 			return 1;
3371a6dfbd04SJohannes Berg 		if (pn1[i] < pn2[i])
3372a6dfbd04SJohannes Berg 			return -1;
3373a6dfbd04SJohannes Berg 	}
3374a6dfbd04SJohannes Berg 
3375a6dfbd04SJohannes Berg 	return 0;
3376a6dfbd04SJohannes Berg }
3377a6dfbd04SJohannes Berg 
3378e705c121SKalle Valo static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
337985aeb58cSDavid Spinadel 				u32 sta_id,
338045c458b4SSara Sharon 				struct ieee80211_key_conf *key, bool mcast,
33814615fd15SEmmanuel Grumbach 				u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
33824883145aSEmmanuel Grumbach 				u8 key_offset, bool mfp)
3383e705c121SKalle Valo {
338445c458b4SSara Sharon 	union {
338545c458b4SSara Sharon 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
338645c458b4SSara Sharon 		struct iwl_mvm_add_sta_key_cmd cmd;
338745c458b4SSara Sharon 	} u = {};
3388e705c121SKalle Valo 	__le16 key_flags;
3389e705c121SKalle Valo 	int ret;
3390e705c121SKalle Valo 	u32 status;
3391e705c121SKalle Valo 	u16 keyidx;
339245c458b4SSara Sharon 	u64 pn = 0;
339345c458b4SSara Sharon 	int i, size;
339445c458b4SSara Sharon 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
339545c458b4SSara Sharon 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3396971cbe50SJohannes Berg 	int api_ver = iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA_KEY,
3397199d895fSJohannes Berg 					    new_api ? 2 : 1);
3398e705c121SKalle Valo 
339985aeb58cSDavid Spinadel 	if (sta_id == IWL_MVM_INVALID_STA)
340085aeb58cSDavid Spinadel 		return -EINVAL;
340185aeb58cSDavid Spinadel 
340245c458b4SSara Sharon 	keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
3403e705c121SKalle Valo 		 STA_KEY_FLG_KEYID_MSK;
3404e705c121SKalle Valo 	key_flags = cpu_to_le16(keyidx);
3405e705c121SKalle Valo 	key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3406e705c121SKalle Valo 
340745c458b4SSara Sharon 	switch (key->cipher) {
3408e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
3409e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
3410199d895fSJohannes Berg 		if (api_ver >= 2) {
341145c458b4SSara Sharon 			memcpy((void *)&u.cmd.tx_mic_key,
341245c458b4SSara Sharon 			       &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
341345c458b4SSara Sharon 			       IWL_MIC_KEY_SIZE);
341445c458b4SSara Sharon 
341545c458b4SSara Sharon 			memcpy((void *)&u.cmd.rx_mic_key,
341645c458b4SSara Sharon 			       &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
341745c458b4SSara Sharon 			       IWL_MIC_KEY_SIZE);
341845c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
341945c458b4SSara Sharon 
342045c458b4SSara Sharon 		} else {
342145c458b4SSara Sharon 			u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3422e705c121SKalle Valo 			for (i = 0; i < 5; i++)
342345c458b4SSara Sharon 				u.cmd_v1.tkip_rx_ttak[i] =
342445c458b4SSara Sharon 					cpu_to_le16(tkip_p1k[i]);
342545c458b4SSara Sharon 		}
342645c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3427e705c121SKalle Valo 		break;
3428e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
3429e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
343045c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3431199d895fSJohannes Berg 		if (api_ver >= 2)
343245c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
3433e705c121SKalle Valo 		break;
3434e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
3435e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
34365a2abdcaSGustavo A. R. Silva 		fallthrough;
3437e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
3438e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
343945c458b4SSara Sharon 		memcpy(u.cmd.common.key + 3, key->key, key->keylen);
3440e705c121SKalle Valo 		break;
34412a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
34422a53d166SAyala Beker 		key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
34435a2abdcaSGustavo A. R. Silva 		fallthrough;
34442a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
34452a53d166SAyala Beker 		key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
344645c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3447199d895fSJohannes Berg 		if (api_ver >= 2)
344845c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
34492a53d166SAyala Beker 		break;
3450e705c121SKalle Valo 	default:
3451e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
345245c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3453e705c121SKalle Valo 	}
3454e705c121SKalle Valo 
3455e705c121SKalle Valo 	if (mcast)
3456e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
34574883145aSEmmanuel Grumbach 	if (mfp)
34584883145aSEmmanuel Grumbach 		key_flags |= cpu_to_le16(STA_KEY_MFP);
3459e705c121SKalle Valo 
346045c458b4SSara Sharon 	u.cmd.common.key_offset = key_offset;
346145c458b4SSara Sharon 	u.cmd.common.key_flags = key_flags;
346285aeb58cSDavid Spinadel 	u.cmd.common.sta_id = sta_id;
346345c458b4SSara Sharon 
3464a6dfbd04SJohannes Berg 	if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
3465a6dfbd04SJohannes Berg 		i = 0;
3466a6dfbd04SJohannes Berg 	else
3467a6dfbd04SJohannes Berg 		i = -1;
3468a6dfbd04SJohannes Berg 
3469a6dfbd04SJohannes Berg 	for (; i < IEEE80211_NUM_TIDS; i++) {
3470a6dfbd04SJohannes Berg 		struct ieee80211_key_seq seq = {};
3471a6dfbd04SJohannes Berg 		u8 _rx_pn[IEEE80211_MAX_PN_LEN] = {}, *rx_pn = _rx_pn;
3472a6dfbd04SJohannes Berg 		int rx_pn_len = 8;
3473199d895fSJohannes Berg 		/* there's a hole at 2/3 in FW format depending on version */
3474199d895fSJohannes Berg 		int hole = api_ver >= 3 ? 0 : 2;
3475a6dfbd04SJohannes Berg 
3476a6dfbd04SJohannes Berg 		ieee80211_get_key_rx_seq(key, i, &seq);
3477a6dfbd04SJohannes Berg 
3478a6dfbd04SJohannes Berg 		if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
3479a6dfbd04SJohannes Berg 			rx_pn[0] = seq.tkip.iv16;
3480a6dfbd04SJohannes Berg 			rx_pn[1] = seq.tkip.iv16 >> 8;
3481199d895fSJohannes Berg 			rx_pn[2 + hole] = seq.tkip.iv32;
3482199d895fSJohannes Berg 			rx_pn[3 + hole] = seq.tkip.iv32 >> 8;
3483199d895fSJohannes Berg 			rx_pn[4 + hole] = seq.tkip.iv32 >> 16;
3484199d895fSJohannes Berg 			rx_pn[5 + hole] = seq.tkip.iv32 >> 24;
3485a6dfbd04SJohannes Berg 		} else if (key_flags & cpu_to_le16(STA_KEY_FLG_EXT)) {
3486a6dfbd04SJohannes Berg 			rx_pn = seq.hw.seq;
3487a6dfbd04SJohannes Berg 			rx_pn_len = seq.hw.seq_len;
3488a6dfbd04SJohannes Berg 		} else {
3489a6dfbd04SJohannes Berg 			rx_pn[0] = seq.ccmp.pn[0];
3490a6dfbd04SJohannes Berg 			rx_pn[1] = seq.ccmp.pn[1];
3491199d895fSJohannes Berg 			rx_pn[2 + hole] = seq.ccmp.pn[2];
3492199d895fSJohannes Berg 			rx_pn[3 + hole] = seq.ccmp.pn[3];
3493199d895fSJohannes Berg 			rx_pn[4 + hole] = seq.ccmp.pn[4];
3494199d895fSJohannes Berg 			rx_pn[5 + hole] = seq.ccmp.pn[5];
3495a6dfbd04SJohannes Berg 		}
3496a6dfbd04SJohannes Berg 
3497a6dfbd04SJohannes Berg 		if (iwl_mvm_pn_cmp(rx_pn, (u8 *)&u.cmd.common.rx_secur_seq_cnt,
3498a6dfbd04SJohannes Berg 				   rx_pn_len) > 0)
3499a6dfbd04SJohannes Berg 			memcpy(&u.cmd.common.rx_secur_seq_cnt, rx_pn,
3500a6dfbd04SJohannes Berg 			       rx_pn_len);
3501a6dfbd04SJohannes Berg 	}
3502a6dfbd04SJohannes Berg 
3503199d895fSJohannes Berg 	if (api_ver >= 2) {
350445c458b4SSara Sharon 		u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
350545c458b4SSara Sharon 		size = sizeof(u.cmd);
350645c458b4SSara Sharon 	} else {
350745c458b4SSara Sharon 		size = sizeof(u.cmd_v1);
350845c458b4SSara Sharon 	}
3509e705c121SKalle Valo 
3510e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
3511e705c121SKalle Valo 	if (cmd_flags & CMD_ASYNC)
351245c458b4SSara Sharon 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
351345c458b4SSara Sharon 					   &u.cmd);
3514e705c121SKalle Valo 	else
351545c458b4SSara Sharon 		ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
351645c458b4SSara Sharon 						  &u.cmd, &status);
3517e705c121SKalle Valo 
3518e705c121SKalle Valo 	switch (status) {
3519e705c121SKalle Valo 	case ADD_STA_SUCCESS:
3520e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3521e705c121SKalle Valo 		break;
3522e705c121SKalle Valo 	default:
3523e705c121SKalle Valo 		ret = -EIO;
3524e705c121SKalle Valo 		IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3525e705c121SKalle Valo 		break;
3526e705c121SKalle Valo 	}
3527e705c121SKalle Valo 
3528e705c121SKalle Valo 	return ret;
3529e705c121SKalle Valo }
3530e705c121SKalle Valo 
3531e705c121SKalle Valo static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3532e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
3533e705c121SKalle Valo 				 u8 sta_id, bool remove_key)
3534e705c121SKalle Valo {
3535e705c121SKalle Valo 	struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3536e705c121SKalle Valo 
3537e705c121SKalle Valo 	/* verify the key details match the required command's expectations */
35388e160ab8SAyala Beker 	if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3539b1fdc250SJohannes Berg 		    (keyconf->keyidx != 4 && keyconf->keyidx != 5 &&
3540b1fdc250SJohannes Berg 		     keyconf->keyidx != 6 && keyconf->keyidx != 7) ||
35418e160ab8SAyala Beker 		    (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
35428e160ab8SAyala Beker 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
35438e160ab8SAyala Beker 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
35448e160ab8SAyala Beker 		return -EINVAL;
35458e160ab8SAyala Beker 
35468e160ab8SAyala Beker 	if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
35478e160ab8SAyala Beker 		    keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3548e705c121SKalle Valo 		return -EINVAL;
3549e705c121SKalle Valo 
3550e705c121SKalle Valo 	igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3551e705c121SKalle Valo 	igtk_cmd.sta_id = cpu_to_le32(sta_id);
3552e705c121SKalle Valo 
3553e705c121SKalle Valo 	if (remove_key) {
3554197288d5SLuca Coelho 		/* This is a valid situation for IGTK */
3555197288d5SLuca Coelho 		if (sta_id == IWL_MVM_INVALID_STA)
3556197288d5SLuca Coelho 			return 0;
3557197288d5SLuca Coelho 
3558e705c121SKalle Valo 		igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3559e705c121SKalle Valo 	} else {
3560e705c121SKalle Valo 		struct ieee80211_key_seq seq;
3561e705c121SKalle Valo 		const u8 *pn;
3562e705c121SKalle Valo 
3563aa950524SAyala Beker 		switch (keyconf->cipher) {
3564aa950524SAyala Beker 		case WLAN_CIPHER_SUITE_AES_CMAC:
3565aa950524SAyala Beker 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3566aa950524SAyala Beker 			break;
35678e160ab8SAyala Beker 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
35688e160ab8SAyala Beker 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
35698e160ab8SAyala Beker 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
35708e160ab8SAyala Beker 			break;
3571aa950524SAyala Beker 		default:
3572aa950524SAyala Beker 			return -EINVAL;
3573aa950524SAyala Beker 		}
3574aa950524SAyala Beker 
35758e160ab8SAyala Beker 		memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
35768e160ab8SAyala Beker 		if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
35778e160ab8SAyala Beker 			igtk_cmd.ctrl_flags |=
35788e160ab8SAyala Beker 				cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3579e705c121SKalle Valo 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3580e705c121SKalle Valo 		pn = seq.aes_cmac.pn;
3581e705c121SKalle Valo 		igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3582e705c121SKalle Valo 						       ((u64) pn[4] << 8) |
3583e705c121SKalle Valo 						       ((u64) pn[3] << 16) |
3584e705c121SKalle Valo 						       ((u64) pn[2] << 24) |
3585e705c121SKalle Valo 						       ((u64) pn[1] << 32) |
3586e705c121SKalle Valo 						       ((u64) pn[0] << 40));
3587e705c121SKalle Valo 	}
3588e705c121SKalle Valo 
3589b1fdc250SJohannes Berg 	IWL_DEBUG_INFO(mvm, "%s %sIGTK (%d) for sta %u\n",
3590e705c121SKalle Valo 		       remove_key ? "removing" : "installing",
3591b1fdc250SJohannes Berg 		       keyconf->keyidx >= 6 ? "B" : "",
3592b1fdc250SJohannes Berg 		       keyconf->keyidx, igtk_cmd.sta_id);
3593e705c121SKalle Valo 
35948e160ab8SAyala Beker 	if (!iwl_mvm_has_new_rx_api(mvm)) {
35958e160ab8SAyala Beker 		struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
35968e160ab8SAyala Beker 			.ctrl_flags = igtk_cmd.ctrl_flags,
35978e160ab8SAyala Beker 			.key_id = igtk_cmd.key_id,
35988e160ab8SAyala Beker 			.sta_id = igtk_cmd.sta_id,
35998e160ab8SAyala Beker 			.receive_seq_cnt = igtk_cmd.receive_seq_cnt
36008e160ab8SAyala Beker 		};
36018e160ab8SAyala Beker 
36028e160ab8SAyala Beker 		memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
36038e160ab8SAyala Beker 		       ARRAY_SIZE(igtk_cmd_v1.igtk));
36048e160ab8SAyala Beker 		return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
36058e160ab8SAyala Beker 					    sizeof(igtk_cmd_v1), &igtk_cmd_v1);
36068e160ab8SAyala Beker 	}
3607e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3608e705c121SKalle Valo 				    sizeof(igtk_cmd), &igtk_cmd);
3609e705c121SKalle Valo }
3610e705c121SKalle Valo 
3611e705c121SKalle Valo 
3612e705c121SKalle Valo static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3613e705c121SKalle Valo 				       struct ieee80211_vif *vif,
3614e705c121SKalle Valo 				       struct ieee80211_sta *sta)
3615e705c121SKalle Valo {
3616e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3617e705c121SKalle Valo 
3618e705c121SKalle Valo 	if (sta)
3619e705c121SKalle Valo 		return sta->addr;
3620e705c121SKalle Valo 
3621e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
36220ae98812SSara Sharon 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3623e705c121SKalle Valo 		u8 sta_id = mvmvif->ap_sta_id;
3624e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3625e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
3626e705c121SKalle Valo 		return sta->addr;
3627e705c121SKalle Valo 	}
3628e705c121SKalle Valo 
3629e705c121SKalle Valo 
3630e705c121SKalle Valo 	return NULL;
3631e705c121SKalle Valo }
3632e705c121SKalle Valo 
3633e705c121SKalle Valo static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3634e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3635e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3636e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
36374615fd15SEmmanuel Grumbach 				 u8 key_offset,
3638e705c121SKalle Valo 				 bool mcast)
3639e705c121SKalle Valo {
3640e705c121SKalle Valo 	const u8 *addr;
3641e705c121SKalle Valo 	struct ieee80211_key_seq seq;
3642e705c121SKalle Valo 	u16 p1k[5];
364385aeb58cSDavid Spinadel 	u32 sta_id;
36444883145aSEmmanuel Grumbach 	bool mfp = false;
364585aeb58cSDavid Spinadel 
364685aeb58cSDavid Spinadel 	if (sta) {
364785aeb58cSDavid Spinadel 		struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
364885aeb58cSDavid Spinadel 
364985aeb58cSDavid Spinadel 		sta_id = mvm_sta->sta_id;
36504883145aSEmmanuel Grumbach 		mfp = sta->mfp;
365185aeb58cSDavid Spinadel 	} else if (vif->type == NL80211_IFTYPE_AP &&
365285aeb58cSDavid Spinadel 		   !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
365385aeb58cSDavid Spinadel 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
365485aeb58cSDavid Spinadel 
365585aeb58cSDavid Spinadel 		sta_id = mvmvif->mcast_sta.sta_id;
365685aeb58cSDavid Spinadel 	} else {
365785aeb58cSDavid Spinadel 		IWL_ERR(mvm, "Failed to find station id\n");
365885aeb58cSDavid Spinadel 		return -EINVAL;
365985aeb58cSDavid Spinadel 	}
3660e705c121SKalle Valo 
366195a62c33SJohannes Berg 	if (keyconf->cipher == WLAN_CIPHER_SUITE_TKIP) {
3662e705c121SKalle Valo 		addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3663e705c121SKalle Valo 		/* get phase 1 key from mac80211 */
3664e705c121SKalle Valo 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3665e705c121SKalle Valo 		ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
366695a62c33SJohannes Berg 
366795a62c33SJohannes Berg 		return iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
36684883145aSEmmanuel Grumbach 					    seq.tkip.iv32, p1k, 0, key_offset,
36694883145aSEmmanuel Grumbach 					    mfp);
3670e705c121SKalle Valo 	}
3671e705c121SKalle Valo 
367295a62c33SJohannes Berg 	return iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
367395a62c33SJohannes Berg 				    0, NULL, 0, key_offset, mfp);
3674e705c121SKalle Valo }
3675e705c121SKalle Valo 
3676e705c121SKalle Valo int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3677e705c121SKalle Valo 			struct ieee80211_vif *vif,
3678e705c121SKalle Valo 			struct ieee80211_sta *sta,
3679e705c121SKalle Valo 			struct ieee80211_key_conf *keyconf,
36804615fd15SEmmanuel Grumbach 			u8 key_offset)
3681e705c121SKalle Valo {
3682e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
36835f7a1847SJohannes Berg 	struct iwl_mvm_sta *mvm_sta;
368485aeb58cSDavid Spinadel 	u8 sta_id = IWL_MVM_INVALID_STA;
3685e705c121SKalle Valo 	int ret;
3686e705c121SKalle Valo 	static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3687e705c121SKalle Valo 
3688e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3689e705c121SKalle Valo 
369085aeb58cSDavid Spinadel 	if (vif->type != NL80211_IFTYPE_AP ||
369185aeb58cSDavid Spinadel 	    keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3692e705c121SKalle Valo 		/* Get the station id from the mvm local station table */
36935f7a1847SJohannes Berg 		mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
36945f7a1847SJohannes Berg 		if (!mvm_sta) {
36955f7a1847SJohannes Berg 			IWL_ERR(mvm, "Failed to find station\n");
3696e705c121SKalle Valo 			return -EINVAL;
3697e705c121SKalle Valo 		}
36985f7a1847SJohannes Berg 		sta_id = mvm_sta->sta_id;
3699e705c121SKalle Valo 
3700e705c121SKalle Valo 		/*
3701e705c121SKalle Valo 		 * It is possible that the 'sta' parameter is NULL, and thus
370285aeb58cSDavid Spinadel 		 * there is a need to retrieve the sta from the local station
370385aeb58cSDavid Spinadel 		 * table.
3704e705c121SKalle Valo 		 */
3705e705c121SKalle Valo 		if (!sta) {
370685aeb58cSDavid Spinadel 			sta = rcu_dereference_protected(
370785aeb58cSDavid Spinadel 				mvm->fw_id_to_mac_id[sta_id],
3708e705c121SKalle Valo 				lockdep_is_held(&mvm->mutex));
3709e705c121SKalle Valo 			if (IS_ERR_OR_NULL(sta)) {
3710e705c121SKalle Valo 				IWL_ERR(mvm, "Invalid station id\n");
3711e705c121SKalle Valo 				return -EINVAL;
3712e705c121SKalle Valo 			}
3713e705c121SKalle Valo 		}
3714e705c121SKalle Valo 
3715e705c121SKalle Valo 		if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3716e705c121SKalle Valo 			return -EINVAL;
3717e829b17cSBeni Lev 	} else {
3718e829b17cSBeni Lev 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3719e829b17cSBeni Lev 
3720e829b17cSBeni Lev 		sta_id = mvmvif->mcast_sta.sta_id;
3721e829b17cSBeni Lev 	}
3722e829b17cSBeni Lev 
3723e829b17cSBeni Lev 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3724e829b17cSBeni Lev 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3725e829b17cSBeni Lev 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3726e829b17cSBeni Lev 		ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3727e829b17cSBeni Lev 		goto end;
372885aeb58cSDavid Spinadel 	}
3729e705c121SKalle Valo 
37304615fd15SEmmanuel Grumbach 	/* If the key_offset is not pre-assigned, we need to find a
37314615fd15SEmmanuel Grumbach 	 * new offset to use.  In normal cases, the offset is not
37324615fd15SEmmanuel Grumbach 	 * pre-assigned, but during HW_RESTART we want to reuse the
37334615fd15SEmmanuel Grumbach 	 * same indices, so we pass them when this function is called.
37344615fd15SEmmanuel Grumbach 	 *
37354615fd15SEmmanuel Grumbach 	 * In D3 entry, we need to hardcoded the indices (because the
37364615fd15SEmmanuel Grumbach 	 * firmware hardcodes the PTK offset to 0).  In this case, we
37374615fd15SEmmanuel Grumbach 	 * need to make sure we don't overwrite the hw_key_idx in the
37384615fd15SEmmanuel Grumbach 	 * keyconf structure, because otherwise we cannot configure
37394615fd15SEmmanuel Grumbach 	 * the original ones back when resuming.
3740e705c121SKalle Valo 	 */
37414615fd15SEmmanuel Grumbach 	if (key_offset == STA_KEY_IDX_INVALID) {
37424615fd15SEmmanuel Grumbach 		key_offset  = iwl_mvm_set_fw_key_idx(mvm);
37434615fd15SEmmanuel Grumbach 		if (key_offset == STA_KEY_IDX_INVALID)
3744e705c121SKalle Valo 			return -ENOSPC;
37454615fd15SEmmanuel Grumbach 		keyconf->hw_key_idx = key_offset;
3746e705c121SKalle Valo 	}
3747e705c121SKalle Valo 
37484615fd15SEmmanuel Grumbach 	ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
37499c3deeb5SLuca Coelho 	if (ret)
3750e705c121SKalle Valo 		goto end;
3751e705c121SKalle Valo 
3752e705c121SKalle Valo 	/*
3753e705c121SKalle Valo 	 * For WEP, the same key is used for multicast and unicast. Upload it
3754e705c121SKalle Valo 	 * again, using the same key offset, and now pointing the other one
3755e705c121SKalle Valo 	 * to the same key slot (offset).
3756e705c121SKalle Valo 	 * If this fails, remove the original as well.
3757e705c121SKalle Valo 	 */
375885aeb58cSDavid Spinadel 	if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
375985aeb58cSDavid Spinadel 	     keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
376085aeb58cSDavid Spinadel 	    sta) {
37614615fd15SEmmanuel Grumbach 		ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
37624615fd15SEmmanuel Grumbach 					    key_offset, !mcast);
3763e705c121SKalle Valo 		if (ret) {
3764e705c121SKalle Valo 			__iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
37659c3deeb5SLuca Coelho 			goto end;
3766e705c121SKalle Valo 		}
3767e705c121SKalle Valo 	}
3768e705c121SKalle Valo 
37699c3deeb5SLuca Coelho 	__set_bit(key_offset, mvm->fw_key_table);
37709c3deeb5SLuca Coelho 
3771e705c121SKalle Valo end:
3772e705c121SKalle Valo 	IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3773e705c121SKalle Valo 		      keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3774e705c121SKalle Valo 		      sta ? sta->addr : zero_addr, ret);
3775e705c121SKalle Valo 	return ret;
3776e705c121SKalle Valo }
3777e705c121SKalle Valo 
3778e705c121SKalle Valo int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3779e705c121SKalle Valo 			   struct ieee80211_vif *vif,
3780e705c121SKalle Valo 			   struct ieee80211_sta *sta,
3781e705c121SKalle Valo 			   struct ieee80211_key_conf *keyconf)
3782e705c121SKalle Valo {
3783e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
37845f7a1847SJohannes Berg 	struct iwl_mvm_sta *mvm_sta;
37850ae98812SSara Sharon 	u8 sta_id = IWL_MVM_INVALID_STA;
3786e705c121SKalle Valo 	int ret, i;
3787e705c121SKalle Valo 
3788e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3789e705c121SKalle Valo 
37905f7a1847SJohannes Berg 	/* Get the station from the mvm local station table */
37915f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
379271793b7dSLuca Coelho 	if (mvm_sta)
3793cd4d23c1SIlan Peer 		sta_id = mvm_sta->sta_id;
379485aeb58cSDavid Spinadel 	else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
379585aeb58cSDavid Spinadel 		sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
379685aeb58cSDavid Spinadel 
3797e705c121SKalle Valo 
3798e705c121SKalle Valo 	IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3799e705c121SKalle Valo 		      keyconf->keyidx, sta_id);
3800e705c121SKalle Valo 
3801197288d5SLuca Coelho 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
38028e160ab8SAyala Beker 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3803197288d5SLuca Coelho 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3804e705c121SKalle Valo 		return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3805e705c121SKalle Valo 
3806e705c121SKalle Valo 	if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3807e705c121SKalle Valo 		IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3808e705c121SKalle Valo 			keyconf->hw_key_idx);
3809e705c121SKalle Valo 		return -ENOENT;
3810e705c121SKalle Valo 	}
3811e705c121SKalle Valo 
3812e705c121SKalle Valo 	/* track which key was deleted last */
3813e705c121SKalle Valo 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3814e705c121SKalle Valo 		if (mvm->fw_key_deleted[i] < U8_MAX)
3815e705c121SKalle Valo 			mvm->fw_key_deleted[i]++;
3816e705c121SKalle Valo 	}
3817e705c121SKalle Valo 	mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3818e705c121SKalle Valo 
381985aeb58cSDavid Spinadel 	if (sta && !mvm_sta) {
3820e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3821e705c121SKalle Valo 		return 0;
3822e705c121SKalle Valo 	}
3823e705c121SKalle Valo 
3824e705c121SKalle Valo 	ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3825e705c121SKalle Valo 	if (ret)
3826e705c121SKalle Valo 		return ret;
3827e705c121SKalle Valo 
3828e705c121SKalle Valo 	/* delete WEP key twice to get rid of (now useless) offset */
3829e705c121SKalle Valo 	if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3830e705c121SKalle Valo 	    keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3831e705c121SKalle Valo 		ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3832e705c121SKalle Valo 
3833e705c121SKalle Valo 	return ret;
3834e705c121SKalle Valo }
3835e705c121SKalle Valo 
3836e705c121SKalle Valo void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3837e705c121SKalle Valo 			     struct ieee80211_vif *vif,
3838e705c121SKalle Valo 			     struct ieee80211_key_conf *keyconf,
3839e705c121SKalle Valo 			     struct ieee80211_sta *sta, u32 iv32,
3840e705c121SKalle Valo 			     u16 *phase1key)
3841e705c121SKalle Valo {
3842e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta;
3843e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
38444883145aSEmmanuel Grumbach 	bool mfp = sta ? sta->mfp : false;
3845e705c121SKalle Valo 
3846e705c121SKalle Valo 	rcu_read_lock();
3847e705c121SKalle Valo 
38485f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
38495f7a1847SJohannes Berg 	if (WARN_ON_ONCE(!mvm_sta))
385012f17211SEmmanuel Grumbach 		goto unlock;
385185aeb58cSDavid Spinadel 	iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
38524883145aSEmmanuel Grumbach 			     iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx,
38534883145aSEmmanuel Grumbach 			     mfp);
385412f17211SEmmanuel Grumbach 
385512f17211SEmmanuel Grumbach  unlock:
3856e705c121SKalle Valo 	rcu_read_unlock();
3857e705c121SKalle Valo }
3858e705c121SKalle Valo 
3859e705c121SKalle Valo void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3860e705c121SKalle Valo 				struct ieee80211_sta *sta)
3861e705c121SKalle Valo {
3862e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3863e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3864e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3865e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3866e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_PS),
3867e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3868e705c121SKalle Valo 	};
3869e705c121SKalle Valo 	int ret;
3870e705c121SKalle Valo 
3871854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3872854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3873e705c121SKalle Valo 	if (ret)
3874e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3875e705c121SKalle Valo }
3876e705c121SKalle Valo 
3877e705c121SKalle Valo void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3878e705c121SKalle Valo 				       struct ieee80211_sta *sta,
3879e705c121SKalle Valo 				       enum ieee80211_frame_release_type reason,
3880e705c121SKalle Valo 				       u16 cnt, u16 tids, bool more_data,
38819a3fcf91SSara Sharon 				       bool single_sta_queue)
3882e705c121SKalle Valo {
3883e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3884e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3885e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3886e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3887e705c121SKalle Valo 		.modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3888e705c121SKalle Valo 		.sleep_tx_count = cpu_to_le16(cnt),
3889e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3890e705c121SKalle Valo 	};
3891e705c121SKalle Valo 	int tid, ret;
3892e705c121SKalle Valo 	unsigned long _tids = tids;
3893e705c121SKalle Valo 
3894e705c121SKalle Valo 	/* convert TIDs to ACs - we don't support TSPEC so that's OK
3895e705c121SKalle Valo 	 * Note that this field is reserved and unused by firmware not
3896e705c121SKalle Valo 	 * supporting GO uAPSD, so it's safe to always do this.
3897e705c121SKalle Valo 	 */
3898e705c121SKalle Valo 	for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3899e705c121SKalle Valo 		cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3900e705c121SKalle Valo 
39019a3fcf91SSara Sharon 	/* If we're releasing frames from aggregation or dqa queues then check
39029a3fcf91SSara Sharon 	 * if all the queues that we're releasing frames from, combined, have:
3903e705c121SKalle Valo 	 *  - more frames than the service period, in which case more_data
3904e705c121SKalle Valo 	 *    needs to be set
3905e705c121SKalle Valo 	 *  - fewer than 'cnt' frames, in which case we need to adjust the
3906e705c121SKalle Valo 	 *    firmware command (but do that unconditionally)
3907e705c121SKalle Valo 	 */
39089a3fcf91SSara Sharon 	if (single_sta_queue) {
3909e705c121SKalle Valo 		int remaining = cnt;
391036be0eb6SEmmanuel Grumbach 		int sleep_tx_count;
3911e705c121SKalle Valo 
3912e705c121SKalle Valo 		spin_lock_bh(&mvmsta->lock);
3913e705c121SKalle Valo 		for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3914e705c121SKalle Valo 			struct iwl_mvm_tid_data *tid_data;
3915e705c121SKalle Valo 			u16 n_queued;
3916e705c121SKalle Valo 
3917e705c121SKalle Valo 			tid_data = &mvmsta->tid_data[tid];
3918e705c121SKalle Valo 
3919dd32162dSLiad Kaufman 			n_queued = iwl_mvm_tid_queued(mvm, tid_data);
3920e705c121SKalle Valo 			if (n_queued > remaining) {
3921e705c121SKalle Valo 				more_data = true;
3922e705c121SKalle Valo 				remaining = 0;
3923e705c121SKalle Valo 				break;
3924e705c121SKalle Valo 			}
3925e705c121SKalle Valo 			remaining -= n_queued;
3926e705c121SKalle Valo 		}
392736be0eb6SEmmanuel Grumbach 		sleep_tx_count = cnt - remaining;
392836be0eb6SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
392936be0eb6SEmmanuel Grumbach 			mvmsta->sleep_tx_count = sleep_tx_count;
3930e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
3931e705c121SKalle Valo 
393236be0eb6SEmmanuel Grumbach 		cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3933e705c121SKalle Valo 		if (WARN_ON(cnt - remaining == 0)) {
3934e705c121SKalle Valo 			ieee80211_sta_eosp(sta);
3935e705c121SKalle Valo 			return;
3936e705c121SKalle Valo 		}
3937e705c121SKalle Valo 	}
3938e705c121SKalle Valo 
3939e705c121SKalle Valo 	/* Note: this is ignored by firmware not supporting GO uAPSD */
3940e705c121SKalle Valo 	if (more_data)
3941ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
3942e705c121SKalle Valo 
3943e705c121SKalle Valo 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3944e705c121SKalle Valo 		mvmsta->next_status_eosp = true;
3945ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
3946e705c121SKalle Valo 	} else {
3947ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
3948e705c121SKalle Valo 	}
3949e705c121SKalle Valo 
3950156f92f2SEmmanuel Grumbach 	/* block the Tx queues until the FW updated the sleep Tx count */
3951156f92f2SEmmanuel Grumbach 	iwl_trans_block_txq_ptrs(mvm->trans, true);
3952156f92f2SEmmanuel Grumbach 
3953156f92f2SEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3954156f92f2SEmmanuel Grumbach 				   CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3955854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3956e705c121SKalle Valo 	if (ret)
3957e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3958e705c121SKalle Valo }
3959e705c121SKalle Valo 
3960e705c121SKalle Valo void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3961e705c121SKalle Valo 			   struct iwl_rx_cmd_buffer *rxb)
3962e705c121SKalle Valo {
3963e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3964e705c121SKalle Valo 	struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3965e705c121SKalle Valo 	struct ieee80211_sta *sta;
3966e705c121SKalle Valo 	u32 sta_id = le32_to_cpu(notif->sta_id);
3967e705c121SKalle Valo 
3968be9ae34eSNathan Errera 	if (WARN_ON_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations))
3969e705c121SKalle Valo 		return;
3970e705c121SKalle Valo 
3971e705c121SKalle Valo 	rcu_read_lock();
3972e705c121SKalle Valo 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3973e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(sta))
3974e705c121SKalle Valo 		ieee80211_sta_eosp(sta);
3975e705c121SKalle Valo 	rcu_read_unlock();
3976e705c121SKalle Valo }
3977e705c121SKalle Valo 
3978e705c121SKalle Valo void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3979e705c121SKalle Valo 				   struct iwl_mvm_sta *mvmsta, bool disable)
3980e705c121SKalle Valo {
3981e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3982e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3983e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3984e705c121SKalle Valo 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3985e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3986e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3987e705c121SKalle Valo 	};
3988e705c121SKalle Valo 	int ret;
3989e705c121SKalle Valo 
3990854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3991854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3992e705c121SKalle Valo 	if (ret)
3993e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3994e705c121SKalle Valo }
3995e705c121SKalle Valo 
3996e705c121SKalle Valo void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3997e705c121SKalle Valo 				      struct ieee80211_sta *sta,
3998e705c121SKalle Valo 				      bool disable)
3999e705c121SKalle Valo {
4000e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4001e705c121SKalle Valo 
4002e705c121SKalle Valo 	spin_lock_bh(&mvm_sta->lock);
4003e705c121SKalle Valo 
4004e705c121SKalle Valo 	if (mvm_sta->disable_tx == disable) {
4005e705c121SKalle Valo 		spin_unlock_bh(&mvm_sta->lock);
4006e705c121SKalle Valo 		return;
4007e705c121SKalle Valo 	}
4008e705c121SKalle Valo 
4009e705c121SKalle Valo 	mvm_sta->disable_tx = disable;
4010e705c121SKalle Valo 
4011b1c6cec0SNaftali Goldstein 	/*
4012b1c6cec0SNaftali Goldstein 	 * If sta PS state is handled by mac80211, tell it to start/stop
4013b1c6cec0SNaftali Goldstein 	 * queuing tx for this station.
4014b1c6cec0SNaftali Goldstein 	 */
4015b1c6cec0SNaftali Goldstein 	if (!ieee80211_hw_check(mvm->hw, AP_LINK_PS))
4016e705c121SKalle Valo 		ieee80211_sta_block_awake(mvm->hw, sta, disable);
4017e705c121SKalle Valo 
4018e705c121SKalle Valo 	iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
4019e705c121SKalle Valo 
4020e705c121SKalle Valo 	spin_unlock_bh(&mvm_sta->lock);
4021e705c121SKalle Valo }
4022e705c121SKalle Valo 
4023ced19f26SSara Sharon static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
4024ced19f26SSara Sharon 					      struct iwl_mvm_vif *mvmvif,
4025ced19f26SSara Sharon 					      struct iwl_mvm_int_sta *sta,
4026ced19f26SSara Sharon 					      bool disable)
4027ced19f26SSara Sharon {
4028ced19f26SSara Sharon 	u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
4029ced19f26SSara Sharon 	struct iwl_mvm_add_sta_cmd cmd = {
4030ced19f26SSara Sharon 		.add_modify = STA_MODE_MODIFY,
4031ced19f26SSara Sharon 		.sta_id = sta->sta_id,
4032ced19f26SSara Sharon 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
4033ced19f26SSara Sharon 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
4034ced19f26SSara Sharon 		.mac_id_n_color = cpu_to_le32(id),
4035ced19f26SSara Sharon 	};
4036ced19f26SSara Sharon 	int ret;
4037ced19f26SSara Sharon 
4038e5d153ecSEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
4039ced19f26SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
4040ced19f26SSara Sharon 	if (ret)
4041ced19f26SSara Sharon 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
4042ced19f26SSara Sharon }
4043ced19f26SSara Sharon 
4044e705c121SKalle Valo void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
4045e705c121SKalle Valo 				       struct iwl_mvm_vif *mvmvif,
4046e705c121SKalle Valo 				       bool disable)
4047e705c121SKalle Valo {
4048e705c121SKalle Valo 	struct ieee80211_sta *sta;
4049e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta;
4050e705c121SKalle Valo 	int i;
4051e705c121SKalle Valo 
4052e5d153ecSEmmanuel Grumbach 	rcu_read_lock();
4053e705c121SKalle Valo 
4054e705c121SKalle Valo 	/* Block/unblock all the stations of the given mvmvif */
4055be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
4056e5d153ecSEmmanuel Grumbach 		sta = rcu_dereference(mvm->fw_id_to_mac_id[i]);
4057e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
4058e705c121SKalle Valo 			continue;
4059e705c121SKalle Valo 
4060e705c121SKalle Valo 		mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4061e705c121SKalle Valo 		if (mvm_sta->mac_id_n_color !=
4062e705c121SKalle Valo 		    FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
4063e705c121SKalle Valo 			continue;
4064e705c121SKalle Valo 
4065e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
4066e705c121SKalle Valo 	}
4067ced19f26SSara Sharon 
4068e5d153ecSEmmanuel Grumbach 	rcu_read_unlock();
4069e5d153ecSEmmanuel Grumbach 
4070ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
4071ced19f26SSara Sharon 		return;
4072ced19f26SSara Sharon 
4073ced19f26SSara Sharon 	/* Need to block/unblock also multicast station */
4074ced19f26SSara Sharon 	if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
4075ced19f26SSara Sharon 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
4076ced19f26SSara Sharon 						  &mvmvif->mcast_sta, disable);
4077ced19f26SSara Sharon 
4078ced19f26SSara Sharon 	/*
4079ced19f26SSara Sharon 	 * Only unblock the broadcast station (FW blocks it for immediate
4080ced19f26SSara Sharon 	 * quiet, not the driver)
4081ced19f26SSara Sharon 	 */
4082ced19f26SSara Sharon 	if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
4083ced19f26SSara Sharon 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
4084ced19f26SSara Sharon 						  &mvmvif->bcast_sta, disable);
4085e705c121SKalle Valo }
4086e705c121SKalle Valo 
4087e705c121SKalle Valo void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
4088e705c121SKalle Valo {
4089e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4090e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
4091e705c121SKalle Valo 
4092e705c121SKalle Valo 	rcu_read_lock();
4093e705c121SKalle Valo 
4094e705c121SKalle Valo 	mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
4095e705c121SKalle Valo 
4096ad12b231SNathan Errera 	if (mvmsta)
4097e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
4098e705c121SKalle Valo 
4099e705c121SKalle Valo 	rcu_read_unlock();
4100e705c121SKalle Valo }
4101dd32162dSLiad Kaufman 
4102dd32162dSLiad Kaufman u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
4103dd32162dSLiad Kaufman {
4104dd32162dSLiad Kaufman 	u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
4105dd32162dSLiad Kaufman 
4106dd32162dSLiad Kaufman 	/*
41072f7a3863SLuca Coelho 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
4108dd32162dSLiad Kaufman 	 * to align the wrap around of ssn so we compare relevant values.
4109dd32162dSLiad Kaufman 	 */
4110286ca8ebSLuca Coelho 	if (mvm->trans->trans_cfg->gen2)
4111dd32162dSLiad Kaufman 		sn &= 0xff;
4112dd32162dSLiad Kaufman 
4113dd32162dSLiad Kaufman 	return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
4114dd32162dSLiad Kaufman }
4115be82ecd3SAvraham Stern 
4116be82ecd3SAvraham Stern int iwl_mvm_add_pasn_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
4117be82ecd3SAvraham Stern 			 struct iwl_mvm_int_sta *sta, u8 *addr, u32 cipher,
4118be82ecd3SAvraham Stern 			 u8 *key, u32 key_len)
4119be82ecd3SAvraham Stern {
4120be82ecd3SAvraham Stern 	int ret;
4121be82ecd3SAvraham Stern 	u16 queue;
4122be82ecd3SAvraham Stern 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4123be82ecd3SAvraham Stern 	struct ieee80211_key_conf *keyconf;
4124be82ecd3SAvraham Stern 
4125be82ecd3SAvraham Stern 	ret = iwl_mvm_allocate_int_sta(mvm, sta, 0,
4126be82ecd3SAvraham Stern 				       NL80211_IFTYPE_UNSPECIFIED,
4127be82ecd3SAvraham Stern 				       IWL_STA_LINK);
4128be82ecd3SAvraham Stern 	if (ret)
4129be82ecd3SAvraham Stern 		return ret;
4130be82ecd3SAvraham Stern 
4131be82ecd3SAvraham Stern 	ret = iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
4132be82ecd3SAvraham Stern 					     addr, sta, &queue,
4133be82ecd3SAvraham Stern 					     IWL_MVM_TX_FIFO_BE);
4134be82ecd3SAvraham Stern 	if (ret)
4135be82ecd3SAvraham Stern 		goto out;
4136be82ecd3SAvraham Stern 
4137be82ecd3SAvraham Stern 	keyconf = kzalloc(sizeof(*keyconf) + key_len, GFP_KERNEL);
4138be82ecd3SAvraham Stern 	if (!keyconf) {
4139be82ecd3SAvraham Stern 		ret = -ENOBUFS;
4140be82ecd3SAvraham Stern 		goto out;
4141be82ecd3SAvraham Stern 	}
4142be82ecd3SAvraham Stern 
4143be82ecd3SAvraham Stern 	keyconf->cipher = cipher;
4144be82ecd3SAvraham Stern 	memcpy(keyconf->key, key, key_len);
4145be82ecd3SAvraham Stern 	keyconf->keylen = key_len;
4146be82ecd3SAvraham Stern 
4147be82ecd3SAvraham Stern 	ret = iwl_mvm_send_sta_key(mvm, sta->sta_id, keyconf, false,
4148be82ecd3SAvraham Stern 				   0, NULL, 0, 0, true);
4149be82ecd3SAvraham Stern 	kfree(keyconf);
4150be82ecd3SAvraham Stern 	return 0;
4151be82ecd3SAvraham Stern out:
4152be82ecd3SAvraham Stern 	iwl_mvm_dealloc_int_sta(mvm, sta);
4153be82ecd3SAvraham Stern 	return ret;
4154be82ecd3SAvraham Stern }
4155ad12b231SNathan Errera 
4156ad12b231SNathan Errera void iwl_mvm_cancel_channel_switch(struct iwl_mvm *mvm,
4157ad12b231SNathan Errera 				   struct ieee80211_vif *vif,
4158ad12b231SNathan Errera 				   u32 mac_id)
4159ad12b231SNathan Errera {
4160ad12b231SNathan Errera 	struct iwl_cancel_channel_switch_cmd cancel_channel_switch_cmd = {
4161ad12b231SNathan Errera 		.mac_id = cpu_to_le32(mac_id),
4162ad12b231SNathan Errera 	};
4163ad12b231SNathan Errera 	int ret;
4164ad12b231SNathan Errera 
4165ad12b231SNathan Errera 	ret = iwl_mvm_send_cmd_pdu(mvm,
4166f0c86427SJohannes Berg 				   WIDE_ID(MAC_CONF_GROUP, CANCEL_CHANNEL_SWITCH_CMD),
4167ad12b231SNathan Errera 				   CMD_ASYNC,
4168ad12b231SNathan Errera 				   sizeof(cancel_channel_switch_cmd),
4169ad12b231SNathan Errera 				   &cancel_channel_switch_cmd);
4170ad12b231SNathan Errera 	if (ret)
4171ad12b231SNathan Errera 		IWL_ERR(mvm, "Failed to cancel the channel switch\n");
4172ad12b231SNathan Errera }
4173