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 */
696cfbc6c4cSSara Sharon 	txq->stopped = true;
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 */
739cfbc6c4cSSara Sharon 	txq->stopped = false;
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 
769*09369983SJohannes Berg static int iwl_mvm_get_queue_size(struct ieee80211_sta *sta)
770*09369983SJohannes Berg {
771*09369983SJohannes Berg 	/* this queue isn't used for traffic (cab_queue) */
772*09369983SJohannes Berg 	if (!sta)
773*09369983SJohannes Berg 		return IWL_MGMT_QUEUE_SIZE;
774*09369983SJohannes Berg 
775*09369983SJohannes Berg 	/* support for 1k ba size */
776*09369983SJohannes Berg 	if (sta->deflink.eht_cap.has_eht)
777*09369983SJohannes Berg 		return IWL_DEFAULT_QUEUE_SIZE_EHT;
778*09369983SJohannes Berg 
779*09369983SJohannes Berg 	/* support for 256 ba size */
780*09369983SJohannes Berg 	if (sta->deflink.he_cap.has_he)
781*09369983SJohannes Berg 		return IWL_DEFAULT_QUEUE_SIZE_HE;
782*09369983SJohannes Berg 
783*09369983SJohannes Berg 	return IWL_DEFAULT_QUEUE_SIZE;
784*09369983SJohannes Berg }
785*09369983SJohannes Berg 
786cfbc6c4cSSara Sharon static int iwl_mvm_tvqm_enable_txq(struct iwl_mvm *mvm,
787*09369983SJohannes Berg 				   struct ieee80211_sta *sta,
78899448a8cSJohannes Berg 				   u8 sta_id, u8 tid, unsigned int timeout)
78999448a8cSJohannes Berg {
790d5399f11SMordechay Goodstein 	int queue, size;
79199448a8cSJohannes Berg 
79299448a8cSJohannes Berg 	if (tid == IWL_MAX_TID_COUNT) {
79399448a8cSJohannes Berg 		tid = IWL_MGMT_TID;
794ff911dcaSShaul Triebitz 		size = max_t(u32, IWL_MGMT_QUEUE_SIZE,
795ff911dcaSShaul Triebitz 			     mvm->trans->cfg->min_txq_size);
796d5399f11SMordechay Goodstein 	} else {
797*09369983SJohannes Berg 		size = iwl_mvm_get_queue_size(sta);
798d5399f11SMordechay Goodstein 	}
799d5399f11SMordechay Goodstein 
800d5399f11SMordechay Goodstein 	/* take the min with bc tbl entries allowed */
801d5399f11SMordechay Goodstein 	size = min_t(u32, size, mvm->trans->txqs.bc_tbl_size / sizeof(u16));
802d5399f11SMordechay Goodstein 
803d5399f11SMordechay Goodstein 	/* size needs to be power of 2 values for calculating read/write pointers */
804d5399f11SMordechay Goodstein 	size = rounddown_pow_of_two(size);
805d5399f11SMordechay Goodstein 
80692f78d4bSJohannes Berg 	do {
807227f2597SJohannes Berg 		queue = iwl_trans_txq_alloc(mvm->trans, 0, BIT(sta_id),
80885b17a33SJohannes Berg 					    tid, size, timeout);
80992f78d4bSJohannes Berg 
81092f78d4bSJohannes Berg 		if (queue < 0)
81199448a8cSJohannes Berg 			IWL_DEBUG_TX_QUEUES(mvm,
81292f78d4bSJohannes Berg 					    "Failed allocating TXQ of size %d for sta %d tid %d, ret: %d\n",
81392f78d4bSJohannes Berg 					    size, sta_id, tid, queue);
81492f78d4bSJohannes Berg 		size /= 2;
81592f78d4bSJohannes Berg 	} while (queue < 0 && size >= 16);
81692f78d4bSJohannes Berg 
81792f78d4bSJohannes Berg 	if (queue < 0)
81899448a8cSJohannes Berg 		return queue;
81999448a8cSJohannes Berg 
82099448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d for sta %d tid %d\n",
82199448a8cSJohannes Berg 			    queue, sta_id, tid);
82299448a8cSJohannes Berg 
82399448a8cSJohannes Berg 	return queue;
82499448a8cSJohannes Berg }
82599448a8cSJohannes Berg 
826310181ecSSara Sharon static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
827310181ecSSara Sharon 					struct ieee80211_sta *sta, u8 ac,
828310181ecSSara Sharon 					int tid)
829310181ecSSara Sharon {
830310181ecSSara Sharon 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
831cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq =
832cfbc6c4cSSara Sharon 		iwl_mvm_txq_from_tid(sta, tid);
833310181ecSSara Sharon 	unsigned int wdg_timeout =
834310181ecSSara Sharon 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
835310181ecSSara Sharon 	int queue = -1;
836310181ecSSara Sharon 
837310181ecSSara Sharon 	lockdep_assert_held(&mvm->mutex);
838310181ecSSara Sharon 
839310181ecSSara Sharon 	IWL_DEBUG_TX_QUEUES(mvm,
840310181ecSSara Sharon 			    "Allocating queue for sta %d on tid %d\n",
841310181ecSSara Sharon 			    mvmsta->sta_id, tid);
842*09369983SJohannes Berg 	queue = iwl_mvm_tvqm_enable_txq(mvm, sta, mvmsta->sta_id,
843*09369983SJohannes Berg 					tid, wdg_timeout);
844310181ecSSara Sharon 	if (queue < 0)
845310181ecSSara Sharon 		return queue;
846310181ecSSara Sharon 
847cfbc6c4cSSara Sharon 	mvmtxq->txq_id = queue;
848cfbc6c4cSSara Sharon 	mvm->tvqm_info[queue].txq_tid = tid;
849cfbc6c4cSSara Sharon 	mvm->tvqm_info[queue].sta_id = mvmsta->sta_id;
850cfbc6c4cSSara Sharon 
851310181ecSSara Sharon 	IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
852310181ecSSara Sharon 
853310181ecSSara Sharon 	spin_lock_bh(&mvmsta->lock);
854310181ecSSara Sharon 	mvmsta->tid_data[tid].txq_id = queue;
855310181ecSSara Sharon 	spin_unlock_bh(&mvmsta->lock);
856310181ecSSara Sharon 
857310181ecSSara Sharon 	return 0;
858310181ecSSara Sharon }
859310181ecSSara Sharon 
860cfbc6c4cSSara Sharon static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm,
861cfbc6c4cSSara Sharon 				       struct ieee80211_sta *sta,
862cfbc6c4cSSara Sharon 				       int queue, u8 sta_id, u8 tid)
86399448a8cSJohannes Berg {
86499448a8cSJohannes Berg 	bool enable_queue = true;
86599448a8cSJohannes Berg 
86699448a8cSJohannes Berg 	/* Make sure this TID isn't already enabled */
86799448a8cSJohannes Berg 	if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) {
86899448a8cSJohannes Berg 		IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n",
86999448a8cSJohannes Berg 			queue, tid);
87099448a8cSJohannes Berg 		return false;
87199448a8cSJohannes Berg 	}
87299448a8cSJohannes Berg 
87399448a8cSJohannes Berg 	/* Update mappings and refcounts */
8741c14089eSJohannes Berg 	if (mvm->queue_info[queue].tid_bitmap)
87599448a8cSJohannes Berg 		enable_queue = false;
87699448a8cSJohannes Berg 
87799448a8cSJohannes Berg 	mvm->queue_info[queue].tid_bitmap |= BIT(tid);
87899448a8cSJohannes Berg 	mvm->queue_info[queue].ra_sta_id = sta_id;
87999448a8cSJohannes Berg 
88099448a8cSJohannes Berg 	if (enable_queue) {
88199448a8cSJohannes Berg 		if (tid != IWL_MAX_TID_COUNT)
88299448a8cSJohannes Berg 			mvm->queue_info[queue].mac80211_ac =
88399448a8cSJohannes Berg 				tid_to_mac80211_ac[tid];
88499448a8cSJohannes Berg 		else
88599448a8cSJohannes Berg 			mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO;
88699448a8cSJohannes Berg 
88799448a8cSJohannes Berg 		mvm->queue_info[queue].txq_tid = tid;
88899448a8cSJohannes Berg 	}
88999448a8cSJohannes Berg 
890cfbc6c4cSSara Sharon 	if (sta) {
891cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
892cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_tid(sta, tid);
893cfbc6c4cSSara Sharon 
894cfbc6c4cSSara Sharon 		mvmtxq->txq_id = queue;
895cfbc6c4cSSara Sharon 	}
896cfbc6c4cSSara Sharon 
89799448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
898cfbc6c4cSSara Sharon 			    "Enabling TXQ #%d tids=0x%x\n",
899cfbc6c4cSSara Sharon 			    queue, mvm->queue_info[queue].tid_bitmap);
90099448a8cSJohannes Berg 
90199448a8cSJohannes Berg 	return enable_queue;
90299448a8cSJohannes Berg }
90399448a8cSJohannes Berg 
904cfbc6c4cSSara Sharon static bool iwl_mvm_enable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
905cfbc6c4cSSara Sharon 			       int queue, u16 ssn,
90699448a8cSJohannes Berg 			       const struct iwl_trans_txq_scd_cfg *cfg,
90799448a8cSJohannes Berg 			       unsigned int wdg_timeout)
90899448a8cSJohannes Berg {
90999448a8cSJohannes Berg 	struct iwl_scd_txq_cfg_cmd cmd = {
91099448a8cSJohannes Berg 		.scd_queue = queue,
91199448a8cSJohannes Berg 		.action = SCD_CFG_ENABLE_QUEUE,
91299448a8cSJohannes Berg 		.window = cfg->frame_limit,
91399448a8cSJohannes Berg 		.sta_id = cfg->sta_id,
91499448a8cSJohannes Berg 		.ssn = cpu_to_le16(ssn),
91599448a8cSJohannes Berg 		.tx_fifo = cfg->fifo,
91699448a8cSJohannes Berg 		.aggregate = cfg->aggregate,
91799448a8cSJohannes Berg 		.tid = cfg->tid,
91899448a8cSJohannes Berg 	};
91999448a8cSJohannes Berg 	bool inc_ssn;
92099448a8cSJohannes Berg 
92199448a8cSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
92299448a8cSJohannes Berg 		return false;
92399448a8cSJohannes Berg 
92499448a8cSJohannes Berg 	/* Send the enabling command if we need to */
925cfbc6c4cSSara Sharon 	if (!iwl_mvm_update_txq_mapping(mvm, sta, queue, cfg->sta_id, cfg->tid))
92699448a8cSJohannes Berg 		return false;
92799448a8cSJohannes Berg 
92899448a8cSJohannes Berg 	inc_ssn = iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn,
92999448a8cSJohannes Berg 					   NULL, wdg_timeout);
93099448a8cSJohannes Berg 	if (inc_ssn)
93199448a8cSJohannes Berg 		le16_add_cpu(&cmd.ssn, 1);
93299448a8cSJohannes Berg 
93399448a8cSJohannes Berg 	WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd),
93499448a8cSJohannes Berg 	     "Failed to configure queue %d on FIFO %d\n", queue, cfg->fifo);
93599448a8cSJohannes Berg 
93699448a8cSJohannes Berg 	return inc_ssn;
93799448a8cSJohannes Berg }
93899448a8cSJohannes Berg 
939b3a87f11SJohannes Berg static void iwl_mvm_change_queue_tid(struct iwl_mvm *mvm, int queue)
94019aefa45SLiad Kaufman {
94119aefa45SLiad Kaufman 	struct iwl_scd_txq_cfg_cmd cmd = {
94219aefa45SLiad Kaufman 		.scd_queue = queue,
94319aefa45SLiad Kaufman 		.action = SCD_CFG_UPDATE_QUEUE_TID,
94419aefa45SLiad Kaufman 	};
94519aefa45SLiad Kaufman 	int tid;
94619aefa45SLiad Kaufman 	unsigned long tid_bitmap;
94719aefa45SLiad Kaufman 	int ret;
94819aefa45SLiad Kaufman 
94919aefa45SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
95019aefa45SLiad Kaufman 
951bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
952bb49701bSSara Sharon 		return;
953bb49701bSSara Sharon 
95419aefa45SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
95519aefa45SLiad Kaufman 
95619aefa45SLiad Kaufman 	if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
95719aefa45SLiad Kaufman 		return;
95819aefa45SLiad Kaufman 
95919aefa45SLiad Kaufman 	/* Find any TID for queue */
96019aefa45SLiad Kaufman 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
96119aefa45SLiad Kaufman 	cmd.tid = tid;
96219aefa45SLiad Kaufman 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
96319aefa45SLiad Kaufman 
96419aefa45SLiad Kaufman 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
965341ca402SLiad Kaufman 	if (ret) {
96619aefa45SLiad Kaufman 		IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
96719aefa45SLiad Kaufman 			queue, ret);
968341ca402SLiad Kaufman 		return;
969341ca402SLiad Kaufman 	}
970341ca402SLiad Kaufman 
971341ca402SLiad Kaufman 	mvm->queue_info[queue].txq_tid = tid;
97219aefa45SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
97319aefa45SLiad Kaufman 			    queue, tid);
97419aefa45SLiad Kaufman }
97519aefa45SLiad Kaufman 
9769f9af3d7SLiad Kaufman static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
9779f9af3d7SLiad Kaufman {
9789f9af3d7SLiad Kaufman 	struct ieee80211_sta *sta;
9799f9af3d7SLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
980806911daSSharon Dvir 	u8 sta_id;
9819f9af3d7SLiad Kaufman 	int tid = -1;
9829f9af3d7SLiad Kaufman 	unsigned long tid_bitmap;
9839f9af3d7SLiad Kaufman 	unsigned int wdg_timeout;
9849f9af3d7SLiad Kaufman 	int ssn;
9859f9af3d7SLiad Kaufman 	int ret = true;
9869f9af3d7SLiad Kaufman 
987bb49701bSSara Sharon 	/* queue sharing is disabled on new TX path */
988bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
989bb49701bSSara Sharon 		return;
990bb49701bSSara Sharon 
9919f9af3d7SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
9929f9af3d7SLiad Kaufman 
9939f9af3d7SLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
9949f9af3d7SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
9959f9af3d7SLiad Kaufman 
9969f9af3d7SLiad Kaufman 	/* Find TID for queue, and make sure it is the only one on the queue */
9979f9af3d7SLiad Kaufman 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
9989f9af3d7SLiad Kaufman 	if (tid_bitmap != BIT(tid)) {
9999f9af3d7SLiad Kaufman 		IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
10009f9af3d7SLiad Kaufman 			queue, tid_bitmap);
10019f9af3d7SLiad Kaufman 		return;
10029f9af3d7SLiad Kaufman 	}
10039f9af3d7SLiad Kaufman 
10049f9af3d7SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
10059f9af3d7SLiad Kaufman 			    tid);
10069f9af3d7SLiad Kaufman 
10079f9af3d7SLiad Kaufman 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
10089f9af3d7SLiad Kaufman 					lockdep_is_held(&mvm->mutex));
10099f9af3d7SLiad Kaufman 
10109f9af3d7SLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
10119f9af3d7SLiad Kaufman 		return;
10129f9af3d7SLiad Kaufman 
10139f9af3d7SLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
10149f9af3d7SLiad Kaufman 	wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
10159f9af3d7SLiad Kaufman 
10169f9af3d7SLiad Kaufman 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
10179f9af3d7SLiad Kaufman 
1018cfbc6c4cSSara Sharon 	ret = iwl_mvm_redirect_queue(mvm, queue, tid,
10199f9af3d7SLiad Kaufman 				     tid_to_mac80211_ac[tid], ssn,
1020cfbc6c4cSSara Sharon 				     wdg_timeout, true,
1021cfbc6c4cSSara Sharon 				     iwl_mvm_txq_from_tid(sta, tid));
10229f9af3d7SLiad Kaufman 	if (ret) {
10239f9af3d7SLiad Kaufman 		IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
10249f9af3d7SLiad Kaufman 		return;
10259f9af3d7SLiad Kaufman 	}
10269f9af3d7SLiad Kaufman 
10279f9af3d7SLiad Kaufman 	/* If aggs should be turned back on - do it */
10289f9af3d7SLiad Kaufman 	if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
10299cd70e80SEmmanuel Grumbach 		struct iwl_mvm_add_sta_cmd cmd = {0};
10309f9af3d7SLiad Kaufman 
10319f9af3d7SLiad Kaufman 		mvmsta->tid_disable_agg &= ~BIT(tid);
10329f9af3d7SLiad Kaufman 
10339f9af3d7SLiad Kaufman 		cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
10349f9af3d7SLiad Kaufman 		cmd.sta_id = mvmsta->sta_id;
10359f9af3d7SLiad Kaufman 		cmd.add_modify = STA_MODE_MODIFY;
10369f9af3d7SLiad Kaufman 		cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
10379f9af3d7SLiad Kaufman 		cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
10389f9af3d7SLiad Kaufman 		cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
10399f9af3d7SLiad Kaufman 
10409f9af3d7SLiad Kaufman 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
10419f9af3d7SLiad Kaufman 					   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
10429f9af3d7SLiad Kaufman 		if (!ret) {
10439f9af3d7SLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm,
10449f9af3d7SLiad Kaufman 					    "TXQ #%d is now aggregated again\n",
10459f9af3d7SLiad Kaufman 					    queue);
10469f9af3d7SLiad Kaufman 
10479f9af3d7SLiad Kaufman 			/* Mark queue intenally as aggregating again */
10489f9af3d7SLiad Kaufman 			iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
10499f9af3d7SLiad Kaufman 		}
10509f9af3d7SLiad Kaufman 	}
10519f9af3d7SLiad Kaufman 
10529f9af3d7SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
10539f9af3d7SLiad Kaufman }
10549f9af3d7SLiad Kaufman 
105599448a8cSJohannes Berg /*
105699448a8cSJohannes Berg  * Remove inactive TIDs of a given queue.
105799448a8cSJohannes Berg  * If all queue TIDs are inactive - mark the queue as inactive
105899448a8cSJohannes Berg  * If only some the queue TIDs are inactive - unmap them from the queue
1059724fe771SJohannes Berg  *
1060724fe771SJohannes Berg  * Returns %true if all TIDs were removed and the queue could be reused.
106199448a8cSJohannes Berg  */
1062724fe771SJohannes Berg static bool iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
106399448a8cSJohannes Berg 					 struct iwl_mvm_sta *mvmsta, int queue,
106490d2d94cSJohannes Berg 					 unsigned long tid_bitmap,
1065b3a87f11SJohannes Berg 					 unsigned long *unshare_queues,
1066b3a87f11SJohannes Berg 					 unsigned long *changetid_queues)
106799448a8cSJohannes Berg {
1068af3cdfd3SJakub Kicinski 	unsigned int tid;
106999448a8cSJohannes Berg 
107099448a8cSJohannes Berg 	lockdep_assert_held(&mvmsta->lock);
1071f3f240f9SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
107299448a8cSJohannes Berg 
107399448a8cSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1074724fe771SJohannes Berg 		return false;
107599448a8cSJohannes Berg 
107699448a8cSJohannes Berg 	/* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
107799448a8cSJohannes Berg 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
107899448a8cSJohannes Berg 		/* If some TFDs are still queued - don't mark TID as inactive */
107999448a8cSJohannes Berg 		if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid]))
108099448a8cSJohannes Berg 			tid_bitmap &= ~BIT(tid);
108199448a8cSJohannes Berg 
108299448a8cSJohannes Berg 		/* Don't mark as inactive any TID that has an active BA */
108399448a8cSJohannes Berg 		if (mvmsta->tid_data[tid].state != IWL_AGG_OFF)
108499448a8cSJohannes Berg 			tid_bitmap &= ~BIT(tid);
108599448a8cSJohannes Berg 	}
108699448a8cSJohannes Berg 
1087724fe771SJohannes Berg 	/* If all TIDs in the queue are inactive - return it can be reused */
108899448a8cSJohannes Berg 	if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
1089724fe771SJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Queue %d is inactive\n", queue);
1090724fe771SJohannes Berg 		return true;
109199448a8cSJohannes Berg 	}
109299448a8cSJohannes Berg 
109399448a8cSJohannes Berg 	/*
109499448a8cSJohannes Berg 	 * If we are here, this is a shared queue and not all TIDs timed-out.
109599448a8cSJohannes Berg 	 * Remove the ones that did.
109699448a8cSJohannes Berg 	 */
109799448a8cSJohannes Berg 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1098d4530f63SJohannes Berg 		u16 q_tid_bitmap;
109999448a8cSJohannes Berg 
110099448a8cSJohannes Berg 		mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
110199448a8cSJohannes Berg 		mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
110299448a8cSJohannes Berg 
1103d4530f63SJohannes Berg 		q_tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1104b3a87f11SJohannes Berg 
1105b3a87f11SJohannes Berg 		/*
1106b3a87f11SJohannes Berg 		 * We need to take into account a situation in which a TXQ was
1107b3a87f11SJohannes Berg 		 * allocated to TID x, and then turned shared by adding TIDs y
1108b3a87f11SJohannes Berg 		 * and z. If TID x becomes inactive and is removed from the TXQ,
1109b3a87f11SJohannes Berg 		 * ownership must be given to one of the remaining TIDs.
1110b3a87f11SJohannes Berg 		 * This is mainly because if TID x continues - a new queue can't
1111b3a87f11SJohannes Berg 		 * be allocated for it as long as it is an owner of another TXQ.
1112b3a87f11SJohannes Berg 		 *
1113b3a87f11SJohannes Berg 		 * Mark this queue in the right bitmap, we'll send the command
1114b3a87f11SJohannes Berg 		 * to the firmware later.
1115b3a87f11SJohannes Berg 		 */
1116d4530f63SJohannes Berg 		if (!(q_tid_bitmap & BIT(mvm->queue_info[queue].txq_tid)))
1117b3a87f11SJohannes Berg 			set_bit(queue, changetid_queues);
1118b3a87f11SJohannes Berg 
111999448a8cSJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm,
112099448a8cSJohannes Berg 				    "Removing inactive TID %d from shared Q:%d\n",
112199448a8cSJohannes Berg 				    tid, queue);
112299448a8cSJohannes Berg 	}
112399448a8cSJohannes Berg 
112499448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
112599448a8cSJohannes Berg 			    "TXQ #%d left with tid bitmap 0x%x\n", queue,
112699448a8cSJohannes Berg 			    mvm->queue_info[queue].tid_bitmap);
112799448a8cSJohannes Berg 
112899448a8cSJohannes Berg 	/*
112999448a8cSJohannes Berg 	 * There may be different TIDs with the same mac queues, so make
113099448a8cSJohannes Berg 	 * sure all TIDs have existing corresponding mac queues enabled
113199448a8cSJohannes Berg 	 */
113299448a8cSJohannes Berg 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
113399448a8cSJohannes Berg 
113499448a8cSJohannes Berg 	/* If the queue is marked as shared - "unshare" it */
11351c14089eSJohannes Berg 	if (hweight16(mvm->queue_info[queue].tid_bitmap) == 1 &&
113699448a8cSJohannes Berg 	    mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
113799448a8cSJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
113899448a8cSJohannes Berg 				    queue);
113990d2d94cSJohannes Berg 		set_bit(queue, unshare_queues);
114099448a8cSJohannes Berg 	}
1141724fe771SJohannes Berg 
1142724fe771SJohannes Berg 	return false;
114399448a8cSJohannes Berg }
114499448a8cSJohannes Berg 
1145724fe771SJohannes Berg /*
1146724fe771SJohannes Berg  * Check for inactivity - this includes checking if any queue
1147724fe771SJohannes Berg  * can be unshared and finding one (and only one) that can be
1148724fe771SJohannes Berg  * reused.
1149724fe771SJohannes Berg  * This function is also invoked as a sort of clean-up task,
1150724fe771SJohannes Berg  * in which case @alloc_for_sta is IWL_MVM_INVALID_STA.
1151724fe771SJohannes Berg  *
1152724fe771SJohannes Berg  * Returns the queue number, or -ENOSPC.
1153724fe771SJohannes Berg  */
1154724fe771SJohannes Berg static int iwl_mvm_inactivity_check(struct iwl_mvm *mvm, u8 alloc_for_sta)
115599448a8cSJohannes Berg {
115699448a8cSJohannes Berg 	unsigned long now = jiffies;
115790d2d94cSJohannes Berg 	unsigned long unshare_queues = 0;
1158b3a87f11SJohannes Berg 	unsigned long changetid_queues = 0;
1159724fe771SJohannes Berg 	int i, ret, free_queue = -ENOSPC;
1160cfbc6c4cSSara Sharon 	struct ieee80211_sta *queue_owner  = NULL;
116199448a8cSJohannes Berg 
1162df2a2245SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
1163df2a2245SJohannes Berg 
116499448a8cSJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm))
1165724fe771SJohannes Berg 		return -ENOSPC;
116699448a8cSJohannes Berg 
116799448a8cSJohannes Berg 	rcu_read_lock();
116899448a8cSJohannes Berg 
1169459ab045SJohannes Berg 	/* we skip the CMD queue below by starting at 1 */
1170459ab045SJohannes Berg 	BUILD_BUG_ON(IWL_MVM_DQA_CMD_QUEUE != 0);
1171459ab045SJohannes Berg 
1172459ab045SJohannes Berg 	for (i = 1; i < IWL_MAX_HW_QUEUES; i++) {
117399448a8cSJohannes Berg 		struct ieee80211_sta *sta;
117499448a8cSJohannes Berg 		struct iwl_mvm_sta *mvmsta;
117599448a8cSJohannes Berg 		u8 sta_id;
117699448a8cSJohannes Berg 		int tid;
117799448a8cSJohannes Berg 		unsigned long inactive_tid_bitmap = 0;
117899448a8cSJohannes Berg 		unsigned long queue_tid_bitmap;
117999448a8cSJohannes Berg 
118099448a8cSJohannes Berg 		queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
1181459ab045SJohannes Berg 		if (!queue_tid_bitmap)
1182459ab045SJohannes Berg 			continue;
118399448a8cSJohannes Berg 
118499448a8cSJohannes Berg 		/* If TXQ isn't in active use anyway - nothing to do here... */
118599448a8cSJohannes Berg 		if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
1186459ab045SJohannes Berg 		    mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED)
118799448a8cSJohannes Berg 			continue;
118899448a8cSJohannes Berg 
118999448a8cSJohannes Berg 		/* Check to see if there are inactive TIDs on this queue */
119099448a8cSJohannes Berg 		for_each_set_bit(tid, &queue_tid_bitmap,
119199448a8cSJohannes Berg 				 IWL_MAX_TID_COUNT + 1) {
119299448a8cSJohannes Berg 			if (time_after(mvm->queue_info[i].last_frame_time[tid] +
119399448a8cSJohannes Berg 				       IWL_MVM_DQA_QUEUE_TIMEOUT, now))
119499448a8cSJohannes Berg 				continue;
119599448a8cSJohannes Berg 
119699448a8cSJohannes Berg 			inactive_tid_bitmap |= BIT(tid);
119799448a8cSJohannes Berg 		}
119899448a8cSJohannes Berg 
119999448a8cSJohannes Berg 		/* If all TIDs are active - finish check on this queue */
120099448a8cSJohannes Berg 		if (!inactive_tid_bitmap)
120199448a8cSJohannes Berg 			continue;
120299448a8cSJohannes Berg 
120399448a8cSJohannes Berg 		/*
120499448a8cSJohannes Berg 		 * If we are here - the queue hadn't been served recently and is
120599448a8cSJohannes Berg 		 * in use
120699448a8cSJohannes Berg 		 */
120799448a8cSJohannes Berg 
120899448a8cSJohannes Berg 		sta_id = mvm->queue_info[i].ra_sta_id;
120999448a8cSJohannes Berg 		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
121099448a8cSJohannes Berg 
121199448a8cSJohannes Berg 		/*
121299448a8cSJohannes Berg 		 * If the STA doesn't exist anymore, it isn't an error. It could
121399448a8cSJohannes Berg 		 * be that it was removed since getting the queues, and in this
121499448a8cSJohannes Berg 		 * case it should've inactivated its queues anyway.
121599448a8cSJohannes Berg 		 */
121699448a8cSJohannes Berg 		if (IS_ERR_OR_NULL(sta))
121799448a8cSJohannes Berg 			continue;
121899448a8cSJohannes Berg 
121999448a8cSJohannes Berg 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
122099448a8cSJohannes Berg 
1221f3f240f9SJohannes Berg 		spin_lock_bh(&mvmsta->lock);
1222724fe771SJohannes Berg 		ret = iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
122390d2d94cSJohannes Berg 						   inactive_tid_bitmap,
1224b3a87f11SJohannes Berg 						   &unshare_queues,
1225b3a87f11SJohannes Berg 						   &changetid_queues);
1226e6d419f9SJohannes Berg 		if (ret && free_queue < 0) {
1227cfbc6c4cSSara Sharon 			queue_owner = sta;
1228e6d419f9SJohannes Berg 			free_queue = i;
1229cfbc6c4cSSara Sharon 		}
1230459ab045SJohannes Berg 		/* only unlock sta lock - we still need the queue info lock */
1231f3f240f9SJohannes Berg 		spin_unlock_bh(&mvmsta->lock);
123299448a8cSJohannes Berg 	}
123399448a8cSJohannes Berg 
1234df2a2245SJohannes Berg 
1235df2a2245SJohannes Berg 	/* Reconfigure queues requiring reconfiguation */
123690d2d94cSJohannes Berg 	for_each_set_bit(i, &unshare_queues, IWL_MAX_HW_QUEUES)
123790d2d94cSJohannes Berg 		iwl_mvm_unshare_queue(mvm, i);
1238b3a87f11SJohannes Berg 	for_each_set_bit(i, &changetid_queues, IWL_MAX_HW_QUEUES)
1239b3a87f11SJohannes Berg 		iwl_mvm_change_queue_tid(mvm, i);
1240724fe771SJohannes Berg 
1241fbb1461aSJohannes Berg 	rcu_read_unlock();
1242fbb1461aSJohannes Berg 
1243724fe771SJohannes Berg 	if (free_queue >= 0 && alloc_for_sta != IWL_MVM_INVALID_STA) {
1244cfbc6c4cSSara Sharon 		ret = iwl_mvm_free_inactive_queue(mvm, free_queue, queue_owner,
1245724fe771SJohannes Berg 						  alloc_for_sta);
1246fbb1461aSJohannes Berg 		if (ret)
1247724fe771SJohannes Berg 			return ret;
1248724fe771SJohannes Berg 	}
1249724fe771SJohannes Berg 
1250724fe771SJohannes Berg 	return free_queue;
125199448a8cSJohannes Berg }
125299448a8cSJohannes Berg 
1253c20e08b0SJohannes Berg static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
1254cfbc6c4cSSara Sharon 				   struct ieee80211_sta *sta, u8 ac, int tid)
1255c20e08b0SJohannes Berg {
1256c20e08b0SJohannes Berg 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1257c20e08b0SJohannes Berg 	struct iwl_trans_txq_scd_cfg cfg = {
1258c20e08b0SJohannes Berg 		.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
1259c20e08b0SJohannes Berg 		.sta_id = mvmsta->sta_id,
1260c20e08b0SJohannes Berg 		.tid = tid,
1261c20e08b0SJohannes Berg 		.frame_limit = IWL_FRAME_LIMIT,
1262c20e08b0SJohannes Berg 	};
1263c20e08b0SJohannes Berg 	unsigned int wdg_timeout =
1264c20e08b0SJohannes Berg 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1265c20e08b0SJohannes Berg 	int queue = -1;
1266c6ce1c74SJohannes Berg 	u16 queue_tmp;
1267c20e08b0SJohannes Berg 	unsigned long disable_agg_tids = 0;
1268c20e08b0SJohannes Berg 	enum iwl_mvm_agg_state queue_state;
1269c20e08b0SJohannes Berg 	bool shared_queue = false, inc_ssn;
1270c20e08b0SJohannes Berg 	int ssn;
1271c20e08b0SJohannes Berg 	unsigned long tfd_queue_mask;
1272c20e08b0SJohannes Berg 	int ret;
1273c20e08b0SJohannes Berg 
1274c20e08b0SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
1275c20e08b0SJohannes Berg 
1276c20e08b0SJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm))
1277c20e08b0SJohannes Berg 		return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
1278c20e08b0SJohannes Berg 
1279c20e08b0SJohannes Berg 	spin_lock_bh(&mvmsta->lock);
1280c20e08b0SJohannes Berg 	tfd_queue_mask = mvmsta->tfd_queue_msk;
128135739348SSara Sharon 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1282c20e08b0SJohannes Berg 	spin_unlock_bh(&mvmsta->lock);
1283c20e08b0SJohannes Berg 
1284cfbc6c4cSSara Sharon 	if (tid == IWL_MAX_TID_COUNT) {
1285c20e08b0SJohannes Berg 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1286c20e08b0SJohannes Berg 						IWL_MVM_DQA_MIN_MGMT_QUEUE,
1287c20e08b0SJohannes Berg 						IWL_MVM_DQA_MAX_MGMT_QUEUE);
1288c20e08b0SJohannes Berg 		if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
1289c20e08b0SJohannes Berg 			IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
1290c20e08b0SJohannes Berg 					    queue);
1291c20e08b0SJohannes Berg 
1292c20e08b0SJohannes Berg 		/* If no such queue is found, we'll use a DATA queue instead */
1293c20e08b0SJohannes Berg 	}
1294c20e08b0SJohannes Berg 
1295c20e08b0SJohannes Berg 	if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
1296c20e08b0SJohannes Berg 	    (mvm->queue_info[mvmsta->reserved_queue].status ==
1297724fe771SJohannes Berg 			IWL_MVM_QUEUE_RESERVED)) {
1298c20e08b0SJohannes Berg 		queue = mvmsta->reserved_queue;
1299c20e08b0SJohannes Berg 		mvm->queue_info[queue].reserved = true;
1300c20e08b0SJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
1301c20e08b0SJohannes Berg 	}
1302c20e08b0SJohannes Berg 
1303c20e08b0SJohannes Berg 	if (queue < 0)
1304c20e08b0SJohannes Berg 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1305c20e08b0SJohannes Berg 						IWL_MVM_DQA_MIN_DATA_QUEUE,
1306c20e08b0SJohannes Berg 						IWL_MVM_DQA_MAX_DATA_QUEUE);
1307724fe771SJohannes Berg 	if (queue < 0) {
1308724fe771SJohannes Berg 		/* try harder - perhaps kill an inactive queue */
1309724fe771SJohannes Berg 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1310c20e08b0SJohannes Berg 	}
1311c20e08b0SJohannes Berg 
1312c20e08b0SJohannes Berg 	/* No free queue - we'll have to share */
1313c20e08b0SJohannes Berg 	if (queue <= 0) {
1314c20e08b0SJohannes Berg 		queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
1315c20e08b0SJohannes Berg 		if (queue > 0) {
1316c20e08b0SJohannes Berg 			shared_queue = true;
1317c20e08b0SJohannes Berg 			mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
1318c20e08b0SJohannes Berg 		}
1319c20e08b0SJohannes Berg 	}
1320c20e08b0SJohannes Berg 
1321c20e08b0SJohannes Berg 	/*
1322c20e08b0SJohannes Berg 	 * Mark TXQ as ready, even though it hasn't been fully configured yet,
1323c20e08b0SJohannes Berg 	 * to make sure no one else takes it.
1324c20e08b0SJohannes Berg 	 * This will allow avoiding re-acquiring the lock at the end of the
1325c20e08b0SJohannes Berg 	 * configuration. On error we'll mark it back as free.
1326c20e08b0SJohannes Berg 	 */
1327c20e08b0SJohannes Berg 	if (queue > 0 && !shared_queue)
1328c20e08b0SJohannes Berg 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1329c20e08b0SJohannes Berg 
1330c20e08b0SJohannes Berg 	/* This shouldn't happen - out of queues */
1331c20e08b0SJohannes Berg 	if (WARN_ON(queue <= 0)) {
1332c20e08b0SJohannes Berg 		IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
1333c20e08b0SJohannes Berg 			tid, cfg.sta_id);
1334c20e08b0SJohannes Berg 		return queue;
1335c20e08b0SJohannes Berg 	}
1336c20e08b0SJohannes Berg 
1337c20e08b0SJohannes Berg 	/*
1338c20e08b0SJohannes Berg 	 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
1339c20e08b0SJohannes Berg 	 * but for configuring the SCD to send A-MPDUs we need to mark the queue
1340c20e08b0SJohannes Berg 	 * as aggregatable.
1341c20e08b0SJohannes Berg 	 * Mark all DATA queues as allowing to be aggregated at some point
1342c20e08b0SJohannes Berg 	 */
1343c20e08b0SJohannes Berg 	cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1344c20e08b0SJohannes Berg 			 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1345c20e08b0SJohannes Berg 
1346c20e08b0SJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
1347c20e08b0SJohannes Berg 			    "Allocating %squeue #%d to sta %d on tid %d\n",
1348c20e08b0SJohannes Berg 			    shared_queue ? "shared " : "", queue,
1349c20e08b0SJohannes Berg 			    mvmsta->sta_id, tid);
1350c20e08b0SJohannes Berg 
1351c20e08b0SJohannes Berg 	if (shared_queue) {
1352c20e08b0SJohannes Berg 		/* Disable any open aggs on this queue */
1353c20e08b0SJohannes Berg 		disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
1354c20e08b0SJohannes Berg 
1355c20e08b0SJohannes Berg 		if (disable_agg_tids) {
1356c20e08b0SJohannes Berg 			IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
1357c20e08b0SJohannes Berg 					    queue);
1358c20e08b0SJohannes Berg 			iwl_mvm_invalidate_sta_queue(mvm, queue,
1359c20e08b0SJohannes Berg 						     disable_agg_tids, false);
1360c20e08b0SJohannes Berg 		}
1361c20e08b0SJohannes Berg 	}
1362c20e08b0SJohannes Berg 
1363cfbc6c4cSSara Sharon 	inc_ssn = iwl_mvm_enable_txq(mvm, sta, queue, ssn, &cfg, wdg_timeout);
1364c20e08b0SJohannes Berg 
1365c20e08b0SJohannes Berg 	/*
1366c20e08b0SJohannes Berg 	 * Mark queue as shared in transport if shared
1367c20e08b0SJohannes Berg 	 * Note this has to be done after queue enablement because enablement
1368c20e08b0SJohannes Berg 	 * can also set this value, and there is no indication there to shared
1369c20e08b0SJohannes Berg 	 * queues
1370c20e08b0SJohannes Berg 	 */
1371c20e08b0SJohannes Berg 	if (shared_queue)
1372c20e08b0SJohannes Berg 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
1373c20e08b0SJohannes Berg 
1374c20e08b0SJohannes Berg 	spin_lock_bh(&mvmsta->lock);
1375c20e08b0SJohannes Berg 	/*
1376c20e08b0SJohannes Berg 	 * This looks racy, but it is not. We have only one packet for
1377c20e08b0SJohannes Berg 	 * this ra/tid in our Tx path since we stop the Qdisc when we
1378c20e08b0SJohannes Berg 	 * need to allocate a new TFD queue.
1379c20e08b0SJohannes Berg 	 */
138035739348SSara Sharon 	if (inc_ssn) {
1381c20e08b0SJohannes Berg 		mvmsta->tid_data[tid].seq_number += 0x10;
138235739348SSara Sharon 		ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
138335739348SSara Sharon 	}
1384c20e08b0SJohannes Berg 	mvmsta->tid_data[tid].txq_id = queue;
1385c20e08b0SJohannes Berg 	mvmsta->tfd_queue_msk |= BIT(queue);
1386c20e08b0SJohannes Berg 	queue_state = mvmsta->tid_data[tid].state;
1387c20e08b0SJohannes Berg 
1388c20e08b0SJohannes Berg 	if (mvmsta->reserved_queue == queue)
1389c20e08b0SJohannes Berg 		mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
1390c20e08b0SJohannes Berg 	spin_unlock_bh(&mvmsta->lock);
1391c20e08b0SJohannes Berg 
1392c20e08b0SJohannes Berg 	if (!shared_queue) {
1393c20e08b0SJohannes Berg 		ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
1394c20e08b0SJohannes Berg 		if (ret)
1395c20e08b0SJohannes Berg 			goto out_err;
1396c20e08b0SJohannes Berg 
1397c20e08b0SJohannes Berg 		/* If we need to re-enable aggregations... */
1398c20e08b0SJohannes Berg 		if (queue_state == IWL_AGG_ON) {
1399c20e08b0SJohannes Berg 			ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1400c20e08b0SJohannes Berg 			if (ret)
1401c20e08b0SJohannes Berg 				goto out_err;
1402c20e08b0SJohannes Berg 		}
1403c20e08b0SJohannes Berg 	} else {
1404c20e08b0SJohannes Berg 		/* Redirect queue, if needed */
1405cfbc6c4cSSara Sharon 		ret = iwl_mvm_redirect_queue(mvm, queue, tid, ac, ssn,
1406cfbc6c4cSSara Sharon 					     wdg_timeout, false,
1407cfbc6c4cSSara Sharon 					     iwl_mvm_txq_from_tid(sta, tid));
1408c20e08b0SJohannes Berg 		if (ret)
1409c20e08b0SJohannes Berg 			goto out_err;
1410c20e08b0SJohannes Berg 	}
1411c20e08b0SJohannes Berg 
1412c20e08b0SJohannes Berg 	return 0;
1413c20e08b0SJohannes Berg 
1414c20e08b0SJohannes Berg out_err:
1415c6ce1c74SJohannes Berg 	queue_tmp = queue;
1416c5a976cfSJohannes Berg 	iwl_mvm_disable_txq(mvm, sta, mvmsta->sta_id, &queue_tmp, tid);
1417c20e08b0SJohannes Berg 
1418c20e08b0SJohannes Berg 	return ret;
1419c20e08b0SJohannes Berg }
1420c20e08b0SJohannes Berg 
142124afba76SLiad Kaufman void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
142224afba76SLiad Kaufman {
142324afba76SLiad Kaufman 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
142424afba76SLiad Kaufman 					   add_stream_wk);
142524afba76SLiad Kaufman 
142624afba76SLiad Kaufman 	mutex_lock(&mvm->mutex);
142724afba76SLiad Kaufman 
1428724fe771SJohannes Berg 	iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
14299f9af3d7SLiad Kaufman 
1430cfbc6c4cSSara Sharon 	while (!list_empty(&mvm->add_stream_txqs)) {
1431cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq;
1432cfbc6c4cSSara Sharon 		struct ieee80211_txq *txq;
1433cfbc6c4cSSara Sharon 		u8 tid;
143424afba76SLiad Kaufman 
1435cfbc6c4cSSara Sharon 		mvmtxq = list_first_entry(&mvm->add_stream_txqs,
1436cfbc6c4cSSara Sharon 					  struct iwl_mvm_txq, list);
143724afba76SLiad Kaufman 
1438cfbc6c4cSSara Sharon 		txq = container_of((void *)mvmtxq, struct ieee80211_txq,
1439cfbc6c4cSSara Sharon 				   drv_priv);
1440cfbc6c4cSSara Sharon 		tid = txq->tid;
1441cfbc6c4cSSara Sharon 		if (tid == IEEE80211_NUM_TIDS)
1442cfbc6c4cSSara Sharon 			tid = IWL_MAX_TID_COUNT;
1443cfbc6c4cSSara Sharon 
14443d1d87abSJohannes Berg 		/*
14453d1d87abSJohannes Berg 		 * We can't really do much here, but if this fails we can't
14463d1d87abSJohannes Berg 		 * transmit anyway - so just don't transmit the frame etc.
14473d1d87abSJohannes Berg 		 * and let them back up ... we've tried our best to allocate
14483d1d87abSJohannes Berg 		 * a queue in the function itself.
14493d1d87abSJohannes Berg 		 */
14503d1d87abSJohannes Berg 		if (iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, tid)) {
14513d1d87abSJohannes Berg 			list_del_init(&mvmtxq->list);
14523d1d87abSJohannes Berg 			continue;
14533d1d87abSJohannes Berg 		}
14543d1d87abSJohannes Berg 
1455cfbc6c4cSSara Sharon 		list_del_init(&mvmtxq->list);
1456f5ae2f93SJohannes Berg 		local_bh_disable();
1457cfbc6c4cSSara Sharon 		iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1458f5ae2f93SJohannes Berg 		local_bh_enable();
145924afba76SLiad Kaufman 	}
146024afba76SLiad Kaufman 
146124afba76SLiad Kaufman 	mutex_unlock(&mvm->mutex);
146224afba76SLiad Kaufman }
146324afba76SLiad Kaufman 
146424afba76SLiad Kaufman static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1465d5216a28SLiad Kaufman 				      struct ieee80211_sta *sta,
1466d5216a28SLiad Kaufman 				      enum nl80211_iftype vif_type)
146724afba76SLiad Kaufman {
146824afba76SLiad Kaufman 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
146924afba76SLiad Kaufman 	int queue;
147024afba76SLiad Kaufman 
1471396952eeSSara Sharon 	/* queue reserving is disabled on new TX path */
1472396952eeSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1473396952eeSSara Sharon 		return 0;
1474396952eeSSara Sharon 
1475724fe771SJohannes Berg 	/* run the general cleanup/unsharing of queues */
1476724fe771SJohannes Berg 	iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
14779794c64fSLiad Kaufman 
147824afba76SLiad Kaufman 	/* Make sure we have free resources for this STA */
1479d5216a28SLiad Kaufman 	if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
14801c14089eSJohannes Berg 	    !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].tid_bitmap &&
1481cf961e16SLiad Kaufman 	    (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1482cf961e16SLiad Kaufman 	     IWL_MVM_QUEUE_FREE))
1483d5216a28SLiad Kaufman 		queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1484d5216a28SLiad Kaufman 	else
14859794c64fSLiad Kaufman 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
14869794c64fSLiad Kaufman 						IWL_MVM_DQA_MIN_DATA_QUEUE,
148724afba76SLiad Kaufman 						IWL_MVM_DQA_MAX_DATA_QUEUE);
148824afba76SLiad Kaufman 	if (queue < 0) {
1489724fe771SJohannes Berg 		/* try again - this time kick out a queue if needed */
1490724fe771SJohannes Berg 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1491724fe771SJohannes Berg 		if (queue < 0) {
149224afba76SLiad Kaufman 			IWL_ERR(mvm, "No available queues for new station\n");
149324afba76SLiad Kaufman 			return -ENOSPC;
1494724fe771SJohannes Berg 		}
149524afba76SLiad Kaufman 	}
1496cf961e16SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
149724afba76SLiad Kaufman 
149824afba76SLiad Kaufman 	mvmsta->reserved_queue = queue;
149924afba76SLiad Kaufman 
150024afba76SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
150124afba76SLiad Kaufman 			    queue, mvmsta->sta_id);
150224afba76SLiad Kaufman 
150324afba76SLiad Kaufman 	return 0;
150424afba76SLiad Kaufman }
150524afba76SLiad Kaufman 
15068d98ae6eSLiad Kaufman /*
15078d98ae6eSLiad Kaufman  * In DQA mode, after a HW restart the queues should be allocated as before, in
15088d98ae6eSLiad Kaufman  * order to avoid race conditions when there are shared queues. This function
15098d98ae6eSLiad Kaufman  * does the re-mapping and queue allocation.
15108d98ae6eSLiad Kaufman  *
15118d98ae6eSLiad Kaufman  * Note that re-enabling aggregations isn't done in this function.
15128d98ae6eSLiad Kaufman  */
15138d98ae6eSLiad Kaufman static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1514cfbc6c4cSSara Sharon 						 struct ieee80211_sta *sta)
15158d98ae6eSLiad Kaufman {
1516cfbc6c4cSSara Sharon 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1517cfbc6c4cSSara Sharon 	unsigned int wdg =
15188d98ae6eSLiad Kaufman 		iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
15198d98ae6eSLiad Kaufman 	int i;
15208d98ae6eSLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
15218d98ae6eSLiad Kaufman 		.sta_id = mvm_sta->sta_id,
15228d98ae6eSLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
15238d98ae6eSLiad Kaufman 	};
15248d98ae6eSLiad Kaufman 
152503c902bfSJohannes Berg 	/* Make sure reserved queue is still marked as such (if allocated) */
152603c902bfSJohannes Berg 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
15278d98ae6eSLiad Kaufman 		mvm->queue_info[mvm_sta->reserved_queue].status =
15288d98ae6eSLiad Kaufman 			IWL_MVM_QUEUE_RESERVED;
15298d98ae6eSLiad Kaufman 
15308d98ae6eSLiad Kaufman 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
15318d98ae6eSLiad Kaufman 		struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
15328d98ae6eSLiad Kaufman 		int txq_id = tid_data->txq_id;
15338d98ae6eSLiad Kaufman 		int ac;
15348d98ae6eSLiad Kaufman 
15356862fceeSSara Sharon 		if (txq_id == IWL_MVM_INVALID_QUEUE)
15368d98ae6eSLiad Kaufman 			continue;
15378d98ae6eSLiad Kaufman 
15388d98ae6eSLiad Kaufman 		ac = tid_to_mac80211_ac[i];
15398d98ae6eSLiad Kaufman 
1540310181ecSSara Sharon 		if (iwl_mvm_has_new_tx_api(mvm)) {
1541310181ecSSara Sharon 			IWL_DEBUG_TX_QUEUES(mvm,
1542310181ecSSara Sharon 					    "Re-mapping sta %d tid %d\n",
1543310181ecSSara Sharon 					    mvm_sta->sta_id, i);
1544*09369983SJohannes Berg 			txq_id = iwl_mvm_tvqm_enable_txq(mvm, sta,
1545*09369983SJohannes Berg 							 mvm_sta->sta_id,
1546cfbc6c4cSSara Sharon 							 i, wdg);
154791cf5dedSJohannes Berg 			/*
154891cf5dedSJohannes Berg 			 * on failures, just set it to IWL_MVM_INVALID_QUEUE
154991cf5dedSJohannes Berg 			 * to try again later, we have no other good way of
155091cf5dedSJohannes Berg 			 * failing here
155191cf5dedSJohannes Berg 			 */
155291cf5dedSJohannes Berg 			if (txq_id < 0)
155391cf5dedSJohannes Berg 				txq_id = IWL_MVM_INVALID_QUEUE;
1554310181ecSSara Sharon 			tid_data->txq_id = txq_id;
15555d39051aSLiad Kaufman 
15565d39051aSLiad Kaufman 			/*
15575d39051aSLiad Kaufman 			 * Since we don't set the seq number after reset, and HW
15585d39051aSLiad Kaufman 			 * sets it now, FW reset will cause the seq num to start
15595d39051aSLiad Kaufman 			 * at 0 again, so driver will need to update it
15605d39051aSLiad Kaufman 			 * internally as well, so it keeps in sync with real val
15615d39051aSLiad Kaufman 			 */
15625d39051aSLiad Kaufman 			tid_data->seq_number = 0;
1563310181ecSSara Sharon 		} else {
1564310181ecSSara Sharon 			u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1565310181ecSSara Sharon 
15668d98ae6eSLiad Kaufman 			cfg.tid = i;
1567cf6c6ea3SEmmanuel Grumbach 			cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
15688d98ae6eSLiad Kaufman 			cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1569310181ecSSara Sharon 					 txq_id ==
1570310181ecSSara Sharon 					 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
15718d98ae6eSLiad Kaufman 
15728d98ae6eSLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm,
15738d98ae6eSLiad Kaufman 					    "Re-mapping sta %d tid %d to queue %d\n",
15748d98ae6eSLiad Kaufman 					    mvm_sta->sta_id, i, txq_id);
15758d98ae6eSLiad Kaufman 
1576cfbc6c4cSSara Sharon 			iwl_mvm_enable_txq(mvm, sta, txq_id, seq, &cfg, wdg);
15778d98ae6eSLiad Kaufman 			mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
15788d98ae6eSLiad Kaufman 		}
157934e10860SSara Sharon 	}
15808d98ae6eSLiad Kaufman }
15818d98ae6eSLiad Kaufman 
1582732d06e9SShaul Triebitz static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1583732d06e9SShaul Triebitz 				      struct iwl_mvm_int_sta *sta,
1584732d06e9SShaul Triebitz 				      const u8 *addr,
1585732d06e9SShaul Triebitz 				      u16 mac_id, u16 color)
1586732d06e9SShaul Triebitz {
1587732d06e9SShaul Triebitz 	struct iwl_mvm_add_sta_cmd cmd;
1588732d06e9SShaul Triebitz 	int ret;
15893f497de9SLuca Coelho 	u32 status = ADD_STA_SUCCESS;
1590732d06e9SShaul Triebitz 
1591732d06e9SShaul Triebitz 	lockdep_assert_held(&mvm->mutex);
1592732d06e9SShaul Triebitz 
1593732d06e9SShaul Triebitz 	memset(&cmd, 0, sizeof(cmd));
1594732d06e9SShaul Triebitz 	cmd.sta_id = sta->sta_id;
15952c2c3647SNathan Errera 
1596971cbe50SJohannes Berg 	if (iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA, 0) >= 12 &&
15972c2c3647SNathan Errera 	    sta->type == IWL_STA_AUX_ACTIVITY)
15982c2c3647SNathan Errera 		cmd.mac_id_n_color = cpu_to_le32(mac_id);
15992c2c3647SNathan Errera 	else
1600732d06e9SShaul Triebitz 		cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1601732d06e9SShaul Triebitz 								     color));
16022c2c3647SNathan Errera 
1603732d06e9SShaul Triebitz 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1604732d06e9SShaul Triebitz 		cmd.station_type = sta->type;
1605732d06e9SShaul Triebitz 
1606732d06e9SShaul Triebitz 	if (!iwl_mvm_has_new_tx_api(mvm))
1607732d06e9SShaul Triebitz 		cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1608732d06e9SShaul Triebitz 	cmd.tid_disable_tx = cpu_to_le16(0xffff);
1609732d06e9SShaul Triebitz 
1610732d06e9SShaul Triebitz 	if (addr)
1611732d06e9SShaul Triebitz 		memcpy(cmd.addr, addr, ETH_ALEN);
1612732d06e9SShaul Triebitz 
1613732d06e9SShaul Triebitz 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1614732d06e9SShaul Triebitz 					  iwl_mvm_add_sta_cmd_size(mvm),
1615732d06e9SShaul Triebitz 					  &cmd, &status);
1616732d06e9SShaul Triebitz 	if (ret)
1617732d06e9SShaul Triebitz 		return ret;
1618732d06e9SShaul Triebitz 
1619732d06e9SShaul Triebitz 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1620732d06e9SShaul Triebitz 	case ADD_STA_SUCCESS:
1621732d06e9SShaul Triebitz 		IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1622732d06e9SShaul Triebitz 		return 0;
1623732d06e9SShaul Triebitz 	default:
1624732d06e9SShaul Triebitz 		ret = -EIO;
1625732d06e9SShaul Triebitz 		IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1626732d06e9SShaul Triebitz 			status);
1627732d06e9SShaul Triebitz 		break;
1628732d06e9SShaul Triebitz 	}
1629732d06e9SShaul Triebitz 	return ret;
1630732d06e9SShaul Triebitz }
1631732d06e9SShaul Triebitz 
1632e705c121SKalle Valo int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1633e705c121SKalle Valo 		    struct ieee80211_vif *vif,
1634e705c121SKalle Valo 		    struct ieee80211_sta *sta)
1635e705c121SKalle Valo {
1636e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1637e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1638a571f5f6SSara Sharon 	struct iwl_mvm_rxq_dup_data *dup_data;
1639e705c121SKalle Valo 	int i, ret, sta_id;
1640732d06e9SShaul Triebitz 	bool sta_update = false;
1641732d06e9SShaul Triebitz 	unsigned int sta_flags = 0;
1642e705c121SKalle Valo 
1643e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1644e705c121SKalle Valo 
1645e705c121SKalle Valo 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1646e705c121SKalle Valo 		sta_id = iwl_mvm_find_free_sta_id(mvm,
1647e705c121SKalle Valo 						  ieee80211_vif_type_p2p(vif));
1648e705c121SKalle Valo 	else
1649e705c121SKalle Valo 		sta_id = mvm_sta->sta_id;
1650e705c121SKalle Valo 
16510ae98812SSara Sharon 	if (sta_id == IWL_MVM_INVALID_STA)
1652e705c121SKalle Valo 		return -ENOSPC;
1653e705c121SKalle Valo 
1654e705c121SKalle Valo 	spin_lock_init(&mvm_sta->lock);
1655e705c121SKalle Valo 
1656c8f54701SJohannes Berg 	/* if this is a HW restart re-alloc existing queues */
1657c8f54701SJohannes Berg 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1658732d06e9SShaul Triebitz 		struct iwl_mvm_int_sta tmp_sta = {
1659732d06e9SShaul Triebitz 			.sta_id = sta_id,
1660732d06e9SShaul Triebitz 			.type = mvm_sta->sta_type,
1661732d06e9SShaul Triebitz 		};
1662732d06e9SShaul Triebitz 
1663732d06e9SShaul Triebitz 		/*
1664732d06e9SShaul Triebitz 		 * First add an empty station since allocating
1665732d06e9SShaul Triebitz 		 * a queue requires a valid station
1666732d06e9SShaul Triebitz 		 */
1667732d06e9SShaul Triebitz 		ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1668732d06e9SShaul Triebitz 						 mvmvif->id, mvmvif->color);
1669732d06e9SShaul Triebitz 		if (ret)
1670732d06e9SShaul Triebitz 			goto err;
1671732d06e9SShaul Triebitz 
1672cfbc6c4cSSara Sharon 		iwl_mvm_realloc_queues_after_restart(mvm, sta);
1673732d06e9SShaul Triebitz 		sta_update = true;
1674732d06e9SShaul Triebitz 		sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
16758d98ae6eSLiad Kaufman 		goto update_fw;
16768d98ae6eSLiad Kaufman 	}
16778d98ae6eSLiad Kaufman 
1678e705c121SKalle Valo 	mvm_sta->sta_id = sta_id;
1679e705c121SKalle Valo 	mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1680e705c121SKalle Valo 						      mvmvif->color);
1681e705c121SKalle Valo 	mvm_sta->vif = vif;
1682286ca8ebSLuca Coelho 	if (!mvm->trans->trans_cfg->gen2)
1683e705c121SKalle Valo 		mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1684a58bb468SLiad Kaufman 	else
1685a58bb468SLiad Kaufman 		mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
1686e705c121SKalle Valo 	mvm_sta->tx_protection = 0;
1687e705c121SKalle Valo 	mvm_sta->tt_tx_protection = false;
1688ced19f26SSara Sharon 	mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
1689e705c121SKalle Valo 
1690e705c121SKalle Valo 	/* HW restart, don't assume the memory has been zeroed */
1691e705c121SKalle Valo 	mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1692e705c121SKalle Valo 	mvm_sta->tfd_queue_msk = 0;
1693e705c121SKalle Valo 
1694e705c121SKalle Valo 	/* for HW restart - reset everything but the sequence number */
169524afba76SLiad Kaufman 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1696e705c121SKalle Valo 		u16 seq = mvm_sta->tid_data[i].seq_number;
1697e705c121SKalle Valo 		memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1698e705c121SKalle Valo 		mvm_sta->tid_data[i].seq_number = seq;
169924afba76SLiad Kaufman 
170024afba76SLiad Kaufman 		/*
170124afba76SLiad Kaufman 		 * Mark all queues for this STA as unallocated and defer TX
170224afba76SLiad Kaufman 		 * frames until the queue is allocated
170324afba76SLiad Kaufman 		 */
17046862fceeSSara Sharon 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1705e705c121SKalle Valo 	}
1706cfbc6c4cSSara Sharon 
1707cfbc6c4cSSara Sharon 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1708cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
1709cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1710cfbc6c4cSSara Sharon 
1711cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1712cfbc6c4cSSara Sharon 		INIT_LIST_HEAD(&mvmtxq->list);
1713fba8248eSSara Sharon 		atomic_set(&mvmtxq->tx_request, 0);
1714cfbc6c4cSSara Sharon 	}
1715cfbc6c4cSSara Sharon 
1716e705c121SKalle Valo 	mvm_sta->agg_tids = 0;
1717e705c121SKalle Valo 
1718a571f5f6SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) &&
1719a571f5f6SSara Sharon 	    !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
172092c4dca6SJohannes Berg 		int q;
172192c4dca6SJohannes Berg 
1722a571f5f6SSara Sharon 		dup_data = kcalloc(mvm->trans->num_rx_queues,
172392c4dca6SJohannes Berg 				   sizeof(*dup_data), GFP_KERNEL);
1724a571f5f6SSara Sharon 		if (!dup_data)
1725a571f5f6SSara Sharon 			return -ENOMEM;
172692c4dca6SJohannes Berg 		/*
172792c4dca6SJohannes Berg 		 * Initialize all the last_seq values to 0xffff which can never
172892c4dca6SJohannes Berg 		 * compare equal to the frame's seq_ctrl in the check in
172992c4dca6SJohannes Berg 		 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
173092c4dca6SJohannes Berg 		 * number and fragmented packets don't reach that function.
173192c4dca6SJohannes Berg 		 *
173292c4dca6SJohannes Berg 		 * This thus allows receiving a packet with seqno 0 and the
173392c4dca6SJohannes Berg 		 * retry bit set as the very first packet on a new TID.
173492c4dca6SJohannes Berg 		 */
173592c4dca6SJohannes Berg 		for (q = 0; q < mvm->trans->num_rx_queues; q++)
173692c4dca6SJohannes Berg 			memset(dup_data[q].last_seq, 0xff,
173792c4dca6SJohannes Berg 			       sizeof(dup_data[q].last_seq));
1738a571f5f6SSara Sharon 		mvm_sta->dup_data = dup_data;
1739a571f5f6SSara Sharon 	}
1740a571f5f6SSara Sharon 
1741c8f54701SJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm)) {
1742d5216a28SLiad Kaufman 		ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1743d5216a28SLiad Kaufman 						 ieee80211_vif_type_p2p(vif));
174424afba76SLiad Kaufman 		if (ret)
174524afba76SLiad Kaufman 			goto err;
174624afba76SLiad Kaufman 	}
174724afba76SLiad Kaufman 
17489f66a397SGregory Greenman 	/*
17499f66a397SGregory Greenman 	 * if rs is registered with mac80211, then "add station" will be handled
17509f66a397SGregory Greenman 	 * via the corresponding ops, otherwise need to notify rate scaling here
17519f66a397SGregory Greenman 	 */
17524243edb4SEmmanuel Grumbach 	if (iwl_mvm_has_tlc_offload(mvm))
17539f66a397SGregory Greenman 		iwl_mvm_rs_add_sta(mvm, mvm_sta);
17540f8084cdSMordechay Goodstein 	else
1755f5d88fa3SGregory Greenman 		spin_lock_init(&mvm_sta->lq_sta.rs_drv.pers.lock);
17569f66a397SGregory Greenman 
17570dde2440SAvraham Stern 	iwl_mvm_toggle_tx_ant(mvm, &mvm_sta->tx_ant);
17580dde2440SAvraham Stern 
17598d98ae6eSLiad Kaufman update_fw:
1760732d06e9SShaul Triebitz 	ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
1761e705c121SKalle Valo 	if (ret)
1762e705c121SKalle Valo 		goto err;
1763e705c121SKalle Valo 
1764e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION) {
1765e705c121SKalle Valo 		if (!sta->tdls) {
17660ae98812SSara Sharon 			WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
1767e705c121SKalle Valo 			mvmvif->ap_sta_id = sta_id;
1768e705c121SKalle Valo 		} else {
17690ae98812SSara Sharon 			WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
1770e705c121SKalle Valo 		}
1771e705c121SKalle Valo 	}
1772e705c121SKalle Valo 
1773e705c121SKalle Valo 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1774e705c121SKalle Valo 
1775e705c121SKalle Valo 	return 0;
1776e705c121SKalle Valo 
1777e705c121SKalle Valo err:
1778e705c121SKalle Valo 	return ret;
1779e705c121SKalle Valo }
1780e705c121SKalle Valo 
1781e705c121SKalle Valo int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1782e705c121SKalle Valo 		      bool drain)
1783e705c121SKalle Valo {
1784e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
1785e705c121SKalle Valo 	int ret;
1786e705c121SKalle Valo 	u32 status;
1787e705c121SKalle Valo 
1788e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1789e705c121SKalle Valo 
1790e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1791e705c121SKalle Valo 	cmd.sta_id = mvmsta->sta_id;
1792e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
1793e705c121SKalle Valo 	cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1794e705c121SKalle Valo 	cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1795e705c121SKalle Valo 
1796e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
1797854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1798854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
1799e705c121SKalle Valo 					  &cmd, &status);
1800e705c121SKalle Valo 	if (ret)
1801e705c121SKalle Valo 		return ret;
1802e705c121SKalle Valo 
1803837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1804e705c121SKalle Valo 	case ADD_STA_SUCCESS:
1805e705c121SKalle Valo 		IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1806e705c121SKalle Valo 			       mvmsta->sta_id);
1807e705c121SKalle Valo 		break;
1808e705c121SKalle Valo 	default:
1809e705c121SKalle Valo 		ret = -EIO;
1810e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1811e705c121SKalle Valo 			mvmsta->sta_id);
1812e705c121SKalle Valo 		break;
1813e705c121SKalle Valo 	}
1814e705c121SKalle Valo 
1815e705c121SKalle Valo 	return ret;
1816e705c121SKalle Valo }
1817e705c121SKalle Valo 
1818e705c121SKalle Valo /*
1819e705c121SKalle Valo  * Remove a station from the FW table. Before sending the command to remove
1820e705c121SKalle Valo  * the station validate that the station is indeed known to the driver (sanity
1821e705c121SKalle Valo  * only).
1822e705c121SKalle Valo  */
1823e705c121SKalle Valo static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1824e705c121SKalle Valo {
1825e705c121SKalle Valo 	struct ieee80211_sta *sta;
1826e705c121SKalle Valo 	struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1827e705c121SKalle Valo 		.sta_id = sta_id,
1828e705c121SKalle Valo 	};
1829e705c121SKalle Valo 	int ret;
1830e705c121SKalle Valo 
1831e705c121SKalle Valo 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1832e705c121SKalle Valo 					lockdep_is_held(&mvm->mutex));
1833e705c121SKalle Valo 
1834e705c121SKalle Valo 	/* Note: internal stations are marked as error values */
1835e705c121SKalle Valo 	if (!sta) {
1836e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid station id\n");
1837e705c121SKalle Valo 		return -EINVAL;
1838e705c121SKalle Valo 	}
1839e705c121SKalle Valo 
1840e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1841e705c121SKalle Valo 				   sizeof(rm_sta_cmd), &rm_sta_cmd);
1842e705c121SKalle Valo 	if (ret) {
1843e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1844e705c121SKalle Valo 		return ret;
1845e705c121SKalle Valo 	}
1846e705c121SKalle Valo 
1847e705c121SKalle Valo 	return 0;
1848e705c121SKalle Valo }
1849e705c121SKalle Valo 
185024afba76SLiad Kaufman static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
185124afba76SLiad Kaufman 				       struct ieee80211_vif *vif,
1852cfbc6c4cSSara Sharon 				       struct ieee80211_sta *sta)
185324afba76SLiad Kaufman {
1854cfbc6c4cSSara Sharon 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
185524afba76SLiad Kaufman 	int i;
185624afba76SLiad Kaufman 
185724afba76SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
185824afba76SLiad Kaufman 
185924afba76SLiad Kaufman 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
18606862fceeSSara Sharon 		if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
186124afba76SLiad Kaufman 			continue;
186224afba76SLiad Kaufman 
1863c5a976cfSJohannes Berg 		iwl_mvm_disable_txq(mvm, sta, mvm_sta->sta_id,
1864c5a976cfSJohannes Berg 				    &mvm_sta->tid_data[i].txq_id, i);
18656862fceeSSara Sharon 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
186624afba76SLiad Kaufman 	}
1867cfbc6c4cSSara Sharon 
1868cfbc6c4cSSara Sharon 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1869cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
1870cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1871cfbc6c4cSSara Sharon 
1872cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
187314a3aacfSJose Ignacio Tornos Martinez 		list_del_init(&mvmtxq->list);
1874cfbc6c4cSSara Sharon 	}
187524afba76SLiad Kaufman }
187624afba76SLiad Kaufman 
1877d6d517b7SSara Sharon int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1878d6d517b7SSara Sharon 				  struct iwl_mvm_sta *mvm_sta)
1879d6d517b7SSara Sharon {
1880bec9522aSSharon Dvir 	int i;
1881d6d517b7SSara Sharon 
1882d6d517b7SSara Sharon 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1883d6d517b7SSara Sharon 		u16 txq_id;
1884bec9522aSSharon Dvir 		int ret;
1885d6d517b7SSara Sharon 
1886d6d517b7SSara Sharon 		spin_lock_bh(&mvm_sta->lock);
1887d6d517b7SSara Sharon 		txq_id = mvm_sta->tid_data[i].txq_id;
1888d6d517b7SSara Sharon 		spin_unlock_bh(&mvm_sta->lock);
1889d6d517b7SSara Sharon 
1890d6d517b7SSara Sharon 		if (txq_id == IWL_MVM_INVALID_QUEUE)
1891d6d517b7SSara Sharon 			continue;
1892d6d517b7SSara Sharon 
1893d6d517b7SSara Sharon 		ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1894d6d517b7SSara Sharon 		if (ret)
1895bec9522aSSharon Dvir 			return ret;
1896d6d517b7SSara Sharon 	}
1897d6d517b7SSara Sharon 
1898bec9522aSSharon Dvir 	return 0;
1899d6d517b7SSara Sharon }
1900d6d517b7SSara Sharon 
1901e705c121SKalle Valo int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1902e705c121SKalle Valo 		   struct ieee80211_vif *vif,
1903e705c121SKalle Valo 		   struct ieee80211_sta *sta)
1904e705c121SKalle Valo {
1905e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1906e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
190794c3e614SSara Sharon 	u8 sta_id = mvm_sta->sta_id;
1908e705c121SKalle Valo 	int ret;
1909e705c121SKalle Valo 
1910e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1911e705c121SKalle Valo 
1912a571f5f6SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
1913a571f5f6SSara Sharon 		kfree(mvm_sta->dup_data);
1914a571f5f6SSara Sharon 
1915e705c121SKalle Valo 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1916e705c121SKalle Valo 	if (ret)
1917e705c121SKalle Valo 		return ret;
1918c8f54701SJohannes Berg 
1919e705c121SKalle Valo 	/* flush its queues here since we are freeing mvm_sta */
1920f9084775SNathan Errera 	ret = iwl_mvm_flush_sta(mvm, mvm_sta, false);
1921e705c121SKalle Valo 	if (ret)
1922e705c121SKalle Valo 		return ret;
1923d6d517b7SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
1924d6d517b7SSara Sharon 		ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1925d6d517b7SSara Sharon 	} else {
1926d6d517b7SSara Sharon 		u32 q_mask = mvm_sta->tfd_queue_msk;
1927d6d517b7SSara Sharon 
1928a1a57877SSara Sharon 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1929d6d517b7SSara Sharon 						     q_mask);
1930d6d517b7SSara Sharon 	}
1931e705c121SKalle Valo 	if (ret)
1932e705c121SKalle Valo 		return ret;
1933c8f54701SJohannes Berg 
1934e705c121SKalle Valo 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1935e705c121SKalle Valo 
1936cfbc6c4cSSara Sharon 	iwl_mvm_disable_sta_queues(mvm, vif, sta);
193756214749SLiad Kaufman 
193856214749SLiad Kaufman 	/* If there is a TXQ still marked as reserved - free it */
1939c8f54701SJohannes Berg 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1940a0315deaSLiad Kaufman 		u8 reserved_txq = mvm_sta->reserved_queue;
1941a0315deaSLiad Kaufman 		enum iwl_mvm_queue_status *status;
1942a0315deaSLiad Kaufman 
1943a0315deaSLiad Kaufman 		/*
1944a0315deaSLiad Kaufman 		 * If no traffic has gone through the reserved TXQ - it
1945a0315deaSLiad Kaufman 		 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1946a0315deaSLiad Kaufman 		 * should be manually marked as free again
1947a0315deaSLiad Kaufman 		 */
1948a0315deaSLiad Kaufman 		status = &mvm->queue_info[reserved_txq].status;
1949a0315deaSLiad Kaufman 		if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1950a0315deaSLiad Kaufman 			 (*status != IWL_MVM_QUEUE_FREE),
1951a0315deaSLiad Kaufman 			 "sta_id %d reserved txq %d status %d",
1952f3f240f9SJohannes Berg 			 sta_id, reserved_txq, *status))
1953a0315deaSLiad Kaufman 			return -EINVAL;
1954a0315deaSLiad Kaufman 
1955a0315deaSLiad Kaufman 		*status = IWL_MVM_QUEUE_FREE;
1956a0315deaSLiad Kaufman 	}
1957a0315deaSLiad Kaufman 
1958e3118ad7SLiad Kaufman 	if (vif->type == NL80211_IFTYPE_STATION &&
195994c3e614SSara Sharon 	    mvmvif->ap_sta_id == sta_id) {
1960e3118ad7SLiad Kaufman 		/* if associated - we can't remove the AP STA now */
1961f276e20bSJohannes Berg 		if (vif->cfg.assoc)
1962e705c121SKalle Valo 			return ret;
1963e705c121SKalle Valo 
19645c75a208SJohannes Berg 		/* first remove remaining keys */
19655c75a208SJohannes Berg 		iwl_mvm_sec_key_remove_ap(mvm, vif);
19665c75a208SJohannes Berg 
1967e705c121SKalle Valo 		/* unassoc - go ahead - remove the AP STA now */
19680ae98812SSara Sharon 		mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1969e705c121SKalle Valo 	}
1970e705c121SKalle Valo 
1971e705c121SKalle Valo 	/*
1972e705c121SKalle Valo 	 * This shouldn't happen - the TDLS channel switch should be canceled
1973e705c121SKalle Valo 	 * before the STA is removed.
1974e705c121SKalle Valo 	 */
197594c3e614SSara Sharon 	if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
19760ae98812SSara Sharon 		mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1977e705c121SKalle Valo 		cancel_delayed_work(&mvm->tdls_cs.dwork);
1978e705c121SKalle Valo 	}
1979e705c121SKalle Valo 
1980e705c121SKalle Valo 	/*
1981e705c121SKalle Valo 	 * Make sure that the tx response code sees the station as -EBUSY and
1982e705c121SKalle Valo 	 * calls the drain worker.
1983e705c121SKalle Valo 	 */
1984e705c121SKalle Valo 	spin_lock_bh(&mvm_sta->lock);
1985e705c121SKalle Valo 	spin_unlock_bh(&mvm_sta->lock);
1986e705c121SKalle Valo 
1987e705c121SKalle Valo 	ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1988e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1989e705c121SKalle Valo 
1990e705c121SKalle Valo 	return ret;
1991e705c121SKalle Valo }
1992e705c121SKalle Valo 
1993e705c121SKalle Valo int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1994e705c121SKalle Valo 		      struct ieee80211_vif *vif,
1995e705c121SKalle Valo 		      u8 sta_id)
1996e705c121SKalle Valo {
1997e705c121SKalle Valo 	int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1998e705c121SKalle Valo 
1999e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2000e705c121SKalle Valo 
2001e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
2002e705c121SKalle Valo 	return ret;
2003e705c121SKalle Valo }
2004e705c121SKalle Valo 
20050e39eb03SChaya Rachel Ivgi int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
2006e705c121SKalle Valo 			     struct iwl_mvm_int_sta *sta,
2007ced19f26SSara Sharon 			     u32 qmask, enum nl80211_iftype iftype,
2008ced19f26SSara Sharon 			     enum iwl_sta_type type)
2009e705c121SKalle Valo {
2010df65c8d1SAvraham Stern 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
2011df65c8d1SAvraham Stern 	    sta->sta_id == IWL_MVM_INVALID_STA) {
2012e705c121SKalle Valo 		sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
20130ae98812SSara Sharon 		if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
2014e705c121SKalle Valo 			return -ENOSPC;
2015e705c121SKalle Valo 	}
2016e705c121SKalle Valo 
2017e705c121SKalle Valo 	sta->tfd_queue_msk = qmask;
2018ced19f26SSara Sharon 	sta->type = type;
2019e705c121SKalle Valo 
2020e705c121SKalle Valo 	/* put a non-NULL value so iterating over the stations won't stop */
2021e705c121SKalle Valo 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
2022e705c121SKalle Valo 	return 0;
2023e705c121SKalle Valo }
2024e705c121SKalle Valo 
202526d6c16bSSara Sharon void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
2026e705c121SKalle Valo {
2027e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
2028e705c121SKalle Valo 	memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
20290ae98812SSara Sharon 	sta->sta_id = IWL_MVM_INVALID_STA;
2030e705c121SKalle Valo }
2031e705c121SKalle Valo 
203291cf5dedSJohannes Berg static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 queue,
2033b13f43a4SEmmanuel Grumbach 					  u8 sta_id, u8 fifo)
2034e705c121SKalle Valo {
20359617040eSEmmanuel Grumbach 	unsigned int wdg_timeout =
20369617040eSEmmanuel Grumbach 		mvm->trans->trans_cfg->base_params->wd_timeout;
203728d0793eSLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
2038b13f43a4SEmmanuel Grumbach 		.fifo = fifo,
2039b13f43a4SEmmanuel Grumbach 		.sta_id = sta_id,
204028d0793eSLiad Kaufman 		.tid = IWL_MAX_TID_COUNT,
204128d0793eSLiad Kaufman 		.aggregate = false,
204228d0793eSLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
204328d0793eSLiad Kaufman 	};
204428d0793eSLiad Kaufman 
204591cf5dedSJohannes Berg 	WARN_ON(iwl_mvm_has_new_tx_api(mvm));
204691cf5dedSJohannes Berg 
204791cf5dedSJohannes Berg 	iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
204828d0793eSLiad Kaufman }
204991cf5dedSJohannes Berg 
205091cf5dedSJohannes Berg static int iwl_mvm_enable_aux_snif_queue_tvqm(struct iwl_mvm *mvm, u8 sta_id)
205191cf5dedSJohannes Berg {
20529617040eSEmmanuel Grumbach 	unsigned int wdg_timeout =
20539617040eSEmmanuel Grumbach 		mvm->trans->trans_cfg->base_params->wd_timeout;
205491cf5dedSJohannes Berg 
205591cf5dedSJohannes Berg 	WARN_ON(!iwl_mvm_has_new_tx_api(mvm));
205691cf5dedSJohannes Berg 
2057*09369983SJohannes Berg 	return iwl_mvm_tvqm_enable_txq(mvm, NULL, sta_id, IWL_MAX_TID_COUNT,
205891cf5dedSJohannes Berg 				       wdg_timeout);
205991cf5dedSJohannes Berg }
206091cf5dedSJohannes Berg 
206191cf5dedSJohannes Berg static int iwl_mvm_add_int_sta_with_queue(struct iwl_mvm *mvm, int macidx,
2062be82ecd3SAvraham Stern 					  int maccolor, u8 *addr,
206391cf5dedSJohannes Berg 					  struct iwl_mvm_int_sta *sta,
206491cf5dedSJohannes Berg 					  u16 *queue, int fifo)
206591cf5dedSJohannes Berg {
206691cf5dedSJohannes Berg 	int ret;
206791cf5dedSJohannes Berg 
206891cf5dedSJohannes Berg 	/* Map queue to fifo - needs to happen before adding station */
206991cf5dedSJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm))
207091cf5dedSJohannes Berg 		iwl_mvm_enable_aux_snif_queue(mvm, *queue, sta->sta_id, fifo);
207191cf5dedSJohannes Berg 
2072be82ecd3SAvraham Stern 	ret = iwl_mvm_add_int_sta_common(mvm, sta, addr, macidx, maccolor);
207391cf5dedSJohannes Berg 	if (ret) {
207491cf5dedSJohannes Berg 		if (!iwl_mvm_has_new_tx_api(mvm))
2075c5a976cfSJohannes Berg 			iwl_mvm_disable_txq(mvm, NULL, sta->sta_id, queue,
207664ff7eb0SJohannes Berg 					    IWL_MAX_TID_COUNT);
207791cf5dedSJohannes Berg 		return ret;
207891cf5dedSJohannes Berg 	}
207991cf5dedSJohannes Berg 
208091cf5dedSJohannes Berg 	/*
208191cf5dedSJohannes Berg 	 * For 22000 firmware and on we cannot add queue to a station unknown
208291cf5dedSJohannes Berg 	 * to firmware so enable queue here - after the station was added
208391cf5dedSJohannes Berg 	 */
208491cf5dedSJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm)) {
208591cf5dedSJohannes Berg 		int txq;
208691cf5dedSJohannes Berg 
208791cf5dedSJohannes Berg 		txq = iwl_mvm_enable_aux_snif_queue_tvqm(mvm, sta->sta_id);
208891cf5dedSJohannes Berg 		if (txq < 0) {
208991cf5dedSJohannes Berg 			iwl_mvm_rm_sta_common(mvm, sta->sta_id);
209091cf5dedSJohannes Berg 			return txq;
209191cf5dedSJohannes Berg 		}
209291cf5dedSJohannes Berg 
209391cf5dedSJohannes Berg 		*queue = txq;
209491cf5dedSJohannes Berg 	}
209591cf5dedSJohannes Berg 
209691cf5dedSJohannes Berg 	return 0;
2097c5a719eeSSara Sharon }
2098c5a719eeSSara Sharon 
20992c2c3647SNathan Errera int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm, u32 lmac_id)
2100c5a719eeSSara Sharon {
2101c5a719eeSSara Sharon 	int ret;
2102c5a719eeSSara Sharon 
2103c5a719eeSSara Sharon 	lockdep_assert_held(&mvm->mutex);
2104c5a719eeSSara Sharon 
2105c5a719eeSSara Sharon 	/* Allocate aux station and assign to it the aux queue */
2106c5a719eeSSara Sharon 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
2107ced19f26SSara Sharon 				       NL80211_IFTYPE_UNSPECIFIED,
2108ced19f26SSara Sharon 				       IWL_STA_AUX_ACTIVITY);
2109c5a719eeSSara Sharon 	if (ret)
2110c5a719eeSSara Sharon 		return ret;
2111c5a719eeSSara Sharon 
21122c2c3647SNathan Errera 	/*
21132c2c3647SNathan Errera 	 * In CDB NICs we need to specify which lmac to use for aux activity
21142c2c3647SNathan Errera 	 * using the mac_id argument place to send lmac_id to the function
21152c2c3647SNathan Errera 	 */
21162c2c3647SNathan Errera 	ret = iwl_mvm_add_int_sta_with_queue(mvm, lmac_id, 0, NULL,
211791cf5dedSJohannes Berg 					     &mvm->aux_sta, &mvm->aux_queue,
2118b13f43a4SEmmanuel Grumbach 					     IWL_MVM_TX_FIFO_MCAST);
2119c5a719eeSSara Sharon 	if (ret) {
2120e705c121SKalle Valo 		iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2121e705c121SKalle Valo 		return ret;
2122e705c121SKalle Valo 	}
2123e705c121SKalle Valo 
2124c5a719eeSSara Sharon 	return 0;
2125c5a719eeSSara Sharon }
2126c5a719eeSSara Sharon 
21270e39eb03SChaya Rachel Ivgi int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
21280e39eb03SChaya Rachel Ivgi {
21290e39eb03SChaya Rachel Ivgi 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
21300e39eb03SChaya Rachel Ivgi 
21310e39eb03SChaya Rachel Ivgi 	lockdep_assert_held(&mvm->mutex);
2132b13f43a4SEmmanuel Grumbach 
213391cf5dedSJohannes Berg 	return iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
2134be82ecd3SAvraham Stern 					      NULL, &mvm->snif_sta,
2135be82ecd3SAvraham Stern 					      &mvm->snif_queue,
2136b13f43a4SEmmanuel Grumbach 					      IWL_MVM_TX_FIFO_BE);
21370e39eb03SChaya Rachel Ivgi }
21380e39eb03SChaya Rachel Ivgi 
21390e39eb03SChaya Rachel Ivgi int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
21400e39eb03SChaya Rachel Ivgi {
21410e39eb03SChaya Rachel Ivgi 	int ret;
21420e39eb03SChaya Rachel Ivgi 
21430e39eb03SChaya Rachel Ivgi 	lockdep_assert_held(&mvm->mutex);
21440e39eb03SChaya Rachel Ivgi 
2145e223e42aSGregory Greenman 	if (WARN_ON_ONCE(mvm->snif_sta.sta_id == IWL_MVM_INVALID_STA))
2146e223e42aSGregory Greenman 		return -EINVAL;
2147e223e42aSGregory Greenman 
2148c5a976cfSJohannes Berg 	iwl_mvm_disable_txq(mvm, NULL, mvm->snif_sta.sta_id,
2149c5a976cfSJohannes Berg 			    &mvm->snif_queue, IWL_MAX_TID_COUNT);
21500e39eb03SChaya Rachel Ivgi 	ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
21510e39eb03SChaya Rachel Ivgi 	if (ret)
21520e39eb03SChaya Rachel Ivgi 		IWL_WARN(mvm, "Failed sending remove station\n");
21530e39eb03SChaya Rachel Ivgi 
21540e39eb03SChaya Rachel Ivgi 	return ret;
21550e39eb03SChaya Rachel Ivgi }
21560e39eb03SChaya Rachel Ivgi 
2157f327236dSSharon int iwl_mvm_rm_aux_sta(struct iwl_mvm *mvm)
2158f327236dSSharon {
2159f327236dSSharon 	int ret;
2160f327236dSSharon 
2161f327236dSSharon 	lockdep_assert_held(&mvm->mutex);
2162f327236dSSharon 
2163e223e42aSGregory Greenman 	if (WARN_ON_ONCE(mvm->aux_sta.sta_id == IWL_MVM_INVALID_STA))
2164e223e42aSGregory Greenman 		return -EINVAL;
2165e223e42aSGregory Greenman 
2166c5a976cfSJohannes Berg 	iwl_mvm_disable_txq(mvm, NULL, mvm->aux_sta.sta_id,
2167c5a976cfSJohannes Berg 			    &mvm->aux_queue, IWL_MAX_TID_COUNT);
2168f327236dSSharon 	ret = iwl_mvm_rm_sta_common(mvm, mvm->aux_sta.sta_id);
2169f327236dSSharon 	if (ret)
2170f327236dSSharon 		IWL_WARN(mvm, "Failed sending remove station\n");
2171f327236dSSharon 	iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2172f327236dSSharon 
2173f327236dSSharon 	return ret;
2174f327236dSSharon }
2175f327236dSSharon 
21760e39eb03SChaya Rachel Ivgi void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
21770e39eb03SChaya Rachel Ivgi {
21780e39eb03SChaya Rachel Ivgi 	iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
21790e39eb03SChaya Rachel Ivgi }
21800e39eb03SChaya Rachel Ivgi 
2181e705c121SKalle Valo /*
2182e705c121SKalle Valo  * Send the add station command for the vif's broadcast station.
2183e705c121SKalle Valo  * Assumes that the station was already allocated.
2184e705c121SKalle Valo  *
2185e705c121SKalle Valo  * @mvm: the mvm component
2186e705c121SKalle Valo  * @vif: the interface to which the broadcast station is added
2187e705c121SKalle Valo  * @bsta: the broadcast station to add.
2188e705c121SKalle Valo  */
2189e705c121SKalle Valo int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2190e705c121SKalle Valo {
2191e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2192e705c121SKalle Valo 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2193e705c121SKalle Valo 	static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2194e705c121SKalle Valo 	const u8 *baddr = _baddr;
21957daa7624SJohannes Berg 	int queue;
2196df88c08dSLiad Kaufman 	int ret;
2197c5a719eeSSara Sharon 	unsigned int wdg_timeout =
2198c5a719eeSSara Sharon 		iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2199de24f638SLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
2200de24f638SLiad Kaufman 		.fifo = IWL_MVM_TX_FIFO_VO,
2201de24f638SLiad Kaufman 		.sta_id = mvmvif->bcast_sta.sta_id,
2202de24f638SLiad Kaufman 		.tid = IWL_MAX_TID_COUNT,
2203de24f638SLiad Kaufman 		.aggregate = false,
2204de24f638SLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
2205de24f638SLiad Kaufman 	};
2206de24f638SLiad Kaufman 
2207c5a719eeSSara Sharon 	lockdep_assert_held(&mvm->mutex);
2208c5a719eeSSara Sharon 
2209c8f54701SJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm)) {
22104d339989SLiad Kaufman 		if (vif->type == NL80211_IFTYPE_AP ||
2211f8510d67SNathan Chancellor 		    vif->type == NL80211_IFTYPE_ADHOC) {
221249f71713SSara Sharon 			queue = mvm->probe_queue;
2213f8510d67SNathan Chancellor 		} else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
221449f71713SSara Sharon 			queue = mvm->p2p_dev_queue;
2215f8510d67SNathan Chancellor 		} else {
2216f8510d67SNathan Chancellor 			WARN(1, "Missing required TXQ for adding bcast STA\n");
2217de24f638SLiad Kaufman 			return -EINVAL;
2218f8510d67SNathan Chancellor 		}
2219de24f638SLiad Kaufman 
2220df88c08dSLiad Kaufman 		bsta->tfd_queue_msk |= BIT(queue);
2221c5a719eeSSara Sharon 
2222cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2223de24f638SLiad Kaufman 	}
2224de24f638SLiad Kaufman 
2225e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_ADHOC)
2226e705c121SKalle Valo 		baddr = vif->bss_conf.bssid;
2227e705c121SKalle Valo 
22280ae98812SSara Sharon 	if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
2229e705c121SKalle Valo 		return -ENOSPC;
2230e705c121SKalle Valo 
2231df88c08dSLiad Kaufman 	ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2232e705c121SKalle Valo 					 mvmvif->id, mvmvif->color);
2233df88c08dSLiad Kaufman 	if (ret)
2234df88c08dSLiad Kaufman 		return ret;
2235df88c08dSLiad Kaufman 
2236df88c08dSLiad Kaufman 	/*
22372f7a3863SLuca Coelho 	 * For 22000 firmware and on we cannot add queue to a station unknown
2238c5a719eeSSara Sharon 	 * to firmware so enable queue here - after the station was added
2239df88c08dSLiad Kaufman 	 */
2240310181ecSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
2241*09369983SJohannes Berg 		queue = iwl_mvm_tvqm_enable_txq(mvm, NULL, bsta->sta_id,
2242310181ecSSara Sharon 						IWL_MAX_TID_COUNT,
2243c5a719eeSSara Sharon 						wdg_timeout);
224491cf5dedSJohannes Berg 		if (queue < 0) {
224591cf5dedSJohannes Berg 			iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
224691cf5dedSJohannes Berg 			return queue;
224791cf5dedSJohannes Berg 		}
22487daa7624SJohannes Berg 
22497b758a11SLuca Coelho 		if (vif->type == NL80211_IFTYPE_AP ||
22507b758a11SLuca Coelho 		    vif->type == NL80211_IFTYPE_ADHOC)
2251310181ecSSara Sharon 			mvm->probe_queue = queue;
2252310181ecSSara Sharon 		else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
2253310181ecSSara Sharon 			mvm->p2p_dev_queue = queue;
2254310181ecSSara Sharon 	}
2255df88c08dSLiad Kaufman 
2256df88c08dSLiad Kaufman 	return 0;
2257df88c08dSLiad Kaufman }
2258df88c08dSLiad Kaufman 
2259df88c08dSLiad Kaufman static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2260df88c08dSLiad Kaufman 					  struct ieee80211_vif *vif)
2261df88c08dSLiad Kaufman {
2262df88c08dSLiad Kaufman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2263c6ce1c74SJohannes Berg 	u16 *queueptr, queue;
2264df88c08dSLiad Kaufman 
2265df88c08dSLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
2266df88c08dSLiad Kaufman 
2267f9084775SNathan Errera 	iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true);
2268d49394a1SSara Sharon 
2269d167e81aSMordechai Goodstein 	switch (vif->type) {
2270d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_AP:
2271d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_ADHOC:
2272c6ce1c74SJohannes Berg 		queueptr = &mvm->probe_queue;
2273d167e81aSMordechai Goodstein 		break;
2274d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_P2P_DEVICE:
2275c6ce1c74SJohannes Berg 		queueptr = &mvm->p2p_dev_queue;
2276d167e81aSMordechai Goodstein 		break;
2277d167e81aSMordechai Goodstein 	default:
2278d167e81aSMordechai Goodstein 		WARN(1, "Can't free bcast queue on vif type %d\n",
2279d167e81aSMordechai Goodstein 		     vif->type);
2280d167e81aSMordechai Goodstein 		return;
2281df88c08dSLiad Kaufman 	}
2282df88c08dSLiad Kaufman 
2283c6ce1c74SJohannes Berg 	queue = *queueptr;
2284c5a976cfSJohannes Berg 	iwl_mvm_disable_txq(mvm, NULL, mvmvif->bcast_sta.sta_id,
2285c5a976cfSJohannes Berg 			    queueptr, IWL_MAX_TID_COUNT);
2286d167e81aSMordechai Goodstein 	if (iwl_mvm_has_new_tx_api(mvm))
2287d167e81aSMordechai Goodstein 		return;
2288d167e81aSMordechai Goodstein 
2289d167e81aSMordechai Goodstein 	WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
2290d167e81aSMordechai Goodstein 	mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
2291e705c121SKalle Valo }
2292e705c121SKalle Valo 
2293e705c121SKalle Valo /* Send the FW a request to remove the station from it's internal data
2294e705c121SKalle Valo  * structures, but DO NOT remove the entry from the local data structures. */
2295e705c121SKalle Valo int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2296e705c121SKalle Valo {
2297e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2298e705c121SKalle Valo 	int ret;
2299e705c121SKalle Valo 
2300e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2301e705c121SKalle Valo 
2302df88c08dSLiad Kaufman 	iwl_mvm_free_bcast_sta_queues(mvm, vif);
2303df88c08dSLiad Kaufman 
2304e705c121SKalle Valo 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
2305e705c121SKalle Valo 	if (ret)
2306e705c121SKalle Valo 		IWL_WARN(mvm, "Failed sending remove station\n");
2307e705c121SKalle Valo 	return ret;
2308e705c121SKalle Valo }
2309e705c121SKalle Valo 
2310e705c121SKalle Valo int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2311e705c121SKalle Valo {
2312e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2313e705c121SKalle Valo 
2314e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2315e705c121SKalle Valo 
2316c8f54701SJohannes Berg 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, 0,
2317ced19f26SSara Sharon 					ieee80211_vif_type_p2p(vif),
2318ced19f26SSara Sharon 					IWL_STA_GENERAL_PURPOSE);
2319e705c121SKalle Valo }
2320e705c121SKalle Valo 
2321e705c121SKalle Valo /* Allocate a new station entry for the broadcast station to the given vif,
2322e705c121SKalle Valo  * and send it to the FW.
2323e705c121SKalle Valo  * Note that each P2P mac should have its own broadcast station.
2324e705c121SKalle Valo  *
2325e705c121SKalle Valo  * @mvm: the mvm component
2326e705c121SKalle Valo  * @vif: the interface to which the broadcast station is added
2327e705c121SKalle Valo  * @bsta: the broadcast station to add. */
2328d197358bSLuca Coelho int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2329e705c121SKalle Valo {
2330e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2331e705c121SKalle Valo 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2332e705c121SKalle Valo 	int ret;
2333e705c121SKalle Valo 
2334e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2335e705c121SKalle Valo 
2336e705c121SKalle Valo 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2337e705c121SKalle Valo 	if (ret)
2338e705c121SKalle Valo 		return ret;
2339e705c121SKalle Valo 
2340e705c121SKalle Valo 	ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2341e705c121SKalle Valo 
2342e705c121SKalle Valo 	if (ret)
2343e705c121SKalle Valo 		iwl_mvm_dealloc_int_sta(mvm, bsta);
2344e705c121SKalle Valo 
2345e705c121SKalle Valo 	return ret;
2346e705c121SKalle Valo }
2347e705c121SKalle Valo 
2348e705c121SKalle Valo void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2349e705c121SKalle Valo {
2350e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2351e705c121SKalle Valo 
2352e705c121SKalle Valo 	iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2353e705c121SKalle Valo }
2354e705c121SKalle Valo 
2355e705c121SKalle Valo /*
2356e705c121SKalle Valo  * Send the FW a request to remove the station from it's internal data
2357e705c121SKalle Valo  * structures, and in addition remove it from the local data structure.
2358e705c121SKalle Valo  */
2359d197358bSLuca Coelho int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2360e705c121SKalle Valo {
2361e705c121SKalle Valo 	int ret;
2362e705c121SKalle Valo 
2363e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2364e705c121SKalle Valo 
2365e705c121SKalle Valo 	ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2366e705c121SKalle Valo 
2367e705c121SKalle Valo 	iwl_mvm_dealloc_bcast_sta(mvm, vif);
2368e705c121SKalle Valo 
2369e705c121SKalle Valo 	return ret;
2370e705c121SKalle Valo }
2371e705c121SKalle Valo 
237226d6c16bSSara Sharon /*
237326d6c16bSSara Sharon  * Allocate a new station entry for the multicast station to the given vif,
237426d6c16bSSara Sharon  * and send it to the FW.
237526d6c16bSSara Sharon  * Note that each AP/GO mac should have its own multicast station.
237626d6c16bSSara Sharon  *
237726d6c16bSSara Sharon  * @mvm: the mvm component
237826d6c16bSSara Sharon  * @vif: the interface to which the multicast station is added
237926d6c16bSSara Sharon  */
238026d6c16bSSara Sharon int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
238126d6c16bSSara Sharon {
238226d6c16bSSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
238326d6c16bSSara Sharon 	struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
238426d6c16bSSara Sharon 	static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
238526d6c16bSSara Sharon 	const u8 *maddr = _maddr;
238626d6c16bSSara Sharon 	struct iwl_trans_txq_scd_cfg cfg = {
2387192a7e1fSJohannes Berg 		.fifo = vif->type == NL80211_IFTYPE_AP ?
2388192a7e1fSJohannes Berg 			IWL_MVM_TX_FIFO_MCAST : IWL_MVM_TX_FIFO_BE,
238926d6c16bSSara Sharon 		.sta_id = msta->sta_id,
23906508de03SIlan Peer 		.tid = 0,
239126d6c16bSSara Sharon 		.aggregate = false,
239226d6c16bSSara Sharon 		.frame_limit = IWL_FRAME_LIMIT,
239326d6c16bSSara Sharon 	};
239426d6c16bSSara Sharon 	unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
239526d6c16bSSara Sharon 	int ret;
239626d6c16bSSara Sharon 
239726d6c16bSSara Sharon 	lockdep_assert_held(&mvm->mutex);
239826d6c16bSSara Sharon 
2399ee48b722SLiad Kaufman 	if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2400ee48b722SLiad Kaufman 		    vif->type != NL80211_IFTYPE_ADHOC))
240126d6c16bSSara Sharon 		return -ENOTSUPP;
240226d6c16bSSara Sharon 
2403ced19f26SSara Sharon 	/*
2404fc07bd8cSSara Sharon 	 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2405fc07bd8cSSara Sharon 	 * invalid, so make sure we use the queue we want.
2406fc07bd8cSSara Sharon 	 * Note that this is done here as we want to avoid making DQA
2407fc07bd8cSSara Sharon 	 * changes in mac80211 layer.
2408fc07bd8cSSara Sharon 	 */
2409cfbc6c4cSSara Sharon 	if (vif->type == NL80211_IFTYPE_ADHOC)
2410cfbc6c4cSSara Sharon 		mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2411fc07bd8cSSara Sharon 
2412fc07bd8cSSara Sharon 	/*
2413ced19f26SSara Sharon 	 * While in previous FWs we had to exclude cab queue from TFD queue
2414ced19f26SSara Sharon 	 * mask, now it is needed as any other queue.
2415ced19f26SSara Sharon 	 */
2416ced19f26SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm) &&
2417ced19f26SSara Sharon 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2418cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2419cfbc6c4cSSara Sharon 				   timeout);
2420cfbc6c4cSSara Sharon 		msta->tfd_queue_msk |= BIT(mvmvif->cab_queue);
2421ced19f26SSara Sharon 	}
242226d6c16bSSara Sharon 	ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
242326d6c16bSSara Sharon 					 mvmvif->id, mvmvif->color);
242491cf5dedSJohannes Berg 	if (ret)
242591cf5dedSJohannes Berg 		goto err;
242626d6c16bSSara Sharon 
242726d6c16bSSara Sharon 	/*
242826d6c16bSSara Sharon 	 * Enable cab queue after the ADD_STA command is sent.
24292f7a3863SLuca Coelho 	 * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
2430ced19f26SSara Sharon 	 * command with unknown station id, and for FW that doesn't support
2431ced19f26SSara Sharon 	 * station API since the cab queue is not included in the
2432ced19f26SSara Sharon 	 * tfd_queue_mask.
243326d6c16bSSara Sharon 	 */
2434310181ecSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
2435*09369983SJohannes Berg 		int queue = iwl_mvm_tvqm_enable_txq(mvm, NULL, msta->sta_id,
2436*09369983SJohannes Berg 						    0, timeout);
243791cf5dedSJohannes Berg 		if (queue < 0) {
243891cf5dedSJohannes Berg 			ret = queue;
243991cf5dedSJohannes Berg 			goto err;
244091cf5dedSJohannes Berg 		}
2441e2af3fabSSara Sharon 		mvmvif->cab_queue = queue;
2442ced19f26SSara Sharon 	} else if (!fw_has_api(&mvm->fw->ucode_capa,
2443fc07bd8cSSara Sharon 			       IWL_UCODE_TLV_API_STA_TYPE))
2444cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2445cfbc6c4cSSara Sharon 				   timeout);
244626d6c16bSSara Sharon 
244726d6c16bSSara Sharon 	return 0;
244891cf5dedSJohannes Berg err:
244991cf5dedSJohannes Berg 	iwl_mvm_dealloc_int_sta(mvm, msta);
245091cf5dedSJohannes Berg 	return ret;
245126d6c16bSSara Sharon }
245226d6c16bSSara Sharon 
245328916a16SEmmanuel Grumbach static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
245428916a16SEmmanuel Grumbach 				    struct ieee80211_key_conf *keyconf,
245528916a16SEmmanuel Grumbach 				    bool mcast)
245628916a16SEmmanuel Grumbach {
245728916a16SEmmanuel Grumbach 	union {
245828916a16SEmmanuel Grumbach 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
245928916a16SEmmanuel Grumbach 		struct iwl_mvm_add_sta_key_cmd cmd;
246028916a16SEmmanuel Grumbach 	} u = {};
246128916a16SEmmanuel Grumbach 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
246228916a16SEmmanuel Grumbach 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
246328916a16SEmmanuel Grumbach 	__le16 key_flags;
246428916a16SEmmanuel Grumbach 	int ret, size;
246528916a16SEmmanuel Grumbach 	u32 status;
246628916a16SEmmanuel Grumbach 
246728916a16SEmmanuel Grumbach 	/* This is a valid situation for GTK removal */
246828916a16SEmmanuel Grumbach 	if (sta_id == IWL_MVM_INVALID_STA)
246928916a16SEmmanuel Grumbach 		return 0;
247028916a16SEmmanuel Grumbach 
247128916a16SEmmanuel Grumbach 	key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
247228916a16SEmmanuel Grumbach 				 STA_KEY_FLG_KEYID_MSK);
247328916a16SEmmanuel Grumbach 	key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
247428916a16SEmmanuel Grumbach 	key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
247528916a16SEmmanuel Grumbach 
247628916a16SEmmanuel Grumbach 	if (mcast)
247728916a16SEmmanuel Grumbach 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
247828916a16SEmmanuel Grumbach 
247928916a16SEmmanuel Grumbach 	/*
248028916a16SEmmanuel Grumbach 	 * The fields assigned here are in the same location at the start
248128916a16SEmmanuel Grumbach 	 * of the command, so we can do this union trick.
248228916a16SEmmanuel Grumbach 	 */
248328916a16SEmmanuel Grumbach 	u.cmd.common.key_flags = key_flags;
248428916a16SEmmanuel Grumbach 	u.cmd.common.key_offset = keyconf->hw_key_idx;
248528916a16SEmmanuel Grumbach 	u.cmd.common.sta_id = sta_id;
248628916a16SEmmanuel Grumbach 
248728916a16SEmmanuel Grumbach 	size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
248828916a16SEmmanuel Grumbach 
248928916a16SEmmanuel Grumbach 	status = ADD_STA_SUCCESS;
249028916a16SEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
249128916a16SEmmanuel Grumbach 					  &status);
249228916a16SEmmanuel Grumbach 
249328916a16SEmmanuel Grumbach 	switch (status) {
249428916a16SEmmanuel Grumbach 	case ADD_STA_SUCCESS:
249528916a16SEmmanuel Grumbach 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
249628916a16SEmmanuel Grumbach 		break;
249728916a16SEmmanuel Grumbach 	default:
249828916a16SEmmanuel Grumbach 		ret = -EIO;
249928916a16SEmmanuel Grumbach 		IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
250028916a16SEmmanuel Grumbach 		break;
250128916a16SEmmanuel Grumbach 	}
250228916a16SEmmanuel Grumbach 
250328916a16SEmmanuel Grumbach 	return ret;
250428916a16SEmmanuel Grumbach }
250528916a16SEmmanuel Grumbach 
250626d6c16bSSara Sharon /*
250726d6c16bSSara Sharon  * Send the FW a request to remove the station from it's internal data
250826d6c16bSSara Sharon  * structures, and in addition remove it from the local data structure.
250926d6c16bSSara Sharon  */
251026d6c16bSSara Sharon int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
251126d6c16bSSara Sharon {
251226d6c16bSSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
251326d6c16bSSara Sharon 	int ret;
251426d6c16bSSara Sharon 
251526d6c16bSSara Sharon 	lockdep_assert_held(&mvm->mutex);
251626d6c16bSSara Sharon 
2517f9084775SNathan Errera 	iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true);
2518d49394a1SSara Sharon 
2519c5a976cfSJohannes Berg 	iwl_mvm_disable_txq(mvm, NULL, mvmvif->mcast_sta.sta_id,
2520c5a976cfSJohannes Berg 			    &mvmvif->cab_queue, 0);
252126d6c16bSSara Sharon 
252226d6c16bSSara Sharon 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
252326d6c16bSSara Sharon 	if (ret)
252426d6c16bSSara Sharon 		IWL_WARN(mvm, "Failed sending remove station\n");
252526d6c16bSSara Sharon 
252626d6c16bSSara Sharon 	return ret;
252726d6c16bSSara Sharon }
252826d6c16bSSara Sharon 
2529b915c101SSara Sharon static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
253010b2b201SSara Sharon {
25315e1688ceSJohannes Berg 	struct iwl_mvm_delba_data notif = {
25325e1688ceSJohannes Berg 		.baid = baid,
2533b915c101SSara Sharon 	};
25345e1688ceSJohannes Berg 
25355e1688ceSJohannes Berg 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_NOTIF_DEL_BA, true,
25365e1688ceSJohannes Berg 					&notif, sizeof(notif));
253710b2b201SSara Sharon };
253810b2b201SSara Sharon 
2539b915c101SSara Sharon static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2540b915c101SSara Sharon 				 struct iwl_mvm_baid_data *data)
2541b915c101SSara Sharon {
2542b915c101SSara Sharon 	int i;
2543b915c101SSara Sharon 
2544b915c101SSara Sharon 	iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2545b915c101SSara Sharon 
2546b915c101SSara Sharon 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2547b915c101SSara Sharon 		int j;
2548b915c101SSara Sharon 		struct iwl_mvm_reorder_buffer *reorder_buf =
2549b915c101SSara Sharon 			&data->reorder_buf[i];
2550dfdddd92SJohannes Berg 		struct iwl_mvm_reorder_buf_entry *entries =
2551dfdddd92SJohannes Berg 			&data->entries[i * data->entries_per_queue];
2552b915c101SSara Sharon 
25530690405fSSara Sharon 		spin_lock_bh(&reorder_buf->lock);
25540690405fSSara Sharon 		if (likely(!reorder_buf->num_stored)) {
25550690405fSSara Sharon 			spin_unlock_bh(&reorder_buf->lock);
2556b915c101SSara Sharon 			continue;
25570690405fSSara Sharon 		}
2558b915c101SSara Sharon 
2559b915c101SSara Sharon 		/*
2560b915c101SSara Sharon 		 * This shouldn't happen in regular DELBA since the internal
2561b915c101SSara Sharon 		 * delBA notification should trigger a release of all frames in
2562b915c101SSara Sharon 		 * the reorder buffer.
2563b915c101SSara Sharon 		 */
2564b915c101SSara Sharon 		WARN_ON(1);
2565b915c101SSara Sharon 
2566b915c101SSara Sharon 		for (j = 0; j < reorder_buf->buf_size; j++)
2567dfdddd92SJohannes Berg 			__skb_queue_purge(&entries[j].e.frames);
25680690405fSSara Sharon 		/*
25690690405fSSara Sharon 		 * Prevent timer re-arm. This prevents a very far fetched case
25700690405fSSara Sharon 		 * where we timed out on the notification. There may be prior
25710690405fSSara Sharon 		 * RX frames pending in the RX queue before the notification
25720690405fSSara Sharon 		 * that might get processed between now and the actual deletion
25730690405fSSara Sharon 		 * and we would re-arm the timer although we are deleting the
25740690405fSSara Sharon 		 * reorder buffer.
25750690405fSSara Sharon 		 */
25760690405fSSara Sharon 		reorder_buf->removed = true;
25770690405fSSara Sharon 		spin_unlock_bh(&reorder_buf->lock);
25780690405fSSara Sharon 		del_timer_sync(&reorder_buf->reorder_timer);
2579b915c101SSara Sharon 	}
2580b915c101SSara Sharon }
2581b915c101SSara Sharon 
2582b915c101SSara Sharon static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2583b915c101SSara Sharon 					struct iwl_mvm_baid_data *data,
2584514c3069SLuca Coelho 					u16 ssn, u16 buf_size)
2585b915c101SSara Sharon {
2586b915c101SSara Sharon 	int i;
2587b915c101SSara Sharon 
2588b915c101SSara Sharon 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2589b915c101SSara Sharon 		struct iwl_mvm_reorder_buffer *reorder_buf =
2590b915c101SSara Sharon 			&data->reorder_buf[i];
2591dfdddd92SJohannes Berg 		struct iwl_mvm_reorder_buf_entry *entries =
2592dfdddd92SJohannes Berg 			&data->entries[i * data->entries_per_queue];
2593b915c101SSara Sharon 		int j;
2594b915c101SSara Sharon 
2595b915c101SSara Sharon 		reorder_buf->num_stored = 0;
2596b915c101SSara Sharon 		reorder_buf->head_sn = ssn;
2597b915c101SSara Sharon 		reorder_buf->buf_size = buf_size;
25980690405fSSara Sharon 		/* rx reorder timer */
25998cef5344SKees Cook 		timer_setup(&reorder_buf->reorder_timer,
26008cef5344SKees Cook 			    iwl_mvm_reorder_timer_expired, 0);
26010690405fSSara Sharon 		spin_lock_init(&reorder_buf->lock);
26020690405fSSara Sharon 		reorder_buf->mvm = mvm;
2603b915c101SSara Sharon 		reorder_buf->queue = i;
26045d43eab6SSara Sharon 		reorder_buf->valid = false;
2605b915c101SSara Sharon 		for (j = 0; j < reorder_buf->buf_size; j++)
2606dfdddd92SJohannes Berg 			__skb_queue_head_init(&entries[j].e.frames);
2607b915c101SSara Sharon 	}
260810b2b201SSara Sharon }
260910b2b201SSara Sharon 
261097f70c56SJohannes Berg static int iwl_mvm_fw_baid_op_sta(struct iwl_mvm *mvm,
261197f70c56SJohannes Berg 				  struct iwl_mvm_sta *mvm_sta,
261297f70c56SJohannes Berg 				  bool start, int tid, u16 ssn,
261397f70c56SJohannes Berg 				  u16 buf_size)
26140f3a4e48SJohannes Berg {
26150f3a4e48SJohannes Berg 	struct iwl_mvm_add_sta_cmd cmd = {
26160f3a4e48SJohannes Berg 		.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
26170f3a4e48SJohannes Berg 		.sta_id = mvm_sta->sta_id,
26180f3a4e48SJohannes Berg 		.add_modify = STA_MODE_MODIFY,
26190f3a4e48SJohannes Berg 	};
26200f3a4e48SJohannes Berg 	u32 status;
26210f3a4e48SJohannes Berg 	int ret;
26220f3a4e48SJohannes Berg 
26230f3a4e48SJohannes Berg 	if (start) {
26240f3a4e48SJohannes Berg 		cmd.add_immediate_ba_tid = tid;
26250f3a4e48SJohannes Berg 		cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
26260f3a4e48SJohannes Berg 		cmd.rx_ba_window = cpu_to_le16(buf_size);
26270f3a4e48SJohannes Berg 		cmd.modify_mask = STA_MODIFY_ADD_BA_TID;
26280f3a4e48SJohannes Berg 	} else {
26290f3a4e48SJohannes Berg 		cmd.remove_immediate_ba_tid = tid;
26300f3a4e48SJohannes Berg 		cmd.modify_mask = STA_MODIFY_REMOVE_BA_TID;
26310f3a4e48SJohannes Berg 	}
26320f3a4e48SJohannes Berg 
26330f3a4e48SJohannes Berg 	status = ADD_STA_SUCCESS;
26340f3a4e48SJohannes Berg 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
26350f3a4e48SJohannes Berg 					  iwl_mvm_add_sta_cmd_size(mvm),
26360f3a4e48SJohannes Berg 					  &cmd, &status);
26370f3a4e48SJohannes Berg 	if (ret)
26380f3a4e48SJohannes Berg 		return ret;
26390f3a4e48SJohannes Berg 
26400f3a4e48SJohannes Berg 	switch (status & IWL_ADD_STA_STATUS_MASK) {
26410f3a4e48SJohannes Berg 	case ADD_STA_SUCCESS:
26420f3a4e48SJohannes Berg 		IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
26430f3a4e48SJohannes Berg 			     start ? "start" : "stopp");
26440f3a4e48SJohannes Berg 		if (WARN_ON(start && iwl_mvm_has_new_rx_api(mvm) &&
26450f3a4e48SJohannes Berg 			    !(status & IWL_ADD_STA_BAID_VALID_MASK)))
26460f3a4e48SJohannes Berg 			return -EINVAL;
26470f3a4e48SJohannes Berg 		return u32_get_bits(status, IWL_ADD_STA_BAID_MASK);
26480f3a4e48SJohannes Berg 	case ADD_STA_IMMEDIATE_BA_FAILURE:
26490f3a4e48SJohannes Berg 		IWL_WARN(mvm, "RX BA Session refused by fw\n");
26500f3a4e48SJohannes Berg 		return -ENOSPC;
26510f3a4e48SJohannes Berg 	default:
26520f3a4e48SJohannes Berg 		IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
26530f3a4e48SJohannes Berg 			start ? "start" : "stopp", status);
26540f3a4e48SJohannes Berg 		return -EIO;
26550f3a4e48SJohannes Berg 	}
26560f3a4e48SJohannes Berg }
26570f3a4e48SJohannes Berg 
265897f70c56SJohannes Berg static int iwl_mvm_fw_baid_op_cmd(struct iwl_mvm *mvm,
265997f70c56SJohannes Berg 				  struct iwl_mvm_sta *mvm_sta,
266097f70c56SJohannes Berg 				  bool start, int tid, u16 ssn,
266197f70c56SJohannes Berg 				  u16 buf_size, int baid)
266297f70c56SJohannes Berg {
266397f70c56SJohannes Berg 	struct iwl_rx_baid_cfg_cmd cmd = {
266497f70c56SJohannes Berg 		.action = start ? cpu_to_le32(IWL_RX_BAID_ACTION_ADD) :
266597f70c56SJohannes Berg 				  cpu_to_le32(IWL_RX_BAID_ACTION_REMOVE),
266697f70c56SJohannes Berg 	};
266797f70c56SJohannes Berg 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, RX_BAID_ALLOCATION_CONFIG_CMD);
266897f70c56SJohannes Berg 	int ret;
266997f70c56SJohannes Berg 
267097f70c56SJohannes Berg 	BUILD_BUG_ON(sizeof(struct iwl_rx_baid_cfg_resp) != sizeof(baid));
267197f70c56SJohannes Berg 
267297f70c56SJohannes Berg 	if (start) {
267397f70c56SJohannes Berg 		cmd.alloc.sta_id_mask = cpu_to_le32(BIT(mvm_sta->sta_id));
267497f70c56SJohannes Berg 		cmd.alloc.tid = tid;
267597f70c56SJohannes Berg 		cmd.alloc.ssn = cpu_to_le16(ssn);
267697f70c56SJohannes Berg 		cmd.alloc.win_size = cpu_to_le16(buf_size);
267797f70c56SJohannes Berg 		baid = -EIO;
267826de4c8bSJohannes Berg 	} else if (iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 1) == 1) {
267926de4c8bSJohannes Berg 		cmd.remove_v1.baid = cpu_to_le32(baid);
268026de4c8bSJohannes Berg 		BUILD_BUG_ON(sizeof(cmd.remove_v1) > sizeof(cmd.remove));
268197f70c56SJohannes Berg 	} else {
268226de4c8bSJohannes Berg 		cmd.remove.sta_id_mask = cpu_to_le32(BIT(mvm_sta->sta_id));
268326de4c8bSJohannes Berg 		cmd.remove.tid = cpu_to_le32(tid);
268497f70c56SJohannes Berg 	}
268597f70c56SJohannes Berg 
268697f70c56SJohannes Berg 	ret = iwl_mvm_send_cmd_pdu_status(mvm, cmd_id, sizeof(cmd),
268797f70c56SJohannes Berg 					  &cmd, &baid);
268897f70c56SJohannes Berg 	if (ret)
268997f70c56SJohannes Berg 		return ret;
269097f70c56SJohannes Berg 
269197f70c56SJohannes Berg 	if (!start) {
269297f70c56SJohannes Berg 		/* ignore firmware baid on remove */
269397f70c56SJohannes Berg 		baid = 0;
269497f70c56SJohannes Berg 	}
269597f70c56SJohannes Berg 
269697f70c56SJohannes Berg 	IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
269797f70c56SJohannes Berg 		     start ? "start" : "stopp");
269897f70c56SJohannes Berg 
269997f70c56SJohannes Berg 	if (baid < 0 || baid >= ARRAY_SIZE(mvm->baid_map))
270097f70c56SJohannes Berg 		return -EINVAL;
270197f70c56SJohannes Berg 
270297f70c56SJohannes Berg 	return baid;
270397f70c56SJohannes Berg }
270497f70c56SJohannes Berg 
270597f70c56SJohannes Berg static int iwl_mvm_fw_baid_op(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvm_sta,
270697f70c56SJohannes Berg 			      bool start, int tid, u16 ssn, u16 buf_size,
270797f70c56SJohannes Berg 			      int baid)
270897f70c56SJohannes Berg {
270997f70c56SJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
271097f70c56SJohannes Berg 			IWL_UCODE_TLV_CAPA_BAID_ML_SUPPORT))
271197f70c56SJohannes Berg 		return iwl_mvm_fw_baid_op_cmd(mvm, mvm_sta, start,
271297f70c56SJohannes Berg 					      tid, ssn, buf_size, baid);
271397f70c56SJohannes Berg 
271497f70c56SJohannes Berg 	return iwl_mvm_fw_baid_op_sta(mvm, mvm_sta, start,
271597f70c56SJohannes Berg 				      tid, ssn, buf_size);
271697f70c56SJohannes Berg }
271797f70c56SJohannes Berg 
2718e705c121SKalle Valo int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2719514c3069SLuca Coelho 		       int tid, u16 ssn, bool start, u16 buf_size, u16 timeout)
2720e705c121SKalle Valo {
2721e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
272210b2b201SSara Sharon 	struct iwl_mvm_baid_data *baid_data = NULL;
27230f3a4e48SJohannes Berg 	int ret, baid;
2724ded5ded3SEmmanuel Grumbach 	u32 max_ba_id_sessions = iwl_mvm_has_new_tx_api(mvm) ? IWL_MAX_BAID :
2725ded5ded3SEmmanuel Grumbach 							       IWL_MAX_BAID_OLD;
2726e705c121SKalle Valo 
2727e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2728e705c121SKalle Valo 
2729ded5ded3SEmmanuel Grumbach 	if (start && mvm->rx_ba_sessions >= max_ba_id_sessions) {
2730e705c121SKalle Valo 		IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2731e705c121SKalle Valo 		return -ENOSPC;
2732e705c121SKalle Valo 	}
2733e705c121SKalle Valo 
273410b2b201SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) && start) {
2735dfdddd92SJohannes Berg 		u16 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2736dfdddd92SJohannes Berg 
2737dfdddd92SJohannes Berg 		/* sparse doesn't like the __align() so don't check */
2738dfdddd92SJohannes Berg #ifndef __CHECKER__
2739dfdddd92SJohannes Berg 		/*
2740dfdddd92SJohannes Berg 		 * The division below will be OK if either the cache line size
2741dfdddd92SJohannes Berg 		 * can be divided by the entry size (ALIGN will round up) or if
2742dfdddd92SJohannes Berg 		 * if the entry size can be divided by the cache line size, in
2743dfdddd92SJohannes Berg 		 * which case the ALIGN() will do nothing.
2744dfdddd92SJohannes Berg 		 */
2745dfdddd92SJohannes Berg 		BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2746dfdddd92SJohannes Berg 			     sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2747dfdddd92SJohannes Berg #endif
2748dfdddd92SJohannes Berg 
2749dfdddd92SJohannes Berg 		/*
2750dfdddd92SJohannes Berg 		 * Upward align the reorder buffer size to fill an entire cache
2751dfdddd92SJohannes Berg 		 * line for each queue, to avoid sharing cache lines between
2752dfdddd92SJohannes Berg 		 * different queues.
2753dfdddd92SJohannes Berg 		 */
2754dfdddd92SJohannes Berg 		reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2755dfdddd92SJohannes Berg 
275610b2b201SSara Sharon 		/*
275710b2b201SSara Sharon 		 * Allocate here so if allocation fails we can bail out early
275810b2b201SSara Sharon 		 * before starting the BA session in the firmware
275910b2b201SSara Sharon 		 */
2760b915c101SSara Sharon 		baid_data = kzalloc(sizeof(*baid_data) +
2761b915c101SSara Sharon 				    mvm->trans->num_rx_queues *
2762dfdddd92SJohannes Berg 				    reorder_buf_size,
2763b915c101SSara Sharon 				    GFP_KERNEL);
276410b2b201SSara Sharon 		if (!baid_data)
276510b2b201SSara Sharon 			return -ENOMEM;
2766dfdddd92SJohannes Berg 
2767dfdddd92SJohannes Berg 		/*
2768dfdddd92SJohannes Berg 		 * This division is why we need the above BUILD_BUG_ON(),
2769dfdddd92SJohannes Berg 		 * if that doesn't hold then this will not be right.
2770dfdddd92SJohannes Berg 		 */
2771dfdddd92SJohannes Berg 		baid_data->entries_per_queue =
2772dfdddd92SJohannes Berg 			reorder_buf_size / sizeof(baid_data->entries[0]);
277310b2b201SSara Sharon 	}
277410b2b201SSara Sharon 
277597f70c56SJohannes Berg 	if (iwl_mvm_has_new_rx_api(mvm) && !start) {
277697f70c56SJohannes Berg 		baid = mvm_sta->tid_to_baid[tid];
277797f70c56SJohannes Berg 	} else {
277897f70c56SJohannes Berg 		/* we don't really need it in this case */
277997f70c56SJohannes Berg 		baid = -1;
278097f70c56SJohannes Berg 	}
278197f70c56SJohannes Berg 
27823538c809SLuca Coelho 	/* Don't send command to remove (start=0) BAID during restart */
27833538c809SLuca Coelho 	if (start || !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
278497f70c56SJohannes Berg 		baid = iwl_mvm_fw_baid_op(mvm, mvm_sta, start, tid, ssn, buf_size,
278597f70c56SJohannes Berg 					  baid);
27863538c809SLuca Coelho 
27870f3a4e48SJohannes Berg 	if (baid < 0) {
27880f3a4e48SJohannes Berg 		ret = baid;
278910b2b201SSara Sharon 		goto out_free;
2790e705c121SKalle Valo 	}
2791e705c121SKalle Valo 
279210b2b201SSara Sharon 	if (start) {
2793e705c121SKalle Valo 		mvm->rx_ba_sessions++;
279410b2b201SSara Sharon 
279510b2b201SSara Sharon 		if (!iwl_mvm_has_new_rx_api(mvm))
279610b2b201SSara Sharon 			return 0;
279710b2b201SSara Sharon 
279810b2b201SSara Sharon 		baid_data->baid = baid;
279910b2b201SSara Sharon 		baid_data->timeout = timeout;
280010b2b201SSara Sharon 		baid_data->last_rx = jiffies;
28018cef5344SKees Cook 		baid_data->rcu_ptr = &mvm->baid_map[baid];
28028cef5344SKees Cook 		timer_setup(&baid_data->session_timer,
28038cef5344SKees Cook 			    iwl_mvm_rx_agg_session_expired, 0);
280410b2b201SSara Sharon 		baid_data->mvm = mvm;
280510b2b201SSara Sharon 		baid_data->tid = tid;
280610b2b201SSara Sharon 		baid_data->sta_id = mvm_sta->sta_id;
280710b2b201SSara Sharon 
280810b2b201SSara Sharon 		mvm_sta->tid_to_baid[tid] = baid;
280910b2b201SSara Sharon 		if (timeout)
281010b2b201SSara Sharon 			mod_timer(&baid_data->session_timer,
281110b2b201SSara Sharon 				  TU_TO_EXP_TIME(timeout * 2));
281210b2b201SSara Sharon 
28133f1c4c58SSara Sharon 		iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size);
281410b2b201SSara Sharon 		/*
281510b2b201SSara Sharon 		 * protect the BA data with RCU to cover a case where our
281610b2b201SSara Sharon 		 * internal RX sync mechanism will timeout (not that it's
281710b2b201SSara Sharon 		 * supposed to happen) and we will free the session data while
281810b2b201SSara Sharon 		 * RX is being processed in parallel
281910b2b201SSara Sharon 		 */
282035263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
282135263a03SSara Sharon 			     mvm_sta->sta_id, tid, baid);
282210b2b201SSara Sharon 		WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
282310b2b201SSara Sharon 		rcu_assign_pointer(mvm->baid_map[baid], baid_data);
282460dec523SSara Sharon 	} else  {
28250f3a4e48SJohannes Berg 		baid = mvm_sta->tid_to_baid[tid];
282610b2b201SSara Sharon 
282760dec523SSara Sharon 		if (mvm->rx_ba_sessions > 0)
2828e705c121SKalle Valo 			/* check that restart flow didn't zero the counter */
2829e705c121SKalle Valo 			mvm->rx_ba_sessions--;
283010b2b201SSara Sharon 		if (!iwl_mvm_has_new_rx_api(mvm))
283110b2b201SSara Sharon 			return 0;
2832e705c121SKalle Valo 
283310b2b201SSara Sharon 		if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
283410b2b201SSara Sharon 			return -EINVAL;
283510b2b201SSara Sharon 
283610b2b201SSara Sharon 		baid_data = rcu_access_pointer(mvm->baid_map[baid]);
283710b2b201SSara Sharon 		if (WARN_ON(!baid_data))
283810b2b201SSara Sharon 			return -EINVAL;
283910b2b201SSara Sharon 
284010b2b201SSara Sharon 		/* synchronize all rx queues so we can safely delete */
2841b915c101SSara Sharon 		iwl_mvm_free_reorder(mvm, baid_data);
2842292a089dSSteven Rostedt (Google) 		timer_shutdown_sync(&baid_data->session_timer);
284310b2b201SSara Sharon 		RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
284410b2b201SSara Sharon 		kfree_rcu(baid_data, rcu_head);
284535263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
28462438d430SJohannes Berg 
28472438d430SJohannes Berg 		/*
28482438d430SJohannes Berg 		 * After we've deleted it, do another queue sync
28492438d430SJohannes Berg 		 * so if an IWL_MVM_RXQ_NSSN_SYNC was concurrently
28502438d430SJohannes Berg 		 * running it won't find a new session in the old
28512438d430SJohannes Berg 		 * BAID. It can find the NULL pointer for the BAID,
28522438d430SJohannes Berg 		 * but we must not have it find a different session.
28532438d430SJohannes Berg 		 */
28542438d430SJohannes Berg 		iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY,
28552438d430SJohannes Berg 						true, NULL, 0);
285610b2b201SSara Sharon 	}
285710b2b201SSara Sharon 	return 0;
285810b2b201SSara Sharon 
285910b2b201SSara Sharon out_free:
286010b2b201SSara Sharon 	kfree(baid_data);
2861e705c121SKalle Valo 	return ret;
2862e705c121SKalle Valo }
2863e705c121SKalle Valo 
28649794c64fSLiad Kaufman int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2865e705c121SKalle Valo 		       int tid, u8 queue, bool start)
2866e705c121SKalle Valo {
2867e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2868e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
2869e705c121SKalle Valo 	int ret;
2870e705c121SKalle Valo 	u32 status;
2871e705c121SKalle Valo 
2872e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2873e705c121SKalle Valo 
2874e705c121SKalle Valo 	if (start) {
2875e705c121SKalle Valo 		mvm_sta->tfd_queue_msk |= BIT(queue);
2876e705c121SKalle Valo 		mvm_sta->tid_disable_agg &= ~BIT(tid);
2877e705c121SKalle Valo 	} else {
2878cf961e16SLiad Kaufman 		/* In DQA-mode the queue isn't removed on agg termination */
2879e705c121SKalle Valo 		mvm_sta->tid_disable_agg |= BIT(tid);
2880e705c121SKalle Valo 	}
2881e705c121SKalle Valo 
2882e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2883e705c121SKalle Valo 	cmd.sta_id = mvm_sta->sta_id;
2884e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
2885bb49701bSSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm))
2886bb49701bSSara Sharon 		cmd.modify_mask = STA_MODIFY_QUEUES;
2887bb49701bSSara Sharon 	cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
2888e705c121SKalle Valo 	cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2889e705c121SKalle Valo 	cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2890e705c121SKalle Valo 
2891e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
2892854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2893854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
2894e705c121SKalle Valo 					  &cmd, &status);
2895e705c121SKalle Valo 	if (ret)
2896e705c121SKalle Valo 		return ret;
2897e705c121SKalle Valo 
2898837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2899e705c121SKalle Valo 	case ADD_STA_SUCCESS:
2900e705c121SKalle Valo 		break;
2901e705c121SKalle Valo 	default:
2902e705c121SKalle Valo 		ret = -EIO;
2903e705c121SKalle Valo 		IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2904e705c121SKalle Valo 			start ? "start" : "stopp", status);
2905e705c121SKalle Valo 		break;
2906e705c121SKalle Valo 	}
2907e705c121SKalle Valo 
2908e705c121SKalle Valo 	return ret;
2909e705c121SKalle Valo }
2910e705c121SKalle Valo 
2911e705c121SKalle Valo const u8 tid_to_mac80211_ac[] = {
2912e705c121SKalle Valo 	IEEE80211_AC_BE,
2913e705c121SKalle Valo 	IEEE80211_AC_BK,
2914e705c121SKalle Valo 	IEEE80211_AC_BK,
2915e705c121SKalle Valo 	IEEE80211_AC_BE,
2916e705c121SKalle Valo 	IEEE80211_AC_VI,
2917e705c121SKalle Valo 	IEEE80211_AC_VI,
2918e705c121SKalle Valo 	IEEE80211_AC_VO,
2919e705c121SKalle Valo 	IEEE80211_AC_VO,
29209794c64fSLiad Kaufman 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2921e705c121SKalle Valo };
2922e705c121SKalle Valo 
2923e705c121SKalle Valo static const u8 tid_to_ucode_ac[] = {
2924e705c121SKalle Valo 	AC_BE,
2925e705c121SKalle Valo 	AC_BK,
2926e705c121SKalle Valo 	AC_BK,
2927e705c121SKalle Valo 	AC_BE,
2928e705c121SKalle Valo 	AC_VI,
2929e705c121SKalle Valo 	AC_VI,
2930e705c121SKalle Valo 	AC_VO,
2931e705c121SKalle Valo 	AC_VO,
2932e705c121SKalle Valo };
2933e705c121SKalle Valo 
2934e705c121SKalle Valo int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2935e705c121SKalle Valo 			     struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2936e705c121SKalle Valo {
2937e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2938e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data;
2939dd32162dSLiad Kaufman 	u16 normalized_ssn;
2940b0d795a9SMordechay Goodstein 	u16 txq_id;
2941e705c121SKalle Valo 	int ret;
2942e705c121SKalle Valo 
2943e705c121SKalle Valo 	if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2944e705c121SKalle Valo 		return -EINVAL;
2945e705c121SKalle Valo 
2946bd800e41SNaftali Goldstein 	if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
2947bd800e41SNaftali Goldstein 	    mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2948bd800e41SNaftali Goldstein 		IWL_ERR(mvm,
2949bd800e41SNaftali Goldstein 			"Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
2950e705c121SKalle Valo 			mvmsta->tid_data[tid].state);
2951e705c121SKalle Valo 		return -ENXIO;
2952e705c121SKalle Valo 	}
2953e705c121SKalle Valo 
2954e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2955e705c121SKalle Valo 
2956bd8f3fc6SLiad Kaufman 	if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE &&
2957bd8f3fc6SLiad Kaufman 	    iwl_mvm_has_new_tx_api(mvm)) {
2958bd8f3fc6SLiad Kaufman 		u8 ac = tid_to_mac80211_ac[tid];
2959bd8f3fc6SLiad Kaufman 
2960bd8f3fc6SLiad Kaufman 		ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
2961bd8f3fc6SLiad Kaufman 		if (ret)
2962bd8f3fc6SLiad Kaufman 			return ret;
2963bd8f3fc6SLiad Kaufman 	}
2964bd8f3fc6SLiad Kaufman 
2965e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2966e705c121SKalle Valo 
2967cf961e16SLiad Kaufman 	/*
2968cf961e16SLiad Kaufman 	 * Note the possible cases:
29694a6d2e52SAvraham Stern 	 *  1. An enabled TXQ - TXQ needs to become agg'ed
29704a6d2e52SAvraham Stern 	 *  2. The TXQ hasn't yet been enabled, so find a free one and mark
29714a6d2e52SAvraham Stern 	 *	it as reserved
2972cf961e16SLiad Kaufman 	 */
2973cf961e16SLiad Kaufman 	txq_id = mvmsta->tid_data[tid].txq_id;
297434e10860SSara Sharon 	if (txq_id == IWL_MVM_INVALID_QUEUE) {
2975b0d795a9SMordechay Goodstein 		ret = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2976c8f54701SJohannes Berg 					      IWL_MVM_DQA_MIN_DATA_QUEUE,
2977c8f54701SJohannes Berg 					      IWL_MVM_DQA_MAX_DATA_QUEUE);
2978b0d795a9SMordechay Goodstein 		if (ret < 0) {
2979e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to allocate agg queue\n");
2980f3f240f9SJohannes Berg 			goto out;
2981e705c121SKalle Valo 		}
2982cf961e16SLiad Kaufman 
2983b0d795a9SMordechay Goodstein 		txq_id = ret;
2984b0d795a9SMordechay Goodstein 
2985cf961e16SLiad Kaufman 		/* TXQ hasn't yet been enabled, so mark it only as reserved */
2986cf961e16SLiad Kaufman 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2987b0d795a9SMordechay Goodstein 	} else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) {
2988b0d795a9SMordechay Goodstein 		ret = -ENXIO;
2989b0d795a9SMordechay Goodstein 		IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n",
2990b0d795a9SMordechay Goodstein 			tid, IWL_MAX_HW_QUEUES - 1);
2991b0d795a9SMordechay Goodstein 		goto out;
2992b0d795a9SMordechay Goodstein 
29934a6d2e52SAvraham Stern 	} else if (unlikely(mvm->queue_info[txq_id].status ==
29944a6d2e52SAvraham Stern 			    IWL_MVM_QUEUE_SHARED)) {
29954a6d2e52SAvraham Stern 		ret = -ENXIO;
29964a6d2e52SAvraham Stern 		IWL_DEBUG_TX_QUEUES(mvm,
29974a6d2e52SAvraham Stern 				    "Can't start tid %d agg on shared queue!\n",
29984a6d2e52SAvraham Stern 				    tid);
2999f3f240f9SJohannes Berg 		goto out;
3000cf961e16SLiad Kaufman 	}
30019f9af3d7SLiad Kaufman 
3002cf961e16SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm,
3003cf961e16SLiad Kaufman 			    "AGG for tid %d will be on queue #%d\n",
3004cf961e16SLiad Kaufman 			    tid, txq_id);
3005cf961e16SLiad Kaufman 
3006e705c121SKalle Valo 	tid_data = &mvmsta->tid_data[tid];
3007e705c121SKalle Valo 	tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3008e705c121SKalle Valo 	tid_data->txq_id = txq_id;
3009e705c121SKalle Valo 	*ssn = tid_data->ssn;
3010e705c121SKalle Valo 
3011e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm,
3012e705c121SKalle Valo 			    "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
3013e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->ssn,
3014e705c121SKalle Valo 			    tid_data->next_reclaimed);
3015e705c121SKalle Valo 
3016dd32162dSLiad Kaufman 	/*
30172f7a3863SLuca Coelho 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
3018dd32162dSLiad Kaufman 	 * to align the wrap around of ssn so we compare relevant values.
3019dd32162dSLiad Kaufman 	 */
3020dd32162dSLiad Kaufman 	normalized_ssn = tid_data->ssn;
3021286ca8ebSLuca Coelho 	if (mvm->trans->trans_cfg->gen2)
3022dd32162dSLiad Kaufman 		normalized_ssn &= 0xff;
3023dd32162dSLiad Kaufman 
3024dd32162dSLiad Kaufman 	if (normalized_ssn == tid_data->next_reclaimed) {
3025e705c121SKalle Valo 		tid_data->state = IWL_AGG_STARTING;
30262ce113deSJohannes Berg 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
3027e705c121SKalle Valo 	} else {
3028e705c121SKalle Valo 		tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
30297e0ca723SMordechay Goodstein 		ret = IEEE80211_AMPDU_TX_START_DELAY_ADDBA;
30302ce113deSJohannes Berg 	}
3031e705c121SKalle Valo 
30329f9af3d7SLiad Kaufman out:
3033e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3034e705c121SKalle Valo 
3035e705c121SKalle Valo 	return ret;
3036e705c121SKalle Valo }
3037e705c121SKalle Valo 
3038e705c121SKalle Valo int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3039514c3069SLuca Coelho 			    struct ieee80211_sta *sta, u16 tid, u16 buf_size,
3040bb81bb68SEmmanuel Grumbach 			    bool amsdu)
3041e705c121SKalle Valo {
3042e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3043e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3044e705c121SKalle Valo 	unsigned int wdg_timeout =
3045e705c121SKalle Valo 		iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
3046eea76c36SEmmanuel Grumbach 	int queue, ret;
3047cf961e16SLiad Kaufman 	bool alloc_queue = true;
30489f9af3d7SLiad Kaufman 	enum iwl_mvm_queue_status queue_status;
3049e705c121SKalle Valo 	u16 ssn;
3050e705c121SKalle Valo 
3051eea76c36SEmmanuel Grumbach 	struct iwl_trans_txq_scd_cfg cfg = {
3052eea76c36SEmmanuel Grumbach 		.sta_id = mvmsta->sta_id,
3053eea76c36SEmmanuel Grumbach 		.tid = tid,
3054eea76c36SEmmanuel Grumbach 		.frame_limit = buf_size,
3055eea76c36SEmmanuel Grumbach 		.aggregate = true,
3056eea76c36SEmmanuel Grumbach 	};
3057eea76c36SEmmanuel Grumbach 
3058ecaf71deSGregory Greenman 	/*
3059ecaf71deSGregory Greenman 	 * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
3060ecaf71deSGregory Greenman 	 * manager, so this function should never be called in this case.
3061ecaf71deSGregory Greenman 	 */
30624243edb4SEmmanuel Grumbach 	if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
3063ecaf71deSGregory Greenman 		return -EINVAL;
3064ecaf71deSGregory Greenman 
3065e705c121SKalle Valo 	BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
3066e705c121SKalle Valo 		     != IWL_MAX_TID_COUNT);
3067e705c121SKalle Valo 
3068e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3069e705c121SKalle Valo 	ssn = tid_data->ssn;
3070e705c121SKalle Valo 	queue = tid_data->txq_id;
3071e705c121SKalle Valo 	tid_data->state = IWL_AGG_ON;
3072e705c121SKalle Valo 	mvmsta->agg_tids |= BIT(tid);
3073e705c121SKalle Valo 	tid_data->ssn = 0xffff;
3074bb81bb68SEmmanuel Grumbach 	tid_data->amsdu_in_ampdu_allowed = amsdu;
3075e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3076e705c121SKalle Valo 
307734e10860SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
307834e10860SSara Sharon 		/*
30790ec9257bSSara Sharon 		 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
30800ec9257bSSara Sharon 		 * would have failed, so if we are here there is no need to
30810ec9257bSSara Sharon 		 * allocate a queue.
30820ec9257bSSara Sharon 		 * However, if aggregation size is different than the default
30830ec9257bSSara Sharon 		 * size, the scheduler should be reconfigured.
30840ec9257bSSara Sharon 		 * We cannot do this with the new TX API, so return unsupported
30850ec9257bSSara Sharon 		 * for now, until it will be offloaded to firmware..
30860ec9257bSSara Sharon 		 * Note that if SCD default value changes - this condition
30870ec9257bSSara Sharon 		 * should be updated as well.
308834e10860SSara Sharon 		 */
30890ec9257bSSara Sharon 		if (buf_size < IWL_FRAME_LIMIT)
309034e10860SSara Sharon 			return -ENOTSUPP;
309134e10860SSara Sharon 
309234e10860SSara Sharon 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
309334e10860SSara Sharon 		if (ret)
309434e10860SSara Sharon 			return -EIO;
309534e10860SSara Sharon 		goto out;
309634e10860SSara Sharon 	}
309734e10860SSara Sharon 
3098eea76c36SEmmanuel Grumbach 	cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
3099e705c121SKalle Valo 
31009f9af3d7SLiad Kaufman 	queue_status = mvm->queue_info[queue].status;
31019f9af3d7SLiad Kaufman 
3102cf961e16SLiad Kaufman 	/* Maybe there is no need to even alloc a queue... */
3103cf961e16SLiad Kaufman 	if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
3104cf961e16SLiad Kaufman 		alloc_queue = false;
3105cf961e16SLiad Kaufman 
3106cf961e16SLiad Kaufman 	/*
3107cf961e16SLiad Kaufman 	 * Only reconfig the SCD for the queue if the window size has
3108cf961e16SLiad Kaufman 	 * changed from current (become smaller)
3109cf961e16SLiad Kaufman 	 */
31100ec9257bSSara Sharon 	if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
3111cf961e16SLiad Kaufman 		/*
3112cf961e16SLiad Kaufman 		 * If reconfiguring an existing queue, it first must be
3113cf961e16SLiad Kaufman 		 * drained
3114cf961e16SLiad Kaufman 		 */
3115a1a57877SSara Sharon 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
3116cf961e16SLiad Kaufman 						     BIT(queue));
3117cf961e16SLiad Kaufman 		if (ret) {
3118cf961e16SLiad Kaufman 			IWL_ERR(mvm,
3119cf961e16SLiad Kaufman 				"Error draining queue before reconfig\n");
3120cf961e16SLiad Kaufman 			return ret;
3121cf961e16SLiad Kaufman 		}
3122cf961e16SLiad Kaufman 
3123cf961e16SLiad Kaufman 		ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
3124cf961e16SLiad Kaufman 					   mvmsta->sta_id, tid,
3125cf961e16SLiad Kaufman 					   buf_size, ssn);
3126cf961e16SLiad Kaufman 		if (ret) {
3127cf961e16SLiad Kaufman 			IWL_ERR(mvm,
3128cf961e16SLiad Kaufman 				"Error reconfiguring TXQ #%d\n", queue);
3129cf961e16SLiad Kaufman 			return ret;
3130cf961e16SLiad Kaufman 		}
3131cf961e16SLiad Kaufman 	}
3132cf961e16SLiad Kaufman 
3133cf961e16SLiad Kaufman 	if (alloc_queue)
3134cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, sta, queue, ssn,
3135cf961e16SLiad Kaufman 				   &cfg, wdg_timeout);
3136e705c121SKalle Valo 
31379f9af3d7SLiad Kaufman 	/* Send ADD_STA command to enable aggs only if the queue isn't shared */
31389f9af3d7SLiad Kaufman 	if (queue_status != IWL_MVM_QUEUE_SHARED) {
3139e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
3140e705c121SKalle Valo 		if (ret)
3141e705c121SKalle Valo 			return -EIO;
31429f9af3d7SLiad Kaufman 	}
3143e705c121SKalle Valo 
3144e705c121SKalle Valo 	/* No need to mark as reserved */
3145cf961e16SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
3146e705c121SKalle Valo 
314734e10860SSara Sharon out:
3148e705c121SKalle Valo 	/*
3149e705c121SKalle Valo 	 * Even though in theory the peer could have different
3150e705c121SKalle Valo 	 * aggregation reorder buffer sizes for different sessions,
3151e705c121SKalle Valo 	 * our ucode doesn't allow for that and has a global limit
3152e705c121SKalle Valo 	 * for each station. Therefore, use the minimum of all the
3153e705c121SKalle Valo 	 * aggregation sessions and our default value.
3154e705c121SKalle Valo 	 */
3155e705c121SKalle Valo 	mvmsta->max_agg_bufsize =
3156e705c121SKalle Valo 		min(mvmsta->max_agg_bufsize, buf_size);
3157ecaf71deSGregory Greenman 	mvmsta->lq_sta.rs_drv.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
3158e705c121SKalle Valo 
3159e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
3160e705c121SKalle Valo 		     sta->addr, tid);
3161e705c121SKalle Valo 
3162cd4d6b0bSGregory Greenman 	return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.rs_drv.lq);
3163e705c121SKalle Valo }
3164e705c121SKalle Valo 
316534e10860SSara Sharon static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
316634e10860SSara Sharon 					struct iwl_mvm_sta *mvmsta,
31674b387906SAvraham Stern 					struct iwl_mvm_tid_data *tid_data)
316834e10860SSara Sharon {
31694b387906SAvraham Stern 	u16 txq_id = tid_data->txq_id;
31704b387906SAvraham Stern 
3171f3f240f9SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
3172f3f240f9SJohannes Berg 
317334e10860SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm))
317434e10860SSara Sharon 		return;
317534e10860SSara Sharon 
317634e10860SSara Sharon 	/*
317734e10860SSara Sharon 	 * The TXQ is marked as reserved only if no traffic came through yet
317834e10860SSara Sharon 	 * This means no traffic has been sent on this TID (agg'd or not), so
317934e10860SSara Sharon 	 * we no longer have use for the queue. Since it hasn't even been
318034e10860SSara Sharon 	 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
318134e10860SSara Sharon 	 * free.
318234e10860SSara Sharon 	 */
31834b387906SAvraham Stern 	if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
318434e10860SSara Sharon 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
31854b387906SAvraham Stern 		tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
31864b387906SAvraham Stern 	}
318734e10860SSara Sharon }
318834e10860SSara Sharon 
3189e705c121SKalle Valo int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3190e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid)
3191e705c121SKalle Valo {
3192e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3193e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3194e705c121SKalle Valo 	u16 txq_id;
3195e705c121SKalle Valo 	int err;
3196e705c121SKalle Valo 
3197e705c121SKalle Valo 	/*
3198e705c121SKalle Valo 	 * If mac80211 is cleaning its state, then say that we finished since
3199e705c121SKalle Valo 	 * our state has been cleared anyway.
3200e705c121SKalle Valo 	 */
3201e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3202e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3203e705c121SKalle Valo 		return 0;
3204e705c121SKalle Valo 	}
3205e705c121SKalle Valo 
3206e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3207e705c121SKalle Valo 
3208e705c121SKalle Valo 	txq_id = tid_data->txq_id;
3209e705c121SKalle Valo 
3210e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
3211e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
3212e705c121SKalle Valo 
3213e705c121SKalle Valo 	mvmsta->agg_tids &= ~BIT(tid);
3214e705c121SKalle Valo 
32154b387906SAvraham Stern 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3216e705c121SKalle Valo 
3217e705c121SKalle Valo 	switch (tid_data->state) {
3218e705c121SKalle Valo 	case IWL_AGG_ON:
3219e705c121SKalle Valo 		tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3220e705c121SKalle Valo 
3221e705c121SKalle Valo 		IWL_DEBUG_TX_QUEUES(mvm,
3222e705c121SKalle Valo 				    "ssn = %d, next_recl = %d\n",
3223e705c121SKalle Valo 				    tid_data->ssn, tid_data->next_reclaimed);
3224e705c121SKalle Valo 
3225e705c121SKalle Valo 		tid_data->ssn = 0xffff;
3226e705c121SKalle Valo 		tid_data->state = IWL_AGG_OFF;
3227e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
3228e705c121SKalle Valo 
3229e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3230e705c121SKalle Valo 
3231e705c121SKalle Valo 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3232e705c121SKalle Valo 		return 0;
3233e705c121SKalle Valo 	case IWL_AGG_STARTING:
3234e705c121SKalle Valo 	case IWL_EMPTYING_HW_QUEUE_ADDBA:
3235e705c121SKalle Valo 		/*
3236e705c121SKalle Valo 		 * The agg session has been stopped before it was set up. This
3237e705c121SKalle Valo 		 * can happen when the AddBA timer times out for example.
3238e705c121SKalle Valo 		 */
3239e705c121SKalle Valo 
3240e705c121SKalle Valo 		/* No barriers since we are under mutex */
3241e705c121SKalle Valo 		lockdep_assert_held(&mvm->mutex);
3242e705c121SKalle Valo 
3243e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3244e705c121SKalle Valo 		tid_data->state = IWL_AGG_OFF;
3245e705c121SKalle Valo 		err = 0;
3246e705c121SKalle Valo 		break;
3247e705c121SKalle Valo 	default:
3248e705c121SKalle Valo 		IWL_ERR(mvm,
3249e705c121SKalle Valo 			"Stopping AGG while state not ON or starting for %d on %d (%d)\n",
3250e705c121SKalle Valo 			mvmsta->sta_id, tid, tid_data->state);
3251e705c121SKalle Valo 		IWL_ERR(mvm,
3252e705c121SKalle Valo 			"\ttid_data->txq_id = %d\n", tid_data->txq_id);
3253e705c121SKalle Valo 		err = -EINVAL;
3254e705c121SKalle Valo 	}
3255e705c121SKalle Valo 
3256e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3257e705c121SKalle Valo 
3258e705c121SKalle Valo 	return err;
3259e705c121SKalle Valo }
3260e705c121SKalle Valo 
3261e705c121SKalle Valo int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3262e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid)
3263e705c121SKalle Valo {
3264e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3265e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3266e705c121SKalle Valo 	u16 txq_id;
3267e705c121SKalle Valo 	enum iwl_mvm_agg_state old_state;
3268e705c121SKalle Valo 
3269e705c121SKalle Valo 	/*
3270e705c121SKalle Valo 	 * First set the agg state to OFF to avoid calling
3271e705c121SKalle Valo 	 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
3272e705c121SKalle Valo 	 */
3273e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3274e705c121SKalle Valo 	txq_id = tid_data->txq_id;
3275e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
3276e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
3277e705c121SKalle Valo 	old_state = tid_data->state;
3278e705c121SKalle Valo 	tid_data->state = IWL_AGG_OFF;
3279e705c121SKalle Valo 	mvmsta->agg_tids &= ~BIT(tid);
3280e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3281e705c121SKalle Valo 
32824b387906SAvraham Stern 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3283e705c121SKalle Valo 
3284e705c121SKalle Valo 	if (old_state >= IWL_AGG_ON) {
3285e705c121SKalle Valo 		iwl_mvm_drain_sta(mvm, mvmsta, true);
3286d167e81aSMordechai Goodstein 
3287d167e81aSMordechai Goodstein 		if (iwl_mvm_has_new_tx_api(mvm)) {
3288d167e81aSMordechai Goodstein 			if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
3289d4e3a341SMordechay Goodstein 						   BIT(tid)))
3290d167e81aSMordechai Goodstein 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3291d167e81aSMordechai Goodstein 			iwl_trans_wait_txq_empty(mvm->trans, txq_id);
3292d167e81aSMordechai Goodstein 		} else {
3293d4e3a341SMordechay Goodstein 			if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id)))
3294e705c121SKalle Valo 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
32950b90964aSSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
3296d167e81aSMordechai Goodstein 		}
3297d6d517b7SSara Sharon 
3298e705c121SKalle Valo 		iwl_mvm_drain_sta(mvm, mvmsta, false);
3299e705c121SKalle Valo 
3300e705c121SKalle Valo 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3301e705c121SKalle Valo 	}
3302e705c121SKalle Valo 
3303e705c121SKalle Valo 	return 0;
3304e705c121SKalle Valo }
3305e705c121SKalle Valo 
3306e705c121SKalle Valo static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
3307e705c121SKalle Valo {
3308e705c121SKalle Valo 	int i, max = -1, max_offs = -1;
3309e705c121SKalle Valo 
3310e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3311e705c121SKalle Valo 
3312e705c121SKalle Valo 	/* Pick the unused key offset with the highest 'deleted'
3313e705c121SKalle Valo 	 * counter. Every time a key is deleted, all the counters
3314e705c121SKalle Valo 	 * are incremented and the one that was just deleted is
3315e705c121SKalle Valo 	 * reset to zero. Thus, the highest counter is the one
3316e705c121SKalle Valo 	 * that was deleted longest ago. Pick that one.
3317e705c121SKalle Valo 	 */
3318e705c121SKalle Valo 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3319e705c121SKalle Valo 		if (test_bit(i, mvm->fw_key_table))
3320e705c121SKalle Valo 			continue;
3321e705c121SKalle Valo 		if (mvm->fw_key_deleted[i] > max) {
3322e705c121SKalle Valo 			max = mvm->fw_key_deleted[i];
3323e705c121SKalle Valo 			max_offs = i;
3324e705c121SKalle Valo 		}
3325e705c121SKalle Valo 	}
3326e705c121SKalle Valo 
3327e705c121SKalle Valo 	if (max_offs < 0)
3328e705c121SKalle Valo 		return STA_KEY_IDX_INVALID;
3329e705c121SKalle Valo 
3330e705c121SKalle Valo 	return max_offs;
3331e705c121SKalle Valo }
3332e705c121SKalle Valo 
33335f7a1847SJohannes Berg static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
33344615fd15SEmmanuel Grumbach 					       struct ieee80211_vif *vif,
3335e705c121SKalle Valo 					       struct ieee80211_sta *sta)
3336e705c121SKalle Valo {
3337e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3338e705c121SKalle Valo 
33395f7a1847SJohannes Berg 	if (sta)
33405f7a1847SJohannes Berg 		return iwl_mvm_sta_from_mac80211(sta);
3341e705c121SKalle Valo 
3342e705c121SKalle Valo 	/*
3343e705c121SKalle Valo 	 * The device expects GTKs for station interfaces to be
3344e705c121SKalle Valo 	 * installed as GTKs for the AP station. If we have no
3345e705c121SKalle Valo 	 * station ID, then use AP's station ID.
3346e705c121SKalle Valo 	 */
3347e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
33480ae98812SSara Sharon 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
33494615fd15SEmmanuel Grumbach 		u8 sta_id = mvmvif->ap_sta_id;
33504615fd15SEmmanuel Grumbach 
33517d6a1ab6SEmmanuel Grumbach 		sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
33527d6a1ab6SEmmanuel Grumbach 					    lockdep_is_held(&mvm->mutex));
33537d6a1ab6SEmmanuel Grumbach 
33544615fd15SEmmanuel Grumbach 		/*
33554615fd15SEmmanuel Grumbach 		 * It is possible that the 'sta' parameter is NULL,
33564615fd15SEmmanuel Grumbach 		 * for example when a GTK is removed - the sta_id will then
33574615fd15SEmmanuel Grumbach 		 * be the AP ID, and no station was passed by mac80211.
33584615fd15SEmmanuel Grumbach 		 */
33597d6a1ab6SEmmanuel Grumbach 		if (IS_ERR_OR_NULL(sta))
33607d6a1ab6SEmmanuel Grumbach 			return NULL;
33617d6a1ab6SEmmanuel Grumbach 
33627d6a1ab6SEmmanuel Grumbach 		return iwl_mvm_sta_from_mac80211(sta);
33634615fd15SEmmanuel Grumbach 	}
3364e705c121SKalle Valo 
33655f7a1847SJohannes Berg 	return NULL;
3366e705c121SKalle Valo }
3367e705c121SKalle Valo 
3368a6dfbd04SJohannes Berg static int iwl_mvm_pn_cmp(const u8 *pn1, const u8 *pn2, int len)
3369a6dfbd04SJohannes Berg {
3370a6dfbd04SJohannes Berg 	int i;
3371a6dfbd04SJohannes Berg 
3372a6dfbd04SJohannes Berg 	for (i = len - 1; i >= 0; i--) {
3373a6dfbd04SJohannes Berg 		if (pn1[i] > pn2[i])
3374a6dfbd04SJohannes Berg 			return 1;
3375a6dfbd04SJohannes Berg 		if (pn1[i] < pn2[i])
3376a6dfbd04SJohannes Berg 			return -1;
3377a6dfbd04SJohannes Berg 	}
3378a6dfbd04SJohannes Berg 
3379a6dfbd04SJohannes Berg 	return 0;
3380a6dfbd04SJohannes Berg }
3381a6dfbd04SJohannes Berg 
3382e705c121SKalle Valo static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
338385aeb58cSDavid Spinadel 				u32 sta_id,
338445c458b4SSara Sharon 				struct ieee80211_key_conf *key, bool mcast,
33854615fd15SEmmanuel Grumbach 				u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
33864883145aSEmmanuel Grumbach 				u8 key_offset, bool mfp)
3387e705c121SKalle Valo {
338845c458b4SSara Sharon 	union {
338945c458b4SSara Sharon 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
339045c458b4SSara Sharon 		struct iwl_mvm_add_sta_key_cmd cmd;
339145c458b4SSara Sharon 	} u = {};
3392e705c121SKalle Valo 	__le16 key_flags;
3393e705c121SKalle Valo 	int ret;
3394e705c121SKalle Valo 	u32 status;
3395e705c121SKalle Valo 	u16 keyidx;
339645c458b4SSara Sharon 	u64 pn = 0;
339745c458b4SSara Sharon 	int i, size;
339845c458b4SSara Sharon 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
339945c458b4SSara Sharon 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3400971cbe50SJohannes Berg 	int api_ver = iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA_KEY,
3401199d895fSJohannes Berg 					    new_api ? 2 : 1);
3402e705c121SKalle Valo 
340385aeb58cSDavid Spinadel 	if (sta_id == IWL_MVM_INVALID_STA)
340485aeb58cSDavid Spinadel 		return -EINVAL;
340585aeb58cSDavid Spinadel 
340645c458b4SSara Sharon 	keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
3407e705c121SKalle Valo 		 STA_KEY_FLG_KEYID_MSK;
3408e705c121SKalle Valo 	key_flags = cpu_to_le16(keyidx);
3409e705c121SKalle Valo 	key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3410e705c121SKalle Valo 
341145c458b4SSara Sharon 	switch (key->cipher) {
3412e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
3413e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
3414199d895fSJohannes Berg 		if (api_ver >= 2) {
341545c458b4SSara Sharon 			memcpy((void *)&u.cmd.tx_mic_key,
341645c458b4SSara Sharon 			       &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
341745c458b4SSara Sharon 			       IWL_MIC_KEY_SIZE);
341845c458b4SSara Sharon 
341945c458b4SSara Sharon 			memcpy((void *)&u.cmd.rx_mic_key,
342045c458b4SSara Sharon 			       &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
342145c458b4SSara Sharon 			       IWL_MIC_KEY_SIZE);
342245c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
342345c458b4SSara Sharon 
342445c458b4SSara Sharon 		} else {
342545c458b4SSara Sharon 			u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3426e705c121SKalle Valo 			for (i = 0; i < 5; i++)
342745c458b4SSara Sharon 				u.cmd_v1.tkip_rx_ttak[i] =
342845c458b4SSara Sharon 					cpu_to_le16(tkip_p1k[i]);
342945c458b4SSara Sharon 		}
343045c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3431e705c121SKalle Valo 		break;
3432e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
3433e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
343445c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3435199d895fSJohannes Berg 		if (api_ver >= 2)
343645c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
3437e705c121SKalle Valo 		break;
3438e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
3439e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
34405a2abdcaSGustavo A. R. Silva 		fallthrough;
3441e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
3442e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
344345c458b4SSara Sharon 		memcpy(u.cmd.common.key + 3, key->key, key->keylen);
3444e705c121SKalle Valo 		break;
34452a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
34462a53d166SAyala Beker 		key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
34475a2abdcaSGustavo A. R. Silva 		fallthrough;
34482a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
34492a53d166SAyala Beker 		key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
345045c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3451199d895fSJohannes Berg 		if (api_ver >= 2)
345245c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
34532a53d166SAyala Beker 		break;
3454e705c121SKalle Valo 	default:
3455e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
345645c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3457e705c121SKalle Valo 	}
3458e705c121SKalle Valo 
3459e705c121SKalle Valo 	if (mcast)
3460e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
34614883145aSEmmanuel Grumbach 	if (mfp)
34624883145aSEmmanuel Grumbach 		key_flags |= cpu_to_le16(STA_KEY_MFP);
3463e705c121SKalle Valo 
346445c458b4SSara Sharon 	u.cmd.common.key_offset = key_offset;
346545c458b4SSara Sharon 	u.cmd.common.key_flags = key_flags;
346685aeb58cSDavid Spinadel 	u.cmd.common.sta_id = sta_id;
346745c458b4SSara Sharon 
3468a6dfbd04SJohannes Berg 	if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
3469a6dfbd04SJohannes Berg 		i = 0;
3470a6dfbd04SJohannes Berg 	else
3471a6dfbd04SJohannes Berg 		i = -1;
3472a6dfbd04SJohannes Berg 
3473a6dfbd04SJohannes Berg 	for (; i < IEEE80211_NUM_TIDS; i++) {
3474a6dfbd04SJohannes Berg 		struct ieee80211_key_seq seq = {};
3475a6dfbd04SJohannes Berg 		u8 _rx_pn[IEEE80211_MAX_PN_LEN] = {}, *rx_pn = _rx_pn;
3476a6dfbd04SJohannes Berg 		int rx_pn_len = 8;
3477199d895fSJohannes Berg 		/* there's a hole at 2/3 in FW format depending on version */
3478199d895fSJohannes Berg 		int hole = api_ver >= 3 ? 0 : 2;
3479a6dfbd04SJohannes Berg 
3480a6dfbd04SJohannes Berg 		ieee80211_get_key_rx_seq(key, i, &seq);
3481a6dfbd04SJohannes Berg 
3482a6dfbd04SJohannes Berg 		if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
3483a6dfbd04SJohannes Berg 			rx_pn[0] = seq.tkip.iv16;
3484a6dfbd04SJohannes Berg 			rx_pn[1] = seq.tkip.iv16 >> 8;
3485199d895fSJohannes Berg 			rx_pn[2 + hole] = seq.tkip.iv32;
3486199d895fSJohannes Berg 			rx_pn[3 + hole] = seq.tkip.iv32 >> 8;
3487199d895fSJohannes Berg 			rx_pn[4 + hole] = seq.tkip.iv32 >> 16;
3488199d895fSJohannes Berg 			rx_pn[5 + hole] = seq.tkip.iv32 >> 24;
3489a6dfbd04SJohannes Berg 		} else if (key_flags & cpu_to_le16(STA_KEY_FLG_EXT)) {
3490a6dfbd04SJohannes Berg 			rx_pn = seq.hw.seq;
3491a6dfbd04SJohannes Berg 			rx_pn_len = seq.hw.seq_len;
3492a6dfbd04SJohannes Berg 		} else {
3493a6dfbd04SJohannes Berg 			rx_pn[0] = seq.ccmp.pn[0];
3494a6dfbd04SJohannes Berg 			rx_pn[1] = seq.ccmp.pn[1];
3495199d895fSJohannes Berg 			rx_pn[2 + hole] = seq.ccmp.pn[2];
3496199d895fSJohannes Berg 			rx_pn[3 + hole] = seq.ccmp.pn[3];
3497199d895fSJohannes Berg 			rx_pn[4 + hole] = seq.ccmp.pn[4];
3498199d895fSJohannes Berg 			rx_pn[5 + hole] = seq.ccmp.pn[5];
3499a6dfbd04SJohannes Berg 		}
3500a6dfbd04SJohannes Berg 
3501a6dfbd04SJohannes Berg 		if (iwl_mvm_pn_cmp(rx_pn, (u8 *)&u.cmd.common.rx_secur_seq_cnt,
3502a6dfbd04SJohannes Berg 				   rx_pn_len) > 0)
3503a6dfbd04SJohannes Berg 			memcpy(&u.cmd.common.rx_secur_seq_cnt, rx_pn,
3504a6dfbd04SJohannes Berg 			       rx_pn_len);
3505a6dfbd04SJohannes Berg 	}
3506a6dfbd04SJohannes Berg 
3507199d895fSJohannes Berg 	if (api_ver >= 2) {
350845c458b4SSara Sharon 		u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
350945c458b4SSara Sharon 		size = sizeof(u.cmd);
351045c458b4SSara Sharon 	} else {
351145c458b4SSara Sharon 		size = sizeof(u.cmd_v1);
351245c458b4SSara Sharon 	}
3513e705c121SKalle Valo 
3514e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
3515e705c121SKalle Valo 	if (cmd_flags & CMD_ASYNC)
351645c458b4SSara Sharon 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
351745c458b4SSara Sharon 					   &u.cmd);
3518e705c121SKalle Valo 	else
351945c458b4SSara Sharon 		ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
352045c458b4SSara Sharon 						  &u.cmd, &status);
3521e705c121SKalle Valo 
3522e705c121SKalle Valo 	switch (status) {
3523e705c121SKalle Valo 	case ADD_STA_SUCCESS:
3524e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3525e705c121SKalle Valo 		break;
3526e705c121SKalle Valo 	default:
3527e705c121SKalle Valo 		ret = -EIO;
3528e705c121SKalle Valo 		IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3529e705c121SKalle Valo 		break;
3530e705c121SKalle Valo 	}
3531e705c121SKalle Valo 
3532e705c121SKalle Valo 	return ret;
3533e705c121SKalle Valo }
3534e705c121SKalle Valo 
3535e705c121SKalle Valo static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3536e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
3537e705c121SKalle Valo 				 u8 sta_id, bool remove_key)
3538e705c121SKalle Valo {
3539e705c121SKalle Valo 	struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3540e705c121SKalle Valo 
3541e705c121SKalle Valo 	/* verify the key details match the required command's expectations */
35428e160ab8SAyala Beker 	if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3543b1fdc250SJohannes Berg 		    (keyconf->keyidx != 4 && keyconf->keyidx != 5 &&
3544b1fdc250SJohannes Berg 		     keyconf->keyidx != 6 && keyconf->keyidx != 7) ||
35458e160ab8SAyala Beker 		    (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
35468e160ab8SAyala Beker 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
35478e160ab8SAyala Beker 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
35488e160ab8SAyala Beker 		return -EINVAL;
35498e160ab8SAyala Beker 
35508e160ab8SAyala Beker 	if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
35518e160ab8SAyala Beker 		    keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3552e705c121SKalle Valo 		return -EINVAL;
3553e705c121SKalle Valo 
3554e705c121SKalle Valo 	igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3555e705c121SKalle Valo 	igtk_cmd.sta_id = cpu_to_le32(sta_id);
3556e705c121SKalle Valo 
3557e705c121SKalle Valo 	if (remove_key) {
3558197288d5SLuca Coelho 		/* This is a valid situation for IGTK */
3559197288d5SLuca Coelho 		if (sta_id == IWL_MVM_INVALID_STA)
3560197288d5SLuca Coelho 			return 0;
3561197288d5SLuca Coelho 
3562e705c121SKalle Valo 		igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3563e705c121SKalle Valo 	} else {
3564e705c121SKalle Valo 		struct ieee80211_key_seq seq;
3565e705c121SKalle Valo 		const u8 *pn;
3566e705c121SKalle Valo 
3567aa950524SAyala Beker 		switch (keyconf->cipher) {
3568aa950524SAyala Beker 		case WLAN_CIPHER_SUITE_AES_CMAC:
3569aa950524SAyala Beker 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3570aa950524SAyala Beker 			break;
35718e160ab8SAyala Beker 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
35728e160ab8SAyala Beker 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
35738e160ab8SAyala Beker 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
35748e160ab8SAyala Beker 			break;
3575aa950524SAyala Beker 		default:
3576aa950524SAyala Beker 			return -EINVAL;
3577aa950524SAyala Beker 		}
3578aa950524SAyala Beker 
35798e160ab8SAyala Beker 		memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
35808e160ab8SAyala Beker 		if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
35818e160ab8SAyala Beker 			igtk_cmd.ctrl_flags |=
35828e160ab8SAyala Beker 				cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3583e705c121SKalle Valo 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3584e705c121SKalle Valo 		pn = seq.aes_cmac.pn;
3585e705c121SKalle Valo 		igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3586e705c121SKalle Valo 						       ((u64) pn[4] << 8) |
3587e705c121SKalle Valo 						       ((u64) pn[3] << 16) |
3588e705c121SKalle Valo 						       ((u64) pn[2] << 24) |
3589e705c121SKalle Valo 						       ((u64) pn[1] << 32) |
3590e705c121SKalle Valo 						       ((u64) pn[0] << 40));
3591e705c121SKalle Valo 	}
3592e705c121SKalle Valo 
3593b1fdc250SJohannes Berg 	IWL_DEBUG_INFO(mvm, "%s %sIGTK (%d) for sta %u\n",
3594e705c121SKalle Valo 		       remove_key ? "removing" : "installing",
3595b1fdc250SJohannes Berg 		       keyconf->keyidx >= 6 ? "B" : "",
3596b1fdc250SJohannes Berg 		       keyconf->keyidx, igtk_cmd.sta_id);
3597e705c121SKalle Valo 
35988e160ab8SAyala Beker 	if (!iwl_mvm_has_new_rx_api(mvm)) {
35998e160ab8SAyala Beker 		struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
36008e160ab8SAyala Beker 			.ctrl_flags = igtk_cmd.ctrl_flags,
36018e160ab8SAyala Beker 			.key_id = igtk_cmd.key_id,
36028e160ab8SAyala Beker 			.sta_id = igtk_cmd.sta_id,
36038e160ab8SAyala Beker 			.receive_seq_cnt = igtk_cmd.receive_seq_cnt
36048e160ab8SAyala Beker 		};
36058e160ab8SAyala Beker 
36068e160ab8SAyala Beker 		memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
36078e160ab8SAyala Beker 		       ARRAY_SIZE(igtk_cmd_v1.igtk));
36088e160ab8SAyala Beker 		return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
36098e160ab8SAyala Beker 					    sizeof(igtk_cmd_v1), &igtk_cmd_v1);
36108e160ab8SAyala Beker 	}
3611e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3612e705c121SKalle Valo 				    sizeof(igtk_cmd), &igtk_cmd);
3613e705c121SKalle Valo }
3614e705c121SKalle Valo 
3615e705c121SKalle Valo 
3616e705c121SKalle Valo static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3617e705c121SKalle Valo 				       struct ieee80211_vif *vif,
3618e705c121SKalle Valo 				       struct ieee80211_sta *sta)
3619e705c121SKalle Valo {
3620e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3621e705c121SKalle Valo 
3622e705c121SKalle Valo 	if (sta)
3623e705c121SKalle Valo 		return sta->addr;
3624e705c121SKalle Valo 
3625e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
36260ae98812SSara Sharon 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3627e705c121SKalle Valo 		u8 sta_id = mvmvif->ap_sta_id;
3628e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3629e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
3630e705c121SKalle Valo 		return sta->addr;
3631e705c121SKalle Valo 	}
3632e705c121SKalle Valo 
3633e705c121SKalle Valo 
3634e705c121SKalle Valo 	return NULL;
3635e705c121SKalle Valo }
3636e705c121SKalle Valo 
3637e705c121SKalle Valo static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3638e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3639e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3640e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
36414615fd15SEmmanuel Grumbach 				 u8 key_offset,
3642e705c121SKalle Valo 				 bool mcast)
3643e705c121SKalle Valo {
3644e705c121SKalle Valo 	const u8 *addr;
3645e705c121SKalle Valo 	struct ieee80211_key_seq seq;
3646e705c121SKalle Valo 	u16 p1k[5];
364785aeb58cSDavid Spinadel 	u32 sta_id;
36484883145aSEmmanuel Grumbach 	bool mfp = false;
364985aeb58cSDavid Spinadel 
365085aeb58cSDavid Spinadel 	if (sta) {
365185aeb58cSDavid Spinadel 		struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
365285aeb58cSDavid Spinadel 
365385aeb58cSDavid Spinadel 		sta_id = mvm_sta->sta_id;
36544883145aSEmmanuel Grumbach 		mfp = sta->mfp;
365585aeb58cSDavid Spinadel 	} else if (vif->type == NL80211_IFTYPE_AP &&
365685aeb58cSDavid Spinadel 		   !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
365785aeb58cSDavid Spinadel 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
365885aeb58cSDavid Spinadel 
365985aeb58cSDavid Spinadel 		sta_id = mvmvif->mcast_sta.sta_id;
366085aeb58cSDavid Spinadel 	} else {
366185aeb58cSDavid Spinadel 		IWL_ERR(mvm, "Failed to find station id\n");
366285aeb58cSDavid Spinadel 		return -EINVAL;
366385aeb58cSDavid Spinadel 	}
3664e705c121SKalle Valo 
366595a62c33SJohannes Berg 	if (keyconf->cipher == WLAN_CIPHER_SUITE_TKIP) {
3666e705c121SKalle Valo 		addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3667e705c121SKalle Valo 		/* get phase 1 key from mac80211 */
3668e705c121SKalle Valo 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3669e705c121SKalle Valo 		ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
367095a62c33SJohannes Berg 
367195a62c33SJohannes Berg 		return iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
36724883145aSEmmanuel Grumbach 					    seq.tkip.iv32, p1k, 0, key_offset,
36734883145aSEmmanuel Grumbach 					    mfp);
3674e705c121SKalle Valo 	}
3675e705c121SKalle Valo 
367695a62c33SJohannes Berg 	return iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
367795a62c33SJohannes Berg 				    0, NULL, 0, key_offset, mfp);
3678e705c121SKalle Valo }
3679e705c121SKalle Valo 
3680e705c121SKalle Valo int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3681e705c121SKalle Valo 			struct ieee80211_vif *vif,
3682e705c121SKalle Valo 			struct ieee80211_sta *sta,
3683e705c121SKalle Valo 			struct ieee80211_key_conf *keyconf,
36844615fd15SEmmanuel Grumbach 			u8 key_offset)
3685e705c121SKalle Valo {
3686e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
36875f7a1847SJohannes Berg 	struct iwl_mvm_sta *mvm_sta;
368885aeb58cSDavid Spinadel 	u8 sta_id = IWL_MVM_INVALID_STA;
3689e705c121SKalle Valo 	int ret;
3690e705c121SKalle Valo 	static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3691e705c121SKalle Valo 
3692e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3693e705c121SKalle Valo 
369485aeb58cSDavid Spinadel 	if (vif->type != NL80211_IFTYPE_AP ||
369585aeb58cSDavid Spinadel 	    keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3696e705c121SKalle Valo 		/* Get the station id from the mvm local station table */
36975f7a1847SJohannes Berg 		mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
36985f7a1847SJohannes Berg 		if (!mvm_sta) {
36995f7a1847SJohannes Berg 			IWL_ERR(mvm, "Failed to find station\n");
3700e705c121SKalle Valo 			return -EINVAL;
3701e705c121SKalle Valo 		}
37025f7a1847SJohannes Berg 		sta_id = mvm_sta->sta_id;
3703e705c121SKalle Valo 
3704e705c121SKalle Valo 		/*
3705e705c121SKalle Valo 		 * It is possible that the 'sta' parameter is NULL, and thus
370685aeb58cSDavid Spinadel 		 * there is a need to retrieve the sta from the local station
370785aeb58cSDavid Spinadel 		 * table.
3708e705c121SKalle Valo 		 */
3709e705c121SKalle Valo 		if (!sta) {
371085aeb58cSDavid Spinadel 			sta = rcu_dereference_protected(
371185aeb58cSDavid Spinadel 				mvm->fw_id_to_mac_id[sta_id],
3712e705c121SKalle Valo 				lockdep_is_held(&mvm->mutex));
3713e705c121SKalle Valo 			if (IS_ERR_OR_NULL(sta)) {
3714e705c121SKalle Valo 				IWL_ERR(mvm, "Invalid station id\n");
3715e705c121SKalle Valo 				return -EINVAL;
3716e705c121SKalle Valo 			}
3717e705c121SKalle Valo 		}
3718e705c121SKalle Valo 
3719e705c121SKalle Valo 		if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3720e705c121SKalle Valo 			return -EINVAL;
3721e829b17cSBeni Lev 	} else {
3722e829b17cSBeni Lev 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3723e829b17cSBeni Lev 
3724e829b17cSBeni Lev 		sta_id = mvmvif->mcast_sta.sta_id;
3725e829b17cSBeni Lev 	}
3726e829b17cSBeni Lev 
3727e829b17cSBeni Lev 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3728e829b17cSBeni Lev 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3729e829b17cSBeni Lev 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3730e829b17cSBeni Lev 		ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3731e829b17cSBeni Lev 		goto end;
373285aeb58cSDavid Spinadel 	}
3733e705c121SKalle Valo 
37344615fd15SEmmanuel Grumbach 	/* If the key_offset is not pre-assigned, we need to find a
37354615fd15SEmmanuel Grumbach 	 * new offset to use.  In normal cases, the offset is not
37364615fd15SEmmanuel Grumbach 	 * pre-assigned, but during HW_RESTART we want to reuse the
37374615fd15SEmmanuel Grumbach 	 * same indices, so we pass them when this function is called.
37384615fd15SEmmanuel Grumbach 	 *
37394615fd15SEmmanuel Grumbach 	 * In D3 entry, we need to hardcoded the indices (because the
37404615fd15SEmmanuel Grumbach 	 * firmware hardcodes the PTK offset to 0).  In this case, we
37414615fd15SEmmanuel Grumbach 	 * need to make sure we don't overwrite the hw_key_idx in the
37424615fd15SEmmanuel Grumbach 	 * keyconf structure, because otherwise we cannot configure
37434615fd15SEmmanuel Grumbach 	 * the original ones back when resuming.
3744e705c121SKalle Valo 	 */
37454615fd15SEmmanuel Grumbach 	if (key_offset == STA_KEY_IDX_INVALID) {
37464615fd15SEmmanuel Grumbach 		key_offset  = iwl_mvm_set_fw_key_idx(mvm);
37474615fd15SEmmanuel Grumbach 		if (key_offset == STA_KEY_IDX_INVALID)
3748e705c121SKalle Valo 			return -ENOSPC;
37494615fd15SEmmanuel Grumbach 		keyconf->hw_key_idx = key_offset;
3750e705c121SKalle Valo 	}
3751e705c121SKalle Valo 
37524615fd15SEmmanuel Grumbach 	ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
37539c3deeb5SLuca Coelho 	if (ret)
3754e705c121SKalle Valo 		goto end;
3755e705c121SKalle Valo 
3756e705c121SKalle Valo 	/*
3757e705c121SKalle Valo 	 * For WEP, the same key is used for multicast and unicast. Upload it
3758e705c121SKalle Valo 	 * again, using the same key offset, and now pointing the other one
3759e705c121SKalle Valo 	 * to the same key slot (offset).
3760e705c121SKalle Valo 	 * If this fails, remove the original as well.
3761e705c121SKalle Valo 	 */
376285aeb58cSDavid Spinadel 	if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
376385aeb58cSDavid Spinadel 	     keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
376485aeb58cSDavid Spinadel 	    sta) {
37654615fd15SEmmanuel Grumbach 		ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
37664615fd15SEmmanuel Grumbach 					    key_offset, !mcast);
3767e705c121SKalle Valo 		if (ret) {
3768e705c121SKalle Valo 			__iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
37699c3deeb5SLuca Coelho 			goto end;
3770e705c121SKalle Valo 		}
3771e705c121SKalle Valo 	}
3772e705c121SKalle Valo 
37739c3deeb5SLuca Coelho 	__set_bit(key_offset, mvm->fw_key_table);
37749c3deeb5SLuca Coelho 
3775e705c121SKalle Valo end:
3776e705c121SKalle Valo 	IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3777e705c121SKalle Valo 		      keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3778e705c121SKalle Valo 		      sta ? sta->addr : zero_addr, ret);
3779e705c121SKalle Valo 	return ret;
3780e705c121SKalle Valo }
3781e705c121SKalle Valo 
3782e705c121SKalle Valo int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3783e705c121SKalle Valo 			   struct ieee80211_vif *vif,
3784e705c121SKalle Valo 			   struct ieee80211_sta *sta,
3785e705c121SKalle Valo 			   struct ieee80211_key_conf *keyconf)
3786e705c121SKalle Valo {
3787e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
37885f7a1847SJohannes Berg 	struct iwl_mvm_sta *mvm_sta;
37890ae98812SSara Sharon 	u8 sta_id = IWL_MVM_INVALID_STA;
3790e705c121SKalle Valo 	int ret, i;
3791e705c121SKalle Valo 
3792e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3793e705c121SKalle Valo 
37945f7a1847SJohannes Berg 	/* Get the station from the mvm local station table */
37955f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
379671793b7dSLuca Coelho 	if (mvm_sta)
3797cd4d23c1SIlan Peer 		sta_id = mvm_sta->sta_id;
379885aeb58cSDavid Spinadel 	else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
379985aeb58cSDavid Spinadel 		sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
380085aeb58cSDavid Spinadel 
3801e705c121SKalle Valo 
3802e705c121SKalle Valo 	IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3803e705c121SKalle Valo 		      keyconf->keyidx, sta_id);
3804e705c121SKalle Valo 
3805197288d5SLuca Coelho 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
38068e160ab8SAyala Beker 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3807197288d5SLuca Coelho 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3808e705c121SKalle Valo 		return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3809e705c121SKalle Valo 
3810e705c121SKalle Valo 	if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3811e705c121SKalle Valo 		IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3812e705c121SKalle Valo 			keyconf->hw_key_idx);
3813e705c121SKalle Valo 		return -ENOENT;
3814e705c121SKalle Valo 	}
3815e705c121SKalle Valo 
3816e705c121SKalle Valo 	/* track which key was deleted last */
3817e705c121SKalle Valo 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3818e705c121SKalle Valo 		if (mvm->fw_key_deleted[i] < U8_MAX)
3819e705c121SKalle Valo 			mvm->fw_key_deleted[i]++;
3820e705c121SKalle Valo 	}
3821e705c121SKalle Valo 	mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3822e705c121SKalle Valo 
382385aeb58cSDavid Spinadel 	if (sta && !mvm_sta) {
3824e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3825e705c121SKalle Valo 		return 0;
3826e705c121SKalle Valo 	}
3827e705c121SKalle Valo 
3828e705c121SKalle Valo 	ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3829e705c121SKalle Valo 	if (ret)
3830e705c121SKalle Valo 		return ret;
3831e705c121SKalle Valo 
3832e705c121SKalle Valo 	/* delete WEP key twice to get rid of (now useless) offset */
3833e705c121SKalle Valo 	if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3834e705c121SKalle Valo 	    keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3835e705c121SKalle Valo 		ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3836e705c121SKalle Valo 
3837e705c121SKalle Valo 	return ret;
3838e705c121SKalle Valo }
3839e705c121SKalle Valo 
3840e705c121SKalle Valo void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3841e705c121SKalle Valo 			     struct ieee80211_vif *vif,
3842e705c121SKalle Valo 			     struct ieee80211_key_conf *keyconf,
3843e705c121SKalle Valo 			     struct ieee80211_sta *sta, u32 iv32,
3844e705c121SKalle Valo 			     u16 *phase1key)
3845e705c121SKalle Valo {
3846e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta;
3847e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
38484883145aSEmmanuel Grumbach 	bool mfp = sta ? sta->mfp : false;
3849e705c121SKalle Valo 
3850e705c121SKalle Valo 	rcu_read_lock();
3851e705c121SKalle Valo 
38525f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
38535f7a1847SJohannes Berg 	if (WARN_ON_ONCE(!mvm_sta))
385412f17211SEmmanuel Grumbach 		goto unlock;
385585aeb58cSDavid Spinadel 	iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
38564883145aSEmmanuel Grumbach 			     iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx,
38574883145aSEmmanuel Grumbach 			     mfp);
385812f17211SEmmanuel Grumbach 
385912f17211SEmmanuel Grumbach  unlock:
3860e705c121SKalle Valo 	rcu_read_unlock();
3861e705c121SKalle Valo }
3862e705c121SKalle Valo 
3863e705c121SKalle Valo void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3864e705c121SKalle Valo 				struct ieee80211_sta *sta)
3865e705c121SKalle Valo {
3866e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3867e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3868e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3869e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3870e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_PS),
3871e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3872e705c121SKalle Valo 	};
3873e705c121SKalle Valo 	int ret;
3874e705c121SKalle Valo 
3875854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3876854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3877e705c121SKalle Valo 	if (ret)
3878e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3879e705c121SKalle Valo }
3880e705c121SKalle Valo 
3881e705c121SKalle Valo void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3882e705c121SKalle Valo 				       struct ieee80211_sta *sta,
3883e705c121SKalle Valo 				       enum ieee80211_frame_release_type reason,
3884e705c121SKalle Valo 				       u16 cnt, u16 tids, bool more_data,
38859a3fcf91SSara Sharon 				       bool single_sta_queue)
3886e705c121SKalle Valo {
3887e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3888e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3889e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3890e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3891e705c121SKalle Valo 		.modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3892e705c121SKalle Valo 		.sleep_tx_count = cpu_to_le16(cnt),
3893e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3894e705c121SKalle Valo 	};
3895e705c121SKalle Valo 	int tid, ret;
3896e705c121SKalle Valo 	unsigned long _tids = tids;
3897e705c121SKalle Valo 
3898e705c121SKalle Valo 	/* convert TIDs to ACs - we don't support TSPEC so that's OK
3899e705c121SKalle Valo 	 * Note that this field is reserved and unused by firmware not
3900e705c121SKalle Valo 	 * supporting GO uAPSD, so it's safe to always do this.
3901e705c121SKalle Valo 	 */
3902e705c121SKalle Valo 	for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3903e705c121SKalle Valo 		cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3904e705c121SKalle Valo 
39059a3fcf91SSara Sharon 	/* If we're releasing frames from aggregation or dqa queues then check
39069a3fcf91SSara Sharon 	 * if all the queues that we're releasing frames from, combined, have:
3907e705c121SKalle Valo 	 *  - more frames than the service period, in which case more_data
3908e705c121SKalle Valo 	 *    needs to be set
3909e705c121SKalle Valo 	 *  - fewer than 'cnt' frames, in which case we need to adjust the
3910e705c121SKalle Valo 	 *    firmware command (but do that unconditionally)
3911e705c121SKalle Valo 	 */
39129a3fcf91SSara Sharon 	if (single_sta_queue) {
3913e705c121SKalle Valo 		int remaining = cnt;
391436be0eb6SEmmanuel Grumbach 		int sleep_tx_count;
3915e705c121SKalle Valo 
3916e705c121SKalle Valo 		spin_lock_bh(&mvmsta->lock);
3917e705c121SKalle Valo 		for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3918e705c121SKalle Valo 			struct iwl_mvm_tid_data *tid_data;
3919e705c121SKalle Valo 			u16 n_queued;
3920e705c121SKalle Valo 
3921e705c121SKalle Valo 			tid_data = &mvmsta->tid_data[tid];
3922e705c121SKalle Valo 
3923dd32162dSLiad Kaufman 			n_queued = iwl_mvm_tid_queued(mvm, tid_data);
3924e705c121SKalle Valo 			if (n_queued > remaining) {
3925e705c121SKalle Valo 				more_data = true;
3926e705c121SKalle Valo 				remaining = 0;
3927e705c121SKalle Valo 				break;
3928e705c121SKalle Valo 			}
3929e705c121SKalle Valo 			remaining -= n_queued;
3930e705c121SKalle Valo 		}
393136be0eb6SEmmanuel Grumbach 		sleep_tx_count = cnt - remaining;
393236be0eb6SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
393336be0eb6SEmmanuel Grumbach 			mvmsta->sleep_tx_count = sleep_tx_count;
3934e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
3935e705c121SKalle Valo 
393636be0eb6SEmmanuel Grumbach 		cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3937e705c121SKalle Valo 		if (WARN_ON(cnt - remaining == 0)) {
3938e705c121SKalle Valo 			ieee80211_sta_eosp(sta);
3939e705c121SKalle Valo 			return;
3940e705c121SKalle Valo 		}
3941e705c121SKalle Valo 	}
3942e705c121SKalle Valo 
3943e705c121SKalle Valo 	/* Note: this is ignored by firmware not supporting GO uAPSD */
3944e705c121SKalle Valo 	if (more_data)
3945ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
3946e705c121SKalle Valo 
3947e705c121SKalle Valo 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3948e705c121SKalle Valo 		mvmsta->next_status_eosp = true;
3949ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
3950e705c121SKalle Valo 	} else {
3951ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
3952e705c121SKalle Valo 	}
3953e705c121SKalle Valo 
3954156f92f2SEmmanuel Grumbach 	/* block the Tx queues until the FW updated the sleep Tx count */
3955156f92f2SEmmanuel Grumbach 	iwl_trans_block_txq_ptrs(mvm->trans, true);
3956156f92f2SEmmanuel Grumbach 
3957156f92f2SEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3958156f92f2SEmmanuel Grumbach 				   CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3959854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3960e705c121SKalle Valo 	if (ret)
3961e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3962e705c121SKalle Valo }
3963e705c121SKalle Valo 
3964e705c121SKalle Valo void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3965e705c121SKalle Valo 			   struct iwl_rx_cmd_buffer *rxb)
3966e705c121SKalle Valo {
3967e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3968e705c121SKalle Valo 	struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3969e705c121SKalle Valo 	struct ieee80211_sta *sta;
3970e705c121SKalle Valo 	u32 sta_id = le32_to_cpu(notif->sta_id);
3971e705c121SKalle Valo 
3972be9ae34eSNathan Errera 	if (WARN_ON_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations))
3973e705c121SKalle Valo 		return;
3974e705c121SKalle Valo 
3975e705c121SKalle Valo 	rcu_read_lock();
3976e705c121SKalle Valo 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3977e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(sta))
3978e705c121SKalle Valo 		ieee80211_sta_eosp(sta);
3979e705c121SKalle Valo 	rcu_read_unlock();
3980e705c121SKalle Valo }
3981e705c121SKalle Valo 
3982e705c121SKalle Valo void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3983e705c121SKalle Valo 				   struct iwl_mvm_sta *mvmsta, bool disable)
3984e705c121SKalle Valo {
3985e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3986e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3987e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3988e705c121SKalle Valo 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3989e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3990e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3991e705c121SKalle Valo 	};
3992e705c121SKalle Valo 	int ret;
3993e705c121SKalle Valo 
3994854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3995854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3996e705c121SKalle Valo 	if (ret)
3997e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3998e705c121SKalle Valo }
3999e705c121SKalle Valo 
4000e705c121SKalle Valo void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
4001e705c121SKalle Valo 				      struct ieee80211_sta *sta,
4002e705c121SKalle Valo 				      bool disable)
4003e705c121SKalle Valo {
4004e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4005e705c121SKalle Valo 
4006e705c121SKalle Valo 	spin_lock_bh(&mvm_sta->lock);
4007e705c121SKalle Valo 
4008e705c121SKalle Valo 	if (mvm_sta->disable_tx == disable) {
4009e705c121SKalle Valo 		spin_unlock_bh(&mvm_sta->lock);
4010e705c121SKalle Valo 		return;
4011e705c121SKalle Valo 	}
4012e705c121SKalle Valo 
4013e705c121SKalle Valo 	mvm_sta->disable_tx = disable;
4014e705c121SKalle Valo 
4015b1c6cec0SNaftali Goldstein 	/*
4016b1c6cec0SNaftali Goldstein 	 * If sta PS state is handled by mac80211, tell it to start/stop
4017b1c6cec0SNaftali Goldstein 	 * queuing tx for this station.
4018b1c6cec0SNaftali Goldstein 	 */
4019b1c6cec0SNaftali Goldstein 	if (!ieee80211_hw_check(mvm->hw, AP_LINK_PS))
4020e705c121SKalle Valo 		ieee80211_sta_block_awake(mvm->hw, sta, disable);
4021e705c121SKalle Valo 
4022e705c121SKalle Valo 	iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
4023e705c121SKalle Valo 
4024e705c121SKalle Valo 	spin_unlock_bh(&mvm_sta->lock);
4025e705c121SKalle Valo }
4026e705c121SKalle Valo 
4027ced19f26SSara Sharon static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
4028ced19f26SSara Sharon 					      struct iwl_mvm_vif *mvmvif,
4029ced19f26SSara Sharon 					      struct iwl_mvm_int_sta *sta,
4030ced19f26SSara Sharon 					      bool disable)
4031ced19f26SSara Sharon {
4032ced19f26SSara Sharon 	u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
4033ced19f26SSara Sharon 	struct iwl_mvm_add_sta_cmd cmd = {
4034ced19f26SSara Sharon 		.add_modify = STA_MODE_MODIFY,
4035ced19f26SSara Sharon 		.sta_id = sta->sta_id,
4036ced19f26SSara Sharon 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
4037ced19f26SSara Sharon 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
4038ced19f26SSara Sharon 		.mac_id_n_color = cpu_to_le32(id),
4039ced19f26SSara Sharon 	};
4040ced19f26SSara Sharon 	int ret;
4041ced19f26SSara Sharon 
4042e5d153ecSEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
4043ced19f26SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
4044ced19f26SSara Sharon 	if (ret)
4045ced19f26SSara Sharon 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
4046ced19f26SSara Sharon }
4047ced19f26SSara Sharon 
4048e705c121SKalle Valo void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
4049e705c121SKalle Valo 				       struct iwl_mvm_vif *mvmvif,
4050e705c121SKalle Valo 				       bool disable)
4051e705c121SKalle Valo {
4052e705c121SKalle Valo 	struct ieee80211_sta *sta;
4053e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta;
4054e705c121SKalle Valo 	int i;
4055e705c121SKalle Valo 
4056e5d153ecSEmmanuel Grumbach 	rcu_read_lock();
4057e705c121SKalle Valo 
4058e705c121SKalle Valo 	/* Block/unblock all the stations of the given mvmvif */
4059be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
4060e5d153ecSEmmanuel Grumbach 		sta = rcu_dereference(mvm->fw_id_to_mac_id[i]);
4061e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
4062e705c121SKalle Valo 			continue;
4063e705c121SKalle Valo 
4064e705c121SKalle Valo 		mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4065e705c121SKalle Valo 		if (mvm_sta->mac_id_n_color !=
4066e705c121SKalle Valo 		    FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
4067e705c121SKalle Valo 			continue;
4068e705c121SKalle Valo 
4069e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
4070e705c121SKalle Valo 	}
4071ced19f26SSara Sharon 
4072e5d153ecSEmmanuel Grumbach 	rcu_read_unlock();
4073e5d153ecSEmmanuel Grumbach 
4074ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
4075ced19f26SSara Sharon 		return;
4076ced19f26SSara Sharon 
4077ced19f26SSara Sharon 	/* Need to block/unblock also multicast station */
4078ced19f26SSara Sharon 	if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
4079ced19f26SSara Sharon 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
4080ced19f26SSara Sharon 						  &mvmvif->mcast_sta, disable);
4081ced19f26SSara Sharon 
4082ced19f26SSara Sharon 	/*
4083ced19f26SSara Sharon 	 * Only unblock the broadcast station (FW blocks it for immediate
4084ced19f26SSara Sharon 	 * quiet, not the driver)
4085ced19f26SSara Sharon 	 */
4086ced19f26SSara Sharon 	if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
4087ced19f26SSara Sharon 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
4088ced19f26SSara Sharon 						  &mvmvif->bcast_sta, disable);
4089e705c121SKalle Valo }
4090e705c121SKalle Valo 
4091e705c121SKalle Valo void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
4092e705c121SKalle Valo {
4093e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4094e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
4095e705c121SKalle Valo 
4096e705c121SKalle Valo 	rcu_read_lock();
4097e705c121SKalle Valo 
4098e705c121SKalle Valo 	mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
4099e705c121SKalle Valo 
4100ad12b231SNathan Errera 	if (mvmsta)
4101e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
4102e705c121SKalle Valo 
4103e705c121SKalle Valo 	rcu_read_unlock();
4104e705c121SKalle Valo }
4105dd32162dSLiad Kaufman 
4106dd32162dSLiad Kaufman u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
4107dd32162dSLiad Kaufman {
4108dd32162dSLiad Kaufman 	u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
4109dd32162dSLiad Kaufman 
4110dd32162dSLiad Kaufman 	/*
41112f7a3863SLuca Coelho 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
4112dd32162dSLiad Kaufman 	 * to align the wrap around of ssn so we compare relevant values.
4113dd32162dSLiad Kaufman 	 */
4114286ca8ebSLuca Coelho 	if (mvm->trans->trans_cfg->gen2)
4115dd32162dSLiad Kaufman 		sn &= 0xff;
4116dd32162dSLiad Kaufman 
4117dd32162dSLiad Kaufman 	return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
4118dd32162dSLiad Kaufman }
4119be82ecd3SAvraham Stern 
4120be82ecd3SAvraham Stern int iwl_mvm_add_pasn_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
4121be82ecd3SAvraham Stern 			 struct iwl_mvm_int_sta *sta, u8 *addr, u32 cipher,
4122be82ecd3SAvraham Stern 			 u8 *key, u32 key_len)
4123be82ecd3SAvraham Stern {
4124be82ecd3SAvraham Stern 	int ret;
4125be82ecd3SAvraham Stern 	u16 queue;
4126be82ecd3SAvraham Stern 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4127be82ecd3SAvraham Stern 	struct ieee80211_key_conf *keyconf;
4128be82ecd3SAvraham Stern 
4129be82ecd3SAvraham Stern 	ret = iwl_mvm_allocate_int_sta(mvm, sta, 0,
4130be82ecd3SAvraham Stern 				       NL80211_IFTYPE_UNSPECIFIED,
4131be82ecd3SAvraham Stern 				       IWL_STA_LINK);
4132be82ecd3SAvraham Stern 	if (ret)
4133be82ecd3SAvraham Stern 		return ret;
4134be82ecd3SAvraham Stern 
4135be82ecd3SAvraham Stern 	ret = iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
4136be82ecd3SAvraham Stern 					     addr, sta, &queue,
4137be82ecd3SAvraham Stern 					     IWL_MVM_TX_FIFO_BE);
4138be82ecd3SAvraham Stern 	if (ret)
4139be82ecd3SAvraham Stern 		goto out;
4140be82ecd3SAvraham Stern 
4141be82ecd3SAvraham Stern 	keyconf = kzalloc(sizeof(*keyconf) + key_len, GFP_KERNEL);
4142be82ecd3SAvraham Stern 	if (!keyconf) {
4143be82ecd3SAvraham Stern 		ret = -ENOBUFS;
4144be82ecd3SAvraham Stern 		goto out;
4145be82ecd3SAvraham Stern 	}
4146be82ecd3SAvraham Stern 
4147be82ecd3SAvraham Stern 	keyconf->cipher = cipher;
4148be82ecd3SAvraham Stern 	memcpy(keyconf->key, key, key_len);
4149be82ecd3SAvraham Stern 	keyconf->keylen = key_len;
4150be82ecd3SAvraham Stern 
4151be82ecd3SAvraham Stern 	ret = iwl_mvm_send_sta_key(mvm, sta->sta_id, keyconf, false,
4152be82ecd3SAvraham Stern 				   0, NULL, 0, 0, true);
4153be82ecd3SAvraham Stern 	kfree(keyconf);
4154be82ecd3SAvraham Stern 	return 0;
4155be82ecd3SAvraham Stern out:
4156be82ecd3SAvraham Stern 	iwl_mvm_dealloc_int_sta(mvm, sta);
4157be82ecd3SAvraham Stern 	return ret;
4158be82ecd3SAvraham Stern }
4159ad12b231SNathan Errera 
4160ad12b231SNathan Errera void iwl_mvm_cancel_channel_switch(struct iwl_mvm *mvm,
4161ad12b231SNathan Errera 				   struct ieee80211_vif *vif,
4162ad12b231SNathan Errera 				   u32 mac_id)
4163ad12b231SNathan Errera {
4164ad12b231SNathan Errera 	struct iwl_cancel_channel_switch_cmd cancel_channel_switch_cmd = {
4165ad12b231SNathan Errera 		.mac_id = cpu_to_le32(mac_id),
4166ad12b231SNathan Errera 	};
4167ad12b231SNathan Errera 	int ret;
4168ad12b231SNathan Errera 
4169ad12b231SNathan Errera 	ret = iwl_mvm_send_cmd_pdu(mvm,
4170f0c86427SJohannes Berg 				   WIDE_ID(MAC_CONF_GROUP, CANCEL_CHANNEL_SWITCH_CMD),
4171ad12b231SNathan Errera 				   CMD_ASYNC,
4172ad12b231SNathan Errera 				   sizeof(cancel_channel_switch_cmd),
4173ad12b231SNathan Errera 				   &cancel_channel_switch_cmd);
4174ad12b231SNathan Errera 	if (ret)
4175ad12b231SNathan Errera 		IWL_ERR(mvm, "Failed to cancel the channel switch\n");
4176ad12b231SNathan Errera }
4177