18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
38e99ea8dSJohannes Berg  * Copyright (C) 2012-2015, 2018-2020 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 
89e705c121SKalle Valo 	switch (sta->bandwidth) {
90e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_160:
91e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
925a2abdcaSGustavo A. R. Silva 		fallthrough;
93e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_80:
94e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
955a2abdcaSGustavo A. R. Silva 		fallthrough;
96e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_40:
97e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
985a2abdcaSGustavo A. R. Silva 		fallthrough;
99e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_20:
100e705c121SKalle Valo 		if (sta->ht_cap.ht_supported)
101e705c121SKalle Valo 			add_sta_cmd.station_flags |=
102e705c121SKalle Valo 				cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
103e705c121SKalle Valo 		break;
104e705c121SKalle Valo 	}
105e705c121SKalle Valo 
106e705c121SKalle Valo 	switch (sta->rx_nss) {
107e705c121SKalle Valo 	case 1:
108e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
109e705c121SKalle Valo 		break;
110e705c121SKalle Valo 	case 2:
111e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
112e705c121SKalle Valo 		break;
113e705c121SKalle Valo 	case 3 ... 8:
114e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
115e705c121SKalle Valo 		break;
116e705c121SKalle Valo 	}
117e705c121SKalle Valo 
118e705c121SKalle Valo 	switch (sta->smps_mode) {
119e705c121SKalle Valo 	case IEEE80211_SMPS_AUTOMATIC:
120e705c121SKalle Valo 	case IEEE80211_SMPS_NUM_MODES:
121e705c121SKalle Valo 		WARN_ON(1);
122e705c121SKalle Valo 		break;
123e705c121SKalle Valo 	case IEEE80211_SMPS_STATIC:
124e705c121SKalle Valo 		/* override NSS */
125e705c121SKalle Valo 		add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
126e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
127e705c121SKalle Valo 		break;
128e705c121SKalle Valo 	case IEEE80211_SMPS_DYNAMIC:
129e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
130e705c121SKalle Valo 		break;
131e705c121SKalle Valo 	case IEEE80211_SMPS_OFF:
132e705c121SKalle Valo 		/* nothing */
133e705c121SKalle Valo 		break;
134e705c121SKalle Valo 	}
135e705c121SKalle Valo 
136e705c121SKalle Valo 	if (sta->ht_cap.ht_supported) {
137e705c121SKalle Valo 		add_sta_cmd.station_flags_msk |=
138e705c121SKalle Valo 			cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
139e705c121SKalle Valo 				    STA_FLG_AGG_MPDU_DENS_MSK);
140e705c121SKalle Valo 
141e705c121SKalle Valo 		mpdu_dens = sta->ht_cap.ampdu_density;
142e705c121SKalle Valo 	}
143e705c121SKalle Valo 
144eae94cf8SLuca Coelho 	if (mvm_sta->vif->bss_conf.chandef.chan->band == NL80211_BAND_6GHZ) {
145eae94cf8SLuca Coelho 		add_sta_cmd.station_flags_msk |=
146eae94cf8SLuca Coelho 			cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
147eae94cf8SLuca Coelho 				    STA_FLG_AGG_MPDU_DENS_MSK);
148c8a2e7a2SMordechay Goodstein 
149eae94cf8SLuca Coelho 		mpdu_dens = le16_get_bits(sta->he_6ghz_capa.capa,
150eae94cf8SLuca Coelho 					  IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
151eae94cf8SLuca Coelho 		agg_size = le16_get_bits(sta->he_6ghz_capa.capa,
152eae94cf8SLuca Coelho 				IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
153eae94cf8SLuca Coelho 	} else
154e705c121SKalle Valo 	if (sta->vht_cap.vht_supported) {
155e705c121SKalle Valo 		agg_size = sta->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;
159e705c121SKalle Valo 	} else if (sta->ht_cap.ht_supported) {
160e705c121SKalle Valo 		agg_size = sta->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 	 */
171c8a2e7a2SMordechay Goodstein 	if (sta->he_cap.has_he)
172c8a2e7a2SMordechay Goodstein 		agg_size += u8_get_bits(sta->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,
319cfbc6c4cSSara Sharon 			       int queue, u8 tid, u8 flags)
32099448a8cSJohannes Berg {
32199448a8cSJohannes Berg 	struct iwl_scd_txq_cfg_cmd cmd = {
32299448a8cSJohannes Berg 		.scd_queue = queue,
32399448a8cSJohannes Berg 		.action = SCD_CFG_DISABLE_QUEUE,
32499448a8cSJohannes Berg 	};
32599448a8cSJohannes Berg 	int ret;
32699448a8cSJohannes Berg 
32799448a8cSJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm)) {
32899448a8cSJohannes Berg 		iwl_trans_txq_free(mvm->trans, queue);
32999448a8cSJohannes Berg 
33099448a8cSJohannes Berg 		return 0;
33199448a8cSJohannes Berg 	}
33299448a8cSJohannes Berg 
333f3f240f9SJohannes Berg 	if (WARN_ON(mvm->queue_info[queue].tid_bitmap == 0))
33499448a8cSJohannes Berg 		return 0;
33599448a8cSJohannes Berg 
33699448a8cSJohannes Berg 	mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
33799448a8cSJohannes Berg 
3381c14089eSJohannes Berg 	cmd.action = mvm->queue_info[queue].tid_bitmap ?
33999448a8cSJohannes Berg 		SCD_CFG_ENABLE_QUEUE : SCD_CFG_DISABLE_QUEUE;
34099448a8cSJohannes Berg 	if (cmd.action == SCD_CFG_DISABLE_QUEUE)
34199448a8cSJohannes Berg 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_FREE;
34299448a8cSJohannes Berg 
34399448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
344cfbc6c4cSSara Sharon 			    "Disabling TXQ #%d tids=0x%x\n",
34599448a8cSJohannes Berg 			    queue,
346cfbc6c4cSSara Sharon 			    mvm->queue_info[queue].tid_bitmap);
34799448a8cSJohannes Berg 
34899448a8cSJohannes Berg 	/* If the queue is still enabled - nothing left to do in this func */
349f3f240f9SJohannes Berg 	if (cmd.action == SCD_CFG_ENABLE_QUEUE)
35099448a8cSJohannes Berg 		return 0;
35199448a8cSJohannes Berg 
35299448a8cSJohannes Berg 	cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
35399448a8cSJohannes Berg 	cmd.tid = mvm->queue_info[queue].txq_tid;
35499448a8cSJohannes Berg 
35599448a8cSJohannes Berg 	/* Make sure queue info is correct even though we overwrite it */
356cfbc6c4cSSara Sharon 	WARN(mvm->queue_info[queue].tid_bitmap,
357cfbc6c4cSSara Sharon 	     "TXQ #%d info out-of-sync - tids=0x%x\n",
358cfbc6c4cSSara Sharon 	     queue, mvm->queue_info[queue].tid_bitmap);
35999448a8cSJohannes Berg 
36099448a8cSJohannes Berg 	/* If we are here - the queue is freed and we can zero out these vals */
36199448a8cSJohannes Berg 	mvm->queue_info[queue].tid_bitmap = 0;
362cfbc6c4cSSara Sharon 
363cfbc6c4cSSara Sharon 	if (sta) {
364cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
365cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_tid(sta, tid);
366cfbc6c4cSSara Sharon 
367cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
368cfbc6c4cSSara Sharon 	}
36999448a8cSJohannes Berg 
37099448a8cSJohannes Berg 	/* Regardless if this is a reserved TXQ for a STA - mark it as false */
37199448a8cSJohannes Berg 	mvm->queue_info[queue].reserved = false;
37299448a8cSJohannes Berg 
37399448a8cSJohannes Berg 	iwl_trans_txq_disable(mvm->trans, queue, false);
37499448a8cSJohannes Berg 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, flags,
37599448a8cSJohannes Berg 				   sizeof(struct iwl_scd_txq_cfg_cmd), &cmd);
37699448a8cSJohannes Berg 
37799448a8cSJohannes Berg 	if (ret)
37899448a8cSJohannes Berg 		IWL_ERR(mvm, "Failed to disable queue %d (ret=%d)\n",
37999448a8cSJohannes Berg 			queue, ret);
38099448a8cSJohannes Berg 	return ret;
38199448a8cSJohannes Berg }
38299448a8cSJohannes Berg 
38342db09c1SLiad Kaufman static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
38442db09c1SLiad Kaufman {
38542db09c1SLiad Kaufman 	struct ieee80211_sta *sta;
38642db09c1SLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
38742db09c1SLiad Kaufman 	unsigned long tid_bitmap;
38842db09c1SLiad Kaufman 	unsigned long agg_tids = 0;
389806911daSSharon Dvir 	u8 sta_id;
39042db09c1SLiad Kaufman 	int tid;
39142db09c1SLiad Kaufman 
39242db09c1SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
39342db09c1SLiad Kaufman 
394bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
395bb49701bSSara Sharon 		return -EINVAL;
396bb49701bSSara Sharon 
39742db09c1SLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
39842db09c1SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
39942db09c1SLiad Kaufman 
40042db09c1SLiad Kaufman 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
40142db09c1SLiad Kaufman 					lockdep_is_held(&mvm->mutex));
40242db09c1SLiad Kaufman 
40342db09c1SLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
40442db09c1SLiad Kaufman 		return -EINVAL;
40542db09c1SLiad Kaufman 
40642db09c1SLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
40742db09c1SLiad Kaufman 
40842db09c1SLiad Kaufman 	spin_lock_bh(&mvmsta->lock);
40942db09c1SLiad Kaufman 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
41042db09c1SLiad Kaufman 		if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
41142db09c1SLiad Kaufman 			agg_tids |= BIT(tid);
41242db09c1SLiad Kaufman 	}
41342db09c1SLiad Kaufman 	spin_unlock_bh(&mvmsta->lock);
41442db09c1SLiad Kaufman 
41542db09c1SLiad Kaufman 	return agg_tids;
41642db09c1SLiad Kaufman }
41742db09c1SLiad Kaufman 
4189794c64fSLiad Kaufman /*
4199794c64fSLiad Kaufman  * Remove a queue from a station's resources.
4209794c64fSLiad Kaufman  * Note that this only marks as free. It DOESN'T delete a BA agreement, and
4219794c64fSLiad Kaufman  * doesn't disable the queue
4229794c64fSLiad Kaufman  */
4239794c64fSLiad Kaufman static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
4249794c64fSLiad Kaufman {
4259794c64fSLiad Kaufman 	struct ieee80211_sta *sta;
4269794c64fSLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
4279794c64fSLiad Kaufman 	unsigned long tid_bitmap;
4289794c64fSLiad Kaufman 	unsigned long disable_agg_tids = 0;
4299794c64fSLiad Kaufman 	u8 sta_id;
4309794c64fSLiad Kaufman 	int tid;
4319794c64fSLiad Kaufman 
4329794c64fSLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
4339794c64fSLiad Kaufman 
434bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
435bb49701bSSara Sharon 		return -EINVAL;
436bb49701bSSara Sharon 
4379794c64fSLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
4389794c64fSLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
4399794c64fSLiad Kaufman 
4409794c64fSLiad Kaufman 	rcu_read_lock();
4419794c64fSLiad Kaufman 
4429794c64fSLiad Kaufman 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
4439794c64fSLiad Kaufman 
4449794c64fSLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
4459794c64fSLiad Kaufman 		rcu_read_unlock();
4469794c64fSLiad Kaufman 		return 0;
4479794c64fSLiad Kaufman 	}
4489794c64fSLiad Kaufman 
4499794c64fSLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
4509794c64fSLiad Kaufman 
4519794c64fSLiad Kaufman 	spin_lock_bh(&mvmsta->lock);
45242db09c1SLiad Kaufman 	/* Unmap MAC queues and TIDs from this queue */
4539794c64fSLiad Kaufman 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
454cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
455cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_tid(sta, tid);
456cfbc6c4cSSara Sharon 
4579794c64fSLiad Kaufman 		if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
4589794c64fSLiad Kaufman 			disable_agg_tids |= BIT(tid);
4596862fceeSSara Sharon 		mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
460cfbc6c4cSSara Sharon 
461cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
4629794c64fSLiad Kaufman 	}
4639794c64fSLiad Kaufman 
46442db09c1SLiad Kaufman 	mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
4659794c64fSLiad Kaufman 	spin_unlock_bh(&mvmsta->lock);
4669794c64fSLiad Kaufman 
4679794c64fSLiad Kaufman 	rcu_read_unlock();
4689794c64fSLiad Kaufman 
46906bc6f6eSJohannes Berg 	/*
47006bc6f6eSJohannes Berg 	 * The TX path may have been using this TXQ_ID from the tid_data,
47106bc6f6eSJohannes Berg 	 * so make sure it's no longer running so that we can safely reuse
47206bc6f6eSJohannes Berg 	 * this TXQ later. We've set all the TIDs to IWL_MVM_INVALID_QUEUE
47306bc6f6eSJohannes Berg 	 * above, but nothing guarantees we've stopped using them. Thus,
47406bc6f6eSJohannes Berg 	 * without this, we could get to iwl_mvm_disable_txq() and remove
47506bc6f6eSJohannes Berg 	 * the queue while still sending frames to it.
47606bc6f6eSJohannes Berg 	 */
47706bc6f6eSJohannes Berg 	synchronize_net();
47806bc6f6eSJohannes Berg 
4799794c64fSLiad Kaufman 	return disable_agg_tids;
4809794c64fSLiad Kaufman }
4819794c64fSLiad Kaufman 
48201796ff2SSara Sharon static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
483cfbc6c4cSSara Sharon 				       struct ieee80211_sta *old_sta,
484724fe771SJohannes Berg 				       u8 new_sta_id)
48501796ff2SSara Sharon {
48601796ff2SSara Sharon 	struct iwl_mvm_sta *mvmsta;
487cfbc6c4cSSara Sharon 	u8 sta_id, tid;
48801796ff2SSara Sharon 	unsigned long disable_agg_tids = 0;
489724fe771SJohannes Berg 	bool same_sta;
49001796ff2SSara Sharon 	int ret;
49101796ff2SSara Sharon 
49201796ff2SSara Sharon 	lockdep_assert_held(&mvm->mutex);
49301796ff2SSara Sharon 
494bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
495bb49701bSSara Sharon 		return -EINVAL;
496bb49701bSSara Sharon 
49701796ff2SSara Sharon 	sta_id = mvm->queue_info[queue].ra_sta_id;
49801796ff2SSara Sharon 	tid = mvm->queue_info[queue].txq_tid;
49901796ff2SSara Sharon 
500724fe771SJohannes Berg 	same_sta = sta_id == new_sta_id;
501724fe771SJohannes Berg 
50201796ff2SSara Sharon 	mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
503e3df1e4bSSharon Dvir 	if (WARN_ON(!mvmsta))
504e3df1e4bSSharon Dvir 		return -EINVAL;
50501796ff2SSara Sharon 
50601796ff2SSara Sharon 	disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
50701796ff2SSara Sharon 	/* Disable the queue */
50801796ff2SSara Sharon 	if (disable_agg_tids)
50901796ff2SSara Sharon 		iwl_mvm_invalidate_sta_queue(mvm, queue,
51001796ff2SSara Sharon 					     disable_agg_tids, false);
51101796ff2SSara Sharon 
512cfbc6c4cSSara Sharon 	ret = iwl_mvm_disable_txq(mvm, old_sta, queue, tid, 0);
51301796ff2SSara Sharon 	if (ret) {
51401796ff2SSara Sharon 		IWL_ERR(mvm,
51501796ff2SSara Sharon 			"Failed to free inactive queue %d (ret=%d)\n",
51601796ff2SSara Sharon 			queue, ret);
51701796ff2SSara Sharon 
51801796ff2SSara Sharon 		return ret;
51901796ff2SSara Sharon 	}
52001796ff2SSara Sharon 
52101796ff2SSara Sharon 	/* If TXQ is allocated to another STA, update removal in FW */
52201796ff2SSara Sharon 	if (!same_sta)
52301796ff2SSara Sharon 		iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
52401796ff2SSara Sharon 
52501796ff2SSara Sharon 	return 0;
52601796ff2SSara Sharon }
52701796ff2SSara Sharon 
52842db09c1SLiad Kaufman static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
52942db09c1SLiad Kaufman 				    unsigned long tfd_queue_mask, u8 ac)
53042db09c1SLiad Kaufman {
53142db09c1SLiad Kaufman 	int queue = 0;
53242db09c1SLiad Kaufman 	u8 ac_to_queue[IEEE80211_NUM_ACS];
53342db09c1SLiad Kaufman 	int i;
53442db09c1SLiad Kaufman 
53590d2d94cSJohannes Berg 	/*
53690d2d94cSJohannes Berg 	 * This protects us against grabbing a queue that's being reconfigured
53790d2d94cSJohannes Berg 	 * by the inactivity checker.
53890d2d94cSJohannes Berg 	 */
53990d2d94cSJohannes Berg 	lockdep_assert_held(&mvm->mutex);
54090d2d94cSJohannes Berg 
541bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
542bb49701bSSara Sharon 		return -EINVAL;
54342db09c1SLiad Kaufman 
54442db09c1SLiad Kaufman 	memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
54542db09c1SLiad Kaufman 
54642db09c1SLiad Kaufman 	/* See what ACs the existing queues for this STA have */
54742db09c1SLiad Kaufman 	for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
54842db09c1SLiad Kaufman 		/* Only DATA queues can be shared */
54942db09c1SLiad Kaufman 		if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
55042db09c1SLiad Kaufman 		    i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
55142db09c1SLiad Kaufman 			continue;
55242db09c1SLiad Kaufman 
55342db09c1SLiad Kaufman 		ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
55442db09c1SLiad Kaufman 	}
55542db09c1SLiad Kaufman 
55642db09c1SLiad Kaufman 	/*
55742db09c1SLiad Kaufman 	 * The queue to share is chosen only from DATA queues as follows (in
55842db09c1SLiad Kaufman 	 * descending priority):
55942db09c1SLiad Kaufman 	 * 1. An AC_BE queue
56042db09c1SLiad Kaufman 	 * 2. Same AC queue
56142db09c1SLiad Kaufman 	 * 3. Highest AC queue that is lower than new AC
56242db09c1SLiad Kaufman 	 * 4. Any existing AC (there always is at least 1 DATA queue)
56342db09c1SLiad Kaufman 	 */
56442db09c1SLiad Kaufman 
56542db09c1SLiad Kaufman 	/* Priority 1: An AC_BE queue */
56642db09c1SLiad Kaufman 	if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
56742db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_BE];
56842db09c1SLiad Kaufman 	/* Priority 2: Same AC queue */
56942db09c1SLiad Kaufman 	else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
57042db09c1SLiad Kaufman 		queue = ac_to_queue[ac];
57142db09c1SLiad Kaufman 	/* Priority 3a: If new AC is VO and VI exists - use VI */
57242db09c1SLiad Kaufman 	else if (ac == IEEE80211_AC_VO &&
57342db09c1SLiad Kaufman 		 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
57442db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VI];
57542db09c1SLiad Kaufman 	/* Priority 3b: No BE so only AC less than the new one is BK */
57642db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
57742db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_BK];
57842db09c1SLiad Kaufman 	/* Priority 4a: No BE nor BK - use VI if exists */
57942db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
58042db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VI];
58142db09c1SLiad Kaufman 	/* Priority 4b: No BE, BK nor VI - use VO if exists */
58242db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
58342db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VO];
58442db09c1SLiad Kaufman 
58542db09c1SLiad Kaufman 	/* Make sure queue found (or not) is legal */
5869f9af3d7SLiad Kaufman 	if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
5879f9af3d7SLiad Kaufman 	    !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
5889f9af3d7SLiad Kaufman 	    (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
58942db09c1SLiad Kaufman 		IWL_ERR(mvm, "No DATA queues available to share\n");
5909f9af3d7SLiad Kaufman 		return -ENOSPC;
5919f9af3d7SLiad Kaufman 	}
5929f9af3d7SLiad Kaufman 
59342db09c1SLiad Kaufman 	return queue;
59442db09c1SLiad Kaufman }
59542db09c1SLiad Kaufman 
59658f2cc57SLiad Kaufman /*
5979f9af3d7SLiad Kaufman  * If a given queue has a higher AC than the TID stream that is being compared
5989f9af3d7SLiad Kaufman  * to, the queue needs to be redirected to the lower AC. This function does that
59958f2cc57SLiad Kaufman  * in such a case, otherwise - if no redirection required - it does nothing,
60058f2cc57SLiad Kaufman  * unless the %force param is true.
60158f2cc57SLiad Kaufman  */
602cfbc6c4cSSara Sharon static int iwl_mvm_redirect_queue(struct iwl_mvm *mvm, int queue, int tid,
60358f2cc57SLiad Kaufman 				  int ac, int ssn, unsigned int wdg_timeout,
604cfbc6c4cSSara Sharon 				  bool force, struct iwl_mvm_txq *txq)
60558f2cc57SLiad Kaufman {
60658f2cc57SLiad Kaufman 	struct iwl_scd_txq_cfg_cmd cmd = {
60758f2cc57SLiad Kaufman 		.scd_queue = queue,
608f7c692deSLiad Kaufman 		.action = SCD_CFG_DISABLE_QUEUE,
60958f2cc57SLiad Kaufman 	};
61058f2cc57SLiad Kaufman 	bool shared_queue;
61158f2cc57SLiad Kaufman 	int ret;
61258f2cc57SLiad Kaufman 
613bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
614bb49701bSSara Sharon 		return -EINVAL;
615bb49701bSSara Sharon 
61658f2cc57SLiad Kaufman 	/*
61758f2cc57SLiad Kaufman 	 * If the AC is lower than current one - FIFO needs to be redirected to
61858f2cc57SLiad Kaufman 	 * the lowest one of the streams in the queue. Check if this is needed
61958f2cc57SLiad Kaufman 	 * here.
62058f2cc57SLiad Kaufman 	 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
62158f2cc57SLiad Kaufman 	 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
62258f2cc57SLiad Kaufman 	 * we need to check if the numerical value of X is LARGER than of Y.
62358f2cc57SLiad Kaufman 	 */
62458f2cc57SLiad Kaufman 	if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
62558f2cc57SLiad Kaufman 		IWL_DEBUG_TX_QUEUES(mvm,
62658f2cc57SLiad Kaufman 				    "No redirection needed on TXQ #%d\n",
62758f2cc57SLiad Kaufman 				    queue);
62858f2cc57SLiad Kaufman 		return 0;
62958f2cc57SLiad Kaufman 	}
63058f2cc57SLiad Kaufman 
63158f2cc57SLiad Kaufman 	cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
63258f2cc57SLiad Kaufman 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
633edbe961cSLiad Kaufman 	cmd.tid = mvm->queue_info[queue].txq_tid;
6341c14089eSJohannes Berg 	shared_queue = hweight16(mvm->queue_info[queue].tid_bitmap) > 1;
63558f2cc57SLiad Kaufman 
6369f9af3d7SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
63758f2cc57SLiad Kaufman 			    queue, iwl_mvm_ac_to_tx_fifo[ac]);
63858f2cc57SLiad Kaufman 
639cfbc6c4cSSara Sharon 	/* Stop the queue and wait for it to empty */
640cfbc6c4cSSara Sharon 	txq->stopped = true;
641cfbc6c4cSSara Sharon 
642a1a57877SSara Sharon 	ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue));
64358f2cc57SLiad Kaufman 	if (ret) {
64458f2cc57SLiad Kaufman 		IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
64558f2cc57SLiad Kaufman 			queue);
64658f2cc57SLiad Kaufman 		ret = -EIO;
64758f2cc57SLiad Kaufman 		goto out;
64858f2cc57SLiad Kaufman 	}
64958f2cc57SLiad Kaufman 
65058f2cc57SLiad Kaufman 	/* Before redirecting the queue we need to de-activate it */
65158f2cc57SLiad Kaufman 	iwl_trans_txq_disable(mvm->trans, queue, false);
65258f2cc57SLiad Kaufman 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
65358f2cc57SLiad Kaufman 	if (ret)
65458f2cc57SLiad Kaufman 		IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
65558f2cc57SLiad Kaufman 			ret);
65658f2cc57SLiad Kaufman 
65758f2cc57SLiad Kaufman 	/* Make sure the SCD wrptr is correctly set before reconfiguring */
658ca3b9c6bSSara Sharon 	iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
65958f2cc57SLiad Kaufman 
660edbe961cSLiad Kaufman 	/* Update the TID "owner" of the queue */
661edbe961cSLiad Kaufman 	mvm->queue_info[queue].txq_tid = tid;
662edbe961cSLiad Kaufman 
66358f2cc57SLiad Kaufman 	/* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
66458f2cc57SLiad Kaufman 
66558f2cc57SLiad Kaufman 	/* Redirect to lower AC */
66658f2cc57SLiad Kaufman 	iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
6670ec9257bSSara Sharon 			     cmd.sta_id, tid, IWL_FRAME_LIMIT, ssn);
66858f2cc57SLiad Kaufman 
66958f2cc57SLiad Kaufman 	/* Update AC marking of the queue */
67058f2cc57SLiad Kaufman 	mvm->queue_info[queue].mac80211_ac = ac;
67158f2cc57SLiad Kaufman 
67258f2cc57SLiad Kaufman 	/*
67358f2cc57SLiad Kaufman 	 * Mark queue as shared in transport if shared
67458f2cc57SLiad Kaufman 	 * Note this has to be done after queue enablement because enablement
67558f2cc57SLiad Kaufman 	 * can also set this value, and there is no indication there to shared
67658f2cc57SLiad Kaufman 	 * queues
67758f2cc57SLiad Kaufman 	 */
67858f2cc57SLiad Kaufman 	if (shared_queue)
67958f2cc57SLiad Kaufman 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
68058f2cc57SLiad Kaufman 
68158f2cc57SLiad Kaufman out:
682cfbc6c4cSSara Sharon 	/* Continue using the queue */
683cfbc6c4cSSara Sharon 	txq->stopped = false;
68458f2cc57SLiad Kaufman 
68558f2cc57SLiad Kaufman 	return ret;
68658f2cc57SLiad Kaufman }
68758f2cc57SLiad Kaufman 
68899448a8cSJohannes Berg static int iwl_mvm_find_free_queue(struct iwl_mvm *mvm, u8 sta_id,
68999448a8cSJohannes Berg 				   u8 minq, u8 maxq)
69099448a8cSJohannes Berg {
69199448a8cSJohannes Berg 	int i;
69299448a8cSJohannes Berg 
693f3f240f9SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
69499448a8cSJohannes Berg 
695e5b72e3bSJohannes Berg 	if (WARN(maxq >= mvm->trans->trans_cfg->base_params->num_of_queues,
696e5b72e3bSJohannes Berg 		 "max queue %d >= num_of_queues (%d)", maxq,
697e5b72e3bSJohannes Berg 		 mvm->trans->trans_cfg->base_params->num_of_queues))
698e5b72e3bSJohannes Berg 		maxq = mvm->trans->trans_cfg->base_params->num_of_queues - 1;
699e5b72e3bSJohannes Berg 
70099448a8cSJohannes Berg 	/* This should not be hit with new TX path */
70199448a8cSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
70299448a8cSJohannes Berg 		return -ENOSPC;
70399448a8cSJohannes Berg 
70499448a8cSJohannes Berg 	/* Start by looking for a free queue */
70599448a8cSJohannes Berg 	for (i = minq; i <= maxq; i++)
7061c14089eSJohannes Berg 		if (mvm->queue_info[i].tid_bitmap == 0 &&
70799448a8cSJohannes Berg 		    mvm->queue_info[i].status == IWL_MVM_QUEUE_FREE)
70899448a8cSJohannes Berg 			return i;
70999448a8cSJohannes Berg 
71099448a8cSJohannes Berg 	return -ENOSPC;
71199448a8cSJohannes Berg }
71299448a8cSJohannes Berg 
713cfbc6c4cSSara Sharon static int iwl_mvm_tvqm_enable_txq(struct iwl_mvm *mvm,
71499448a8cSJohannes Berg 				   u8 sta_id, u8 tid, unsigned int timeout)
71599448a8cSJohannes Berg {
7169a16ee0cSShaul Triebitz 	int queue, size = max_t(u32, IWL_DEFAULT_QUEUE_SIZE,
7179a16ee0cSShaul Triebitz 				mvm->trans->cfg->min_256_ba_txq_size);
71899448a8cSJohannes Berg 
71999448a8cSJohannes Berg 	if (tid == IWL_MAX_TID_COUNT) {
72099448a8cSJohannes Berg 		tid = IWL_MGMT_TID;
721ff911dcaSShaul Triebitz 		size = max_t(u32, IWL_MGMT_QUEUE_SIZE,
722ff911dcaSShaul Triebitz 			     mvm->trans->cfg->min_txq_size);
72399448a8cSJohannes Berg 	}
72499448a8cSJohannes Berg 
72592f78d4bSJohannes Berg 	do {
72692f78d4bSJohannes Berg 		__le16 enable = cpu_to_le16(TX_QUEUE_CFG_ENABLE_QUEUE);
72792f78d4bSJohannes Berg 
72892f78d4bSJohannes Berg 		queue = iwl_trans_txq_alloc(mvm->trans, enable,
72992f78d4bSJohannes Berg 					    sta_id, tid, SCD_QUEUE_CFG,
73092f78d4bSJohannes Berg 					    size, timeout);
73192f78d4bSJohannes Berg 
73292f78d4bSJohannes Berg 		if (queue < 0)
73399448a8cSJohannes Berg 			IWL_DEBUG_TX_QUEUES(mvm,
73492f78d4bSJohannes Berg 					    "Failed allocating TXQ of size %d for sta %d tid %d, ret: %d\n",
73592f78d4bSJohannes Berg 					    size, sta_id, tid, queue);
73692f78d4bSJohannes Berg 		size /= 2;
73792f78d4bSJohannes Berg 	} while (queue < 0 && size >= 16);
73892f78d4bSJohannes Berg 
73992f78d4bSJohannes Berg 	if (queue < 0)
74099448a8cSJohannes Berg 		return queue;
74199448a8cSJohannes Berg 
74299448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d for sta %d tid %d\n",
74399448a8cSJohannes Berg 			    queue, sta_id, tid);
74499448a8cSJohannes Berg 
74599448a8cSJohannes Berg 	return queue;
74699448a8cSJohannes Berg }
74799448a8cSJohannes Berg 
748310181ecSSara Sharon static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
749310181ecSSara Sharon 					struct ieee80211_sta *sta, u8 ac,
750310181ecSSara Sharon 					int tid)
751310181ecSSara Sharon {
752310181ecSSara Sharon 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
753cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq =
754cfbc6c4cSSara Sharon 		iwl_mvm_txq_from_tid(sta, tid);
755310181ecSSara Sharon 	unsigned int wdg_timeout =
756310181ecSSara Sharon 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
757310181ecSSara Sharon 	int queue = -1;
758310181ecSSara Sharon 
759310181ecSSara Sharon 	lockdep_assert_held(&mvm->mutex);
760310181ecSSara Sharon 
761310181ecSSara Sharon 	IWL_DEBUG_TX_QUEUES(mvm,
762310181ecSSara Sharon 			    "Allocating queue for sta %d on tid %d\n",
763310181ecSSara Sharon 			    mvmsta->sta_id, tid);
764cfbc6c4cSSara Sharon 	queue = iwl_mvm_tvqm_enable_txq(mvm, mvmsta->sta_id, tid, wdg_timeout);
765310181ecSSara Sharon 	if (queue < 0)
766310181ecSSara Sharon 		return queue;
767310181ecSSara Sharon 
768cfbc6c4cSSara Sharon 	mvmtxq->txq_id = queue;
769cfbc6c4cSSara Sharon 	mvm->tvqm_info[queue].txq_tid = tid;
770cfbc6c4cSSara Sharon 	mvm->tvqm_info[queue].sta_id = mvmsta->sta_id;
771cfbc6c4cSSara Sharon 
772310181ecSSara Sharon 	IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
773310181ecSSara Sharon 
774310181ecSSara Sharon 	spin_lock_bh(&mvmsta->lock);
775310181ecSSara Sharon 	mvmsta->tid_data[tid].txq_id = queue;
776310181ecSSara Sharon 	spin_unlock_bh(&mvmsta->lock);
777310181ecSSara Sharon 
778310181ecSSara Sharon 	return 0;
779310181ecSSara Sharon }
780310181ecSSara Sharon 
781cfbc6c4cSSara Sharon static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm,
782cfbc6c4cSSara Sharon 				       struct ieee80211_sta *sta,
783cfbc6c4cSSara Sharon 				       int queue, u8 sta_id, u8 tid)
78499448a8cSJohannes Berg {
78599448a8cSJohannes Berg 	bool enable_queue = true;
78699448a8cSJohannes Berg 
78799448a8cSJohannes Berg 	/* Make sure this TID isn't already enabled */
78899448a8cSJohannes Berg 	if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) {
78999448a8cSJohannes Berg 		IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n",
79099448a8cSJohannes Berg 			queue, tid);
79199448a8cSJohannes Berg 		return false;
79299448a8cSJohannes Berg 	}
79399448a8cSJohannes Berg 
79499448a8cSJohannes Berg 	/* Update mappings and refcounts */
7951c14089eSJohannes Berg 	if (mvm->queue_info[queue].tid_bitmap)
79699448a8cSJohannes Berg 		enable_queue = false;
79799448a8cSJohannes Berg 
79899448a8cSJohannes Berg 	mvm->queue_info[queue].tid_bitmap |= BIT(tid);
79999448a8cSJohannes Berg 	mvm->queue_info[queue].ra_sta_id = sta_id;
80099448a8cSJohannes Berg 
80199448a8cSJohannes Berg 	if (enable_queue) {
80299448a8cSJohannes Berg 		if (tid != IWL_MAX_TID_COUNT)
80399448a8cSJohannes Berg 			mvm->queue_info[queue].mac80211_ac =
80499448a8cSJohannes Berg 				tid_to_mac80211_ac[tid];
80599448a8cSJohannes Berg 		else
80699448a8cSJohannes Berg 			mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO;
80799448a8cSJohannes Berg 
80899448a8cSJohannes Berg 		mvm->queue_info[queue].txq_tid = tid;
80999448a8cSJohannes Berg 	}
81099448a8cSJohannes Berg 
811cfbc6c4cSSara Sharon 	if (sta) {
812cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
813cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_tid(sta, tid);
814cfbc6c4cSSara Sharon 
815cfbc6c4cSSara Sharon 		mvmtxq->txq_id = queue;
816cfbc6c4cSSara Sharon 	}
817cfbc6c4cSSara Sharon 
81899448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
819cfbc6c4cSSara Sharon 			    "Enabling TXQ #%d tids=0x%x\n",
820cfbc6c4cSSara Sharon 			    queue, mvm->queue_info[queue].tid_bitmap);
82199448a8cSJohannes Berg 
82299448a8cSJohannes Berg 	return enable_queue;
82399448a8cSJohannes Berg }
82499448a8cSJohannes Berg 
825cfbc6c4cSSara Sharon static bool iwl_mvm_enable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
826cfbc6c4cSSara Sharon 			       int queue, u16 ssn,
82799448a8cSJohannes Berg 			       const struct iwl_trans_txq_scd_cfg *cfg,
82899448a8cSJohannes Berg 			       unsigned int wdg_timeout)
82999448a8cSJohannes Berg {
83099448a8cSJohannes Berg 	struct iwl_scd_txq_cfg_cmd cmd = {
83199448a8cSJohannes Berg 		.scd_queue = queue,
83299448a8cSJohannes Berg 		.action = SCD_CFG_ENABLE_QUEUE,
83399448a8cSJohannes Berg 		.window = cfg->frame_limit,
83499448a8cSJohannes Berg 		.sta_id = cfg->sta_id,
83599448a8cSJohannes Berg 		.ssn = cpu_to_le16(ssn),
83699448a8cSJohannes Berg 		.tx_fifo = cfg->fifo,
83799448a8cSJohannes Berg 		.aggregate = cfg->aggregate,
83899448a8cSJohannes Berg 		.tid = cfg->tid,
83999448a8cSJohannes Berg 	};
84099448a8cSJohannes Berg 	bool inc_ssn;
84199448a8cSJohannes Berg 
84299448a8cSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
84399448a8cSJohannes Berg 		return false;
84499448a8cSJohannes Berg 
84599448a8cSJohannes Berg 	/* Send the enabling command if we need to */
846cfbc6c4cSSara Sharon 	if (!iwl_mvm_update_txq_mapping(mvm, sta, queue, cfg->sta_id, cfg->tid))
84799448a8cSJohannes Berg 		return false;
84899448a8cSJohannes Berg 
84999448a8cSJohannes Berg 	inc_ssn = iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn,
85099448a8cSJohannes Berg 					   NULL, wdg_timeout);
85199448a8cSJohannes Berg 	if (inc_ssn)
85299448a8cSJohannes Berg 		le16_add_cpu(&cmd.ssn, 1);
85399448a8cSJohannes Berg 
85499448a8cSJohannes Berg 	WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd),
85599448a8cSJohannes Berg 	     "Failed to configure queue %d on FIFO %d\n", queue, cfg->fifo);
85699448a8cSJohannes Berg 
85799448a8cSJohannes Berg 	return inc_ssn;
85899448a8cSJohannes Berg }
85999448a8cSJohannes Berg 
860b3a87f11SJohannes Berg static void iwl_mvm_change_queue_tid(struct iwl_mvm *mvm, int queue)
86119aefa45SLiad Kaufman {
86219aefa45SLiad Kaufman 	struct iwl_scd_txq_cfg_cmd cmd = {
86319aefa45SLiad Kaufman 		.scd_queue = queue,
86419aefa45SLiad Kaufman 		.action = SCD_CFG_UPDATE_QUEUE_TID,
86519aefa45SLiad Kaufman 	};
86619aefa45SLiad Kaufman 	int tid;
86719aefa45SLiad Kaufman 	unsigned long tid_bitmap;
86819aefa45SLiad Kaufman 	int ret;
86919aefa45SLiad Kaufman 
87019aefa45SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
87119aefa45SLiad Kaufman 
872bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
873bb49701bSSara Sharon 		return;
874bb49701bSSara Sharon 
87519aefa45SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
87619aefa45SLiad Kaufman 
87719aefa45SLiad Kaufman 	if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
87819aefa45SLiad Kaufman 		return;
87919aefa45SLiad Kaufman 
88019aefa45SLiad Kaufman 	/* Find any TID for queue */
88119aefa45SLiad Kaufman 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
88219aefa45SLiad Kaufman 	cmd.tid = tid;
88319aefa45SLiad Kaufman 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
88419aefa45SLiad Kaufman 
88519aefa45SLiad Kaufman 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
886341ca402SLiad Kaufman 	if (ret) {
88719aefa45SLiad Kaufman 		IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
88819aefa45SLiad Kaufman 			queue, ret);
889341ca402SLiad Kaufman 		return;
890341ca402SLiad Kaufman 	}
891341ca402SLiad Kaufman 
892341ca402SLiad Kaufman 	mvm->queue_info[queue].txq_tid = tid;
89319aefa45SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
89419aefa45SLiad Kaufman 			    queue, tid);
89519aefa45SLiad Kaufman }
89619aefa45SLiad Kaufman 
8979f9af3d7SLiad Kaufman static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
8989f9af3d7SLiad Kaufman {
8999f9af3d7SLiad Kaufman 	struct ieee80211_sta *sta;
9009f9af3d7SLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
901806911daSSharon Dvir 	u8 sta_id;
9029f9af3d7SLiad Kaufman 	int tid = -1;
9039f9af3d7SLiad Kaufman 	unsigned long tid_bitmap;
9049f9af3d7SLiad Kaufman 	unsigned int wdg_timeout;
9059f9af3d7SLiad Kaufman 	int ssn;
9069f9af3d7SLiad Kaufman 	int ret = true;
9079f9af3d7SLiad Kaufman 
908bb49701bSSara Sharon 	/* queue sharing is disabled on new TX path */
909bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
910bb49701bSSara Sharon 		return;
911bb49701bSSara Sharon 
9129f9af3d7SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
9139f9af3d7SLiad Kaufman 
9149f9af3d7SLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
9159f9af3d7SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
9169f9af3d7SLiad Kaufman 
9179f9af3d7SLiad Kaufman 	/* Find TID for queue, and make sure it is the only one on the queue */
9189f9af3d7SLiad Kaufman 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
9199f9af3d7SLiad Kaufman 	if (tid_bitmap != BIT(tid)) {
9209f9af3d7SLiad Kaufman 		IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
9219f9af3d7SLiad Kaufman 			queue, tid_bitmap);
9229f9af3d7SLiad Kaufman 		return;
9239f9af3d7SLiad Kaufman 	}
9249f9af3d7SLiad Kaufman 
9259f9af3d7SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
9269f9af3d7SLiad Kaufman 			    tid);
9279f9af3d7SLiad Kaufman 
9289f9af3d7SLiad Kaufman 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
9299f9af3d7SLiad Kaufman 					lockdep_is_held(&mvm->mutex));
9309f9af3d7SLiad Kaufman 
9319f9af3d7SLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
9329f9af3d7SLiad Kaufman 		return;
9339f9af3d7SLiad Kaufman 
9349f9af3d7SLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
9359f9af3d7SLiad Kaufman 	wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
9369f9af3d7SLiad Kaufman 
9379f9af3d7SLiad Kaufman 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
9389f9af3d7SLiad Kaufman 
939cfbc6c4cSSara Sharon 	ret = iwl_mvm_redirect_queue(mvm, queue, tid,
9409f9af3d7SLiad Kaufman 				     tid_to_mac80211_ac[tid], ssn,
941cfbc6c4cSSara Sharon 				     wdg_timeout, true,
942cfbc6c4cSSara Sharon 				     iwl_mvm_txq_from_tid(sta, tid));
9439f9af3d7SLiad Kaufman 	if (ret) {
9449f9af3d7SLiad Kaufman 		IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
9459f9af3d7SLiad Kaufman 		return;
9469f9af3d7SLiad Kaufman 	}
9479f9af3d7SLiad Kaufman 
9489f9af3d7SLiad Kaufman 	/* If aggs should be turned back on - do it */
9499f9af3d7SLiad Kaufman 	if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
9509cd70e80SEmmanuel Grumbach 		struct iwl_mvm_add_sta_cmd cmd = {0};
9519f9af3d7SLiad Kaufman 
9529f9af3d7SLiad Kaufman 		mvmsta->tid_disable_agg &= ~BIT(tid);
9539f9af3d7SLiad Kaufman 
9549f9af3d7SLiad Kaufman 		cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
9559f9af3d7SLiad Kaufman 		cmd.sta_id = mvmsta->sta_id;
9569f9af3d7SLiad Kaufman 		cmd.add_modify = STA_MODE_MODIFY;
9579f9af3d7SLiad Kaufman 		cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
9589f9af3d7SLiad Kaufman 		cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
9599f9af3d7SLiad Kaufman 		cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
9609f9af3d7SLiad Kaufman 
9619f9af3d7SLiad Kaufman 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
9629f9af3d7SLiad Kaufman 					   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
9639f9af3d7SLiad Kaufman 		if (!ret) {
9649f9af3d7SLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm,
9659f9af3d7SLiad Kaufman 					    "TXQ #%d is now aggregated again\n",
9669f9af3d7SLiad Kaufman 					    queue);
9679f9af3d7SLiad Kaufman 
9689f9af3d7SLiad Kaufman 			/* Mark queue intenally as aggregating again */
9699f9af3d7SLiad Kaufman 			iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
9709f9af3d7SLiad Kaufman 		}
9719f9af3d7SLiad Kaufman 	}
9729f9af3d7SLiad Kaufman 
9739f9af3d7SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
9749f9af3d7SLiad Kaufman }
9759f9af3d7SLiad Kaufman 
97699448a8cSJohannes Berg /*
97799448a8cSJohannes Berg  * Remove inactive TIDs of a given queue.
97899448a8cSJohannes Berg  * If all queue TIDs are inactive - mark the queue as inactive
97999448a8cSJohannes Berg  * If only some the queue TIDs are inactive - unmap them from the queue
980724fe771SJohannes Berg  *
981724fe771SJohannes Berg  * Returns %true if all TIDs were removed and the queue could be reused.
98299448a8cSJohannes Berg  */
983724fe771SJohannes Berg static bool iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
98499448a8cSJohannes Berg 					 struct iwl_mvm_sta *mvmsta, int queue,
98590d2d94cSJohannes Berg 					 unsigned long tid_bitmap,
986b3a87f11SJohannes Berg 					 unsigned long *unshare_queues,
987b3a87f11SJohannes Berg 					 unsigned long *changetid_queues)
98899448a8cSJohannes Berg {
98999448a8cSJohannes Berg 	int tid;
99099448a8cSJohannes Berg 
99199448a8cSJohannes Berg 	lockdep_assert_held(&mvmsta->lock);
992f3f240f9SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
99399448a8cSJohannes Berg 
99499448a8cSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
995724fe771SJohannes Berg 		return false;
99699448a8cSJohannes Berg 
99799448a8cSJohannes Berg 	/* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
99899448a8cSJohannes Berg 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
99999448a8cSJohannes Berg 		/* If some TFDs are still queued - don't mark TID as inactive */
100099448a8cSJohannes Berg 		if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid]))
100199448a8cSJohannes Berg 			tid_bitmap &= ~BIT(tid);
100299448a8cSJohannes Berg 
100399448a8cSJohannes Berg 		/* Don't mark as inactive any TID that has an active BA */
100499448a8cSJohannes Berg 		if (mvmsta->tid_data[tid].state != IWL_AGG_OFF)
100599448a8cSJohannes Berg 			tid_bitmap &= ~BIT(tid);
100699448a8cSJohannes Berg 	}
100799448a8cSJohannes Berg 
1008724fe771SJohannes Berg 	/* If all TIDs in the queue are inactive - return it can be reused */
100999448a8cSJohannes Berg 	if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
1010724fe771SJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Queue %d is inactive\n", queue);
1011724fe771SJohannes Berg 		return true;
101299448a8cSJohannes Berg 	}
101399448a8cSJohannes Berg 
101499448a8cSJohannes Berg 	/*
101599448a8cSJohannes Berg 	 * If we are here, this is a shared queue and not all TIDs timed-out.
101699448a8cSJohannes Berg 	 * Remove the ones that did.
101799448a8cSJohannes Berg 	 */
101899448a8cSJohannes Berg 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1019b3a87f11SJohannes Berg 		u16 tid_bitmap;
102099448a8cSJohannes Berg 
102199448a8cSJohannes Berg 		mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
102299448a8cSJohannes Berg 		mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
102399448a8cSJohannes Berg 
1024b3a87f11SJohannes Berg 		tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1025b3a87f11SJohannes Berg 
1026b3a87f11SJohannes Berg 		/*
1027b3a87f11SJohannes Berg 		 * We need to take into account a situation in which a TXQ was
1028b3a87f11SJohannes Berg 		 * allocated to TID x, and then turned shared by adding TIDs y
1029b3a87f11SJohannes Berg 		 * and z. If TID x becomes inactive and is removed from the TXQ,
1030b3a87f11SJohannes Berg 		 * ownership must be given to one of the remaining TIDs.
1031b3a87f11SJohannes Berg 		 * This is mainly because if TID x continues - a new queue can't
1032b3a87f11SJohannes Berg 		 * be allocated for it as long as it is an owner of another TXQ.
1033b3a87f11SJohannes Berg 		 *
1034b3a87f11SJohannes Berg 		 * Mark this queue in the right bitmap, we'll send the command
1035b3a87f11SJohannes Berg 		 * to the firmware later.
1036b3a87f11SJohannes Berg 		 */
1037b3a87f11SJohannes Berg 		if (!(tid_bitmap & BIT(mvm->queue_info[queue].txq_tid)))
1038b3a87f11SJohannes Berg 			set_bit(queue, changetid_queues);
1039b3a87f11SJohannes Berg 
104099448a8cSJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm,
104199448a8cSJohannes Berg 				    "Removing inactive TID %d from shared Q:%d\n",
104299448a8cSJohannes Berg 				    tid, queue);
104399448a8cSJohannes Berg 	}
104499448a8cSJohannes Berg 
104599448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
104699448a8cSJohannes Berg 			    "TXQ #%d left with tid bitmap 0x%x\n", queue,
104799448a8cSJohannes Berg 			    mvm->queue_info[queue].tid_bitmap);
104899448a8cSJohannes Berg 
104999448a8cSJohannes Berg 	/*
105099448a8cSJohannes Berg 	 * There may be different TIDs with the same mac queues, so make
105199448a8cSJohannes Berg 	 * sure all TIDs have existing corresponding mac queues enabled
105299448a8cSJohannes Berg 	 */
105399448a8cSJohannes Berg 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
105499448a8cSJohannes Berg 
105599448a8cSJohannes Berg 	/* If the queue is marked as shared - "unshare" it */
10561c14089eSJohannes Berg 	if (hweight16(mvm->queue_info[queue].tid_bitmap) == 1 &&
105799448a8cSJohannes Berg 	    mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
105899448a8cSJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
105999448a8cSJohannes Berg 				    queue);
106090d2d94cSJohannes Berg 		set_bit(queue, unshare_queues);
106199448a8cSJohannes Berg 	}
1062724fe771SJohannes Berg 
1063724fe771SJohannes Berg 	return false;
106499448a8cSJohannes Berg }
106599448a8cSJohannes Berg 
1066724fe771SJohannes Berg /*
1067724fe771SJohannes Berg  * Check for inactivity - this includes checking if any queue
1068724fe771SJohannes Berg  * can be unshared and finding one (and only one) that can be
1069724fe771SJohannes Berg  * reused.
1070724fe771SJohannes Berg  * This function is also invoked as a sort of clean-up task,
1071724fe771SJohannes Berg  * in which case @alloc_for_sta is IWL_MVM_INVALID_STA.
1072724fe771SJohannes Berg  *
1073724fe771SJohannes Berg  * Returns the queue number, or -ENOSPC.
1074724fe771SJohannes Berg  */
1075724fe771SJohannes Berg static int iwl_mvm_inactivity_check(struct iwl_mvm *mvm, u8 alloc_for_sta)
107699448a8cSJohannes Berg {
107799448a8cSJohannes Berg 	unsigned long now = jiffies;
107890d2d94cSJohannes Berg 	unsigned long unshare_queues = 0;
1079b3a87f11SJohannes Berg 	unsigned long changetid_queues = 0;
1080724fe771SJohannes Berg 	int i, ret, free_queue = -ENOSPC;
1081cfbc6c4cSSara Sharon 	struct ieee80211_sta *queue_owner  = NULL;
108299448a8cSJohannes Berg 
1083df2a2245SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
1084df2a2245SJohannes Berg 
108599448a8cSJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm))
1086724fe771SJohannes Berg 		return -ENOSPC;
108799448a8cSJohannes Berg 
108899448a8cSJohannes Berg 	rcu_read_lock();
108999448a8cSJohannes Berg 
1090459ab045SJohannes Berg 	/* we skip the CMD queue below by starting at 1 */
1091459ab045SJohannes Berg 	BUILD_BUG_ON(IWL_MVM_DQA_CMD_QUEUE != 0);
1092459ab045SJohannes Berg 
1093459ab045SJohannes Berg 	for (i = 1; i < IWL_MAX_HW_QUEUES; i++) {
109499448a8cSJohannes Berg 		struct ieee80211_sta *sta;
109599448a8cSJohannes Berg 		struct iwl_mvm_sta *mvmsta;
109699448a8cSJohannes Berg 		u8 sta_id;
109799448a8cSJohannes Berg 		int tid;
109899448a8cSJohannes Berg 		unsigned long inactive_tid_bitmap = 0;
109999448a8cSJohannes Berg 		unsigned long queue_tid_bitmap;
110099448a8cSJohannes Berg 
110199448a8cSJohannes Berg 		queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
1102459ab045SJohannes Berg 		if (!queue_tid_bitmap)
1103459ab045SJohannes Berg 			continue;
110499448a8cSJohannes Berg 
110599448a8cSJohannes Berg 		/* If TXQ isn't in active use anyway - nothing to do here... */
110699448a8cSJohannes Berg 		if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
1107459ab045SJohannes Berg 		    mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED)
110899448a8cSJohannes Berg 			continue;
110999448a8cSJohannes Berg 
111099448a8cSJohannes Berg 		/* Check to see if there are inactive TIDs on this queue */
111199448a8cSJohannes Berg 		for_each_set_bit(tid, &queue_tid_bitmap,
111299448a8cSJohannes Berg 				 IWL_MAX_TID_COUNT + 1) {
111399448a8cSJohannes Berg 			if (time_after(mvm->queue_info[i].last_frame_time[tid] +
111499448a8cSJohannes Berg 				       IWL_MVM_DQA_QUEUE_TIMEOUT, now))
111599448a8cSJohannes Berg 				continue;
111699448a8cSJohannes Berg 
111799448a8cSJohannes Berg 			inactive_tid_bitmap |= BIT(tid);
111899448a8cSJohannes Berg 		}
111999448a8cSJohannes Berg 
112099448a8cSJohannes Berg 		/* If all TIDs are active - finish check on this queue */
112199448a8cSJohannes Berg 		if (!inactive_tid_bitmap)
112299448a8cSJohannes Berg 			continue;
112399448a8cSJohannes Berg 
112499448a8cSJohannes Berg 		/*
112599448a8cSJohannes Berg 		 * If we are here - the queue hadn't been served recently and is
112699448a8cSJohannes Berg 		 * in use
112799448a8cSJohannes Berg 		 */
112899448a8cSJohannes Berg 
112999448a8cSJohannes Berg 		sta_id = mvm->queue_info[i].ra_sta_id;
113099448a8cSJohannes Berg 		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
113199448a8cSJohannes Berg 
113299448a8cSJohannes Berg 		/*
113399448a8cSJohannes Berg 		 * If the STA doesn't exist anymore, it isn't an error. It could
113499448a8cSJohannes Berg 		 * be that it was removed since getting the queues, and in this
113599448a8cSJohannes Berg 		 * case it should've inactivated its queues anyway.
113699448a8cSJohannes Berg 		 */
113799448a8cSJohannes Berg 		if (IS_ERR_OR_NULL(sta))
113899448a8cSJohannes Berg 			continue;
113999448a8cSJohannes Berg 
114099448a8cSJohannes Berg 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
114199448a8cSJohannes Berg 
1142f3f240f9SJohannes Berg 		spin_lock_bh(&mvmsta->lock);
1143724fe771SJohannes Berg 		ret = iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
114490d2d94cSJohannes Berg 						   inactive_tid_bitmap,
1145b3a87f11SJohannes Berg 						   &unshare_queues,
1146b3a87f11SJohannes Berg 						   &changetid_queues);
1147e6d419f9SJohannes Berg 		if (ret && free_queue < 0) {
1148cfbc6c4cSSara Sharon 			queue_owner = sta;
1149e6d419f9SJohannes Berg 			free_queue = i;
1150cfbc6c4cSSara Sharon 		}
1151459ab045SJohannes Berg 		/* only unlock sta lock - we still need the queue info lock */
1152f3f240f9SJohannes Berg 		spin_unlock_bh(&mvmsta->lock);
115399448a8cSJohannes Berg 	}
115499448a8cSJohannes Berg 
1155df2a2245SJohannes Berg 
1156df2a2245SJohannes Berg 	/* Reconfigure queues requiring reconfiguation */
115790d2d94cSJohannes Berg 	for_each_set_bit(i, &unshare_queues, IWL_MAX_HW_QUEUES)
115890d2d94cSJohannes Berg 		iwl_mvm_unshare_queue(mvm, i);
1159b3a87f11SJohannes Berg 	for_each_set_bit(i, &changetid_queues, IWL_MAX_HW_QUEUES)
1160b3a87f11SJohannes Berg 		iwl_mvm_change_queue_tid(mvm, i);
1161724fe771SJohannes Berg 
1162fbb1461aSJohannes Berg 	rcu_read_unlock();
1163fbb1461aSJohannes Berg 
1164724fe771SJohannes Berg 	if (free_queue >= 0 && alloc_for_sta != IWL_MVM_INVALID_STA) {
1165cfbc6c4cSSara Sharon 		ret = iwl_mvm_free_inactive_queue(mvm, free_queue, queue_owner,
1166724fe771SJohannes Berg 						  alloc_for_sta);
1167fbb1461aSJohannes Berg 		if (ret)
1168724fe771SJohannes Berg 			return ret;
1169724fe771SJohannes Berg 	}
1170724fe771SJohannes Berg 
1171724fe771SJohannes Berg 	return free_queue;
117299448a8cSJohannes Berg }
117399448a8cSJohannes Berg 
1174c20e08b0SJohannes Berg static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
1175cfbc6c4cSSara Sharon 				   struct ieee80211_sta *sta, u8 ac, int tid)
1176c20e08b0SJohannes Berg {
1177c20e08b0SJohannes Berg 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1178c20e08b0SJohannes Berg 	struct iwl_trans_txq_scd_cfg cfg = {
1179c20e08b0SJohannes Berg 		.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
1180c20e08b0SJohannes Berg 		.sta_id = mvmsta->sta_id,
1181c20e08b0SJohannes Berg 		.tid = tid,
1182c20e08b0SJohannes Berg 		.frame_limit = IWL_FRAME_LIMIT,
1183c20e08b0SJohannes Berg 	};
1184c20e08b0SJohannes Berg 	unsigned int wdg_timeout =
1185c20e08b0SJohannes Berg 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1186c20e08b0SJohannes Berg 	int queue = -1;
1187c20e08b0SJohannes Berg 	unsigned long disable_agg_tids = 0;
1188c20e08b0SJohannes Berg 	enum iwl_mvm_agg_state queue_state;
1189c20e08b0SJohannes Berg 	bool shared_queue = false, inc_ssn;
1190c20e08b0SJohannes Berg 	int ssn;
1191c20e08b0SJohannes Berg 	unsigned long tfd_queue_mask;
1192c20e08b0SJohannes Berg 	int ret;
1193c20e08b0SJohannes Berg 
1194c20e08b0SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
1195c20e08b0SJohannes Berg 
1196c20e08b0SJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm))
1197c20e08b0SJohannes Berg 		return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
1198c20e08b0SJohannes Berg 
1199c20e08b0SJohannes Berg 	spin_lock_bh(&mvmsta->lock);
1200c20e08b0SJohannes Berg 	tfd_queue_mask = mvmsta->tfd_queue_msk;
120135739348SSara Sharon 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1202c20e08b0SJohannes Berg 	spin_unlock_bh(&mvmsta->lock);
1203c20e08b0SJohannes Berg 
1204cfbc6c4cSSara Sharon 	if (tid == IWL_MAX_TID_COUNT) {
1205c20e08b0SJohannes Berg 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1206c20e08b0SJohannes Berg 						IWL_MVM_DQA_MIN_MGMT_QUEUE,
1207c20e08b0SJohannes Berg 						IWL_MVM_DQA_MAX_MGMT_QUEUE);
1208c20e08b0SJohannes Berg 		if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
1209c20e08b0SJohannes Berg 			IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
1210c20e08b0SJohannes Berg 					    queue);
1211c20e08b0SJohannes Berg 
1212c20e08b0SJohannes Berg 		/* If no such queue is found, we'll use a DATA queue instead */
1213c20e08b0SJohannes Berg 	}
1214c20e08b0SJohannes Berg 
1215c20e08b0SJohannes Berg 	if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
1216c20e08b0SJohannes Berg 	    (mvm->queue_info[mvmsta->reserved_queue].status ==
1217724fe771SJohannes Berg 			IWL_MVM_QUEUE_RESERVED)) {
1218c20e08b0SJohannes Berg 		queue = mvmsta->reserved_queue;
1219c20e08b0SJohannes Berg 		mvm->queue_info[queue].reserved = true;
1220c20e08b0SJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
1221c20e08b0SJohannes Berg 	}
1222c20e08b0SJohannes Berg 
1223c20e08b0SJohannes Berg 	if (queue < 0)
1224c20e08b0SJohannes Berg 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1225c20e08b0SJohannes Berg 						IWL_MVM_DQA_MIN_DATA_QUEUE,
1226c20e08b0SJohannes Berg 						IWL_MVM_DQA_MAX_DATA_QUEUE);
1227724fe771SJohannes Berg 	if (queue < 0) {
1228724fe771SJohannes Berg 		/* try harder - perhaps kill an inactive queue */
1229724fe771SJohannes Berg 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1230c20e08b0SJohannes Berg 	}
1231c20e08b0SJohannes Berg 
1232c20e08b0SJohannes Berg 	/* No free queue - we'll have to share */
1233c20e08b0SJohannes Berg 	if (queue <= 0) {
1234c20e08b0SJohannes Berg 		queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
1235c20e08b0SJohannes Berg 		if (queue > 0) {
1236c20e08b0SJohannes Berg 			shared_queue = true;
1237c20e08b0SJohannes Berg 			mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
1238c20e08b0SJohannes Berg 		}
1239c20e08b0SJohannes Berg 	}
1240c20e08b0SJohannes Berg 
1241c20e08b0SJohannes Berg 	/*
1242c20e08b0SJohannes Berg 	 * Mark TXQ as ready, even though it hasn't been fully configured yet,
1243c20e08b0SJohannes Berg 	 * to make sure no one else takes it.
1244c20e08b0SJohannes Berg 	 * This will allow avoiding re-acquiring the lock at the end of the
1245c20e08b0SJohannes Berg 	 * configuration. On error we'll mark it back as free.
1246c20e08b0SJohannes Berg 	 */
1247c20e08b0SJohannes Berg 	if (queue > 0 && !shared_queue)
1248c20e08b0SJohannes Berg 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1249c20e08b0SJohannes Berg 
1250c20e08b0SJohannes Berg 	/* This shouldn't happen - out of queues */
1251c20e08b0SJohannes Berg 	if (WARN_ON(queue <= 0)) {
1252c20e08b0SJohannes Berg 		IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
1253c20e08b0SJohannes Berg 			tid, cfg.sta_id);
1254c20e08b0SJohannes Berg 		return queue;
1255c20e08b0SJohannes Berg 	}
1256c20e08b0SJohannes Berg 
1257c20e08b0SJohannes Berg 	/*
1258c20e08b0SJohannes Berg 	 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
1259c20e08b0SJohannes Berg 	 * but for configuring the SCD to send A-MPDUs we need to mark the queue
1260c20e08b0SJohannes Berg 	 * as aggregatable.
1261c20e08b0SJohannes Berg 	 * Mark all DATA queues as allowing to be aggregated at some point
1262c20e08b0SJohannes Berg 	 */
1263c20e08b0SJohannes Berg 	cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1264c20e08b0SJohannes Berg 			 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1265c20e08b0SJohannes Berg 
1266c20e08b0SJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
1267c20e08b0SJohannes Berg 			    "Allocating %squeue #%d to sta %d on tid %d\n",
1268c20e08b0SJohannes Berg 			    shared_queue ? "shared " : "", queue,
1269c20e08b0SJohannes Berg 			    mvmsta->sta_id, tid);
1270c20e08b0SJohannes Berg 
1271c20e08b0SJohannes Berg 	if (shared_queue) {
1272c20e08b0SJohannes Berg 		/* Disable any open aggs on this queue */
1273c20e08b0SJohannes Berg 		disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
1274c20e08b0SJohannes Berg 
1275c20e08b0SJohannes Berg 		if (disable_agg_tids) {
1276c20e08b0SJohannes Berg 			IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
1277c20e08b0SJohannes Berg 					    queue);
1278c20e08b0SJohannes Berg 			iwl_mvm_invalidate_sta_queue(mvm, queue,
1279c20e08b0SJohannes Berg 						     disable_agg_tids, false);
1280c20e08b0SJohannes Berg 		}
1281c20e08b0SJohannes Berg 	}
1282c20e08b0SJohannes Berg 
1283cfbc6c4cSSara Sharon 	inc_ssn = iwl_mvm_enable_txq(mvm, sta, queue, ssn, &cfg, wdg_timeout);
1284c20e08b0SJohannes Berg 
1285c20e08b0SJohannes Berg 	/*
1286c20e08b0SJohannes Berg 	 * Mark queue as shared in transport if shared
1287c20e08b0SJohannes Berg 	 * Note this has to be done after queue enablement because enablement
1288c20e08b0SJohannes Berg 	 * can also set this value, and there is no indication there to shared
1289c20e08b0SJohannes Berg 	 * queues
1290c20e08b0SJohannes Berg 	 */
1291c20e08b0SJohannes Berg 	if (shared_queue)
1292c20e08b0SJohannes Berg 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
1293c20e08b0SJohannes Berg 
1294c20e08b0SJohannes Berg 	spin_lock_bh(&mvmsta->lock);
1295c20e08b0SJohannes Berg 	/*
1296c20e08b0SJohannes Berg 	 * This looks racy, but it is not. We have only one packet for
1297c20e08b0SJohannes Berg 	 * this ra/tid in our Tx path since we stop the Qdisc when we
1298c20e08b0SJohannes Berg 	 * need to allocate a new TFD queue.
1299c20e08b0SJohannes Berg 	 */
130035739348SSara Sharon 	if (inc_ssn) {
1301c20e08b0SJohannes Berg 		mvmsta->tid_data[tid].seq_number += 0x10;
130235739348SSara Sharon 		ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
130335739348SSara Sharon 	}
1304c20e08b0SJohannes Berg 	mvmsta->tid_data[tid].txq_id = queue;
1305c20e08b0SJohannes Berg 	mvmsta->tfd_queue_msk |= BIT(queue);
1306c20e08b0SJohannes Berg 	queue_state = mvmsta->tid_data[tid].state;
1307c20e08b0SJohannes Berg 
1308c20e08b0SJohannes Berg 	if (mvmsta->reserved_queue == queue)
1309c20e08b0SJohannes Berg 		mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
1310c20e08b0SJohannes Berg 	spin_unlock_bh(&mvmsta->lock);
1311c20e08b0SJohannes Berg 
1312c20e08b0SJohannes Berg 	if (!shared_queue) {
1313c20e08b0SJohannes Berg 		ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
1314c20e08b0SJohannes Berg 		if (ret)
1315c20e08b0SJohannes Berg 			goto out_err;
1316c20e08b0SJohannes Berg 
1317c20e08b0SJohannes Berg 		/* If we need to re-enable aggregations... */
1318c20e08b0SJohannes Berg 		if (queue_state == IWL_AGG_ON) {
1319c20e08b0SJohannes Berg 			ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1320c20e08b0SJohannes Berg 			if (ret)
1321c20e08b0SJohannes Berg 				goto out_err;
1322c20e08b0SJohannes Berg 		}
1323c20e08b0SJohannes Berg 	} else {
1324c20e08b0SJohannes Berg 		/* Redirect queue, if needed */
1325cfbc6c4cSSara Sharon 		ret = iwl_mvm_redirect_queue(mvm, queue, tid, ac, ssn,
1326cfbc6c4cSSara Sharon 					     wdg_timeout, false,
1327cfbc6c4cSSara Sharon 					     iwl_mvm_txq_from_tid(sta, tid));
1328c20e08b0SJohannes Berg 		if (ret)
1329c20e08b0SJohannes Berg 			goto out_err;
1330c20e08b0SJohannes Berg 	}
1331c20e08b0SJohannes Berg 
1332c20e08b0SJohannes Berg 	return 0;
1333c20e08b0SJohannes Berg 
1334c20e08b0SJohannes Berg out_err:
1335cfbc6c4cSSara Sharon 	iwl_mvm_disable_txq(mvm, sta, queue, tid, 0);
1336c20e08b0SJohannes Berg 
1337c20e08b0SJohannes Berg 	return ret;
1338c20e08b0SJohannes Berg }
1339c20e08b0SJohannes Berg 
134024afba76SLiad Kaufman void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
134124afba76SLiad Kaufman {
134224afba76SLiad Kaufman 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
134324afba76SLiad Kaufman 					   add_stream_wk);
134424afba76SLiad Kaufman 
134524afba76SLiad Kaufman 	mutex_lock(&mvm->mutex);
134624afba76SLiad Kaufman 
1347724fe771SJohannes Berg 	iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
13489f9af3d7SLiad Kaufman 
1349cfbc6c4cSSara Sharon 	while (!list_empty(&mvm->add_stream_txqs)) {
1350cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq;
1351cfbc6c4cSSara Sharon 		struct ieee80211_txq *txq;
1352cfbc6c4cSSara Sharon 		u8 tid;
135324afba76SLiad Kaufman 
1354cfbc6c4cSSara Sharon 		mvmtxq = list_first_entry(&mvm->add_stream_txqs,
1355cfbc6c4cSSara Sharon 					  struct iwl_mvm_txq, list);
135624afba76SLiad Kaufman 
1357cfbc6c4cSSara Sharon 		txq = container_of((void *)mvmtxq, struct ieee80211_txq,
1358cfbc6c4cSSara Sharon 				   drv_priv);
1359cfbc6c4cSSara Sharon 		tid = txq->tid;
1360cfbc6c4cSSara Sharon 		if (tid == IEEE80211_NUM_TIDS)
1361cfbc6c4cSSara Sharon 			tid = IWL_MAX_TID_COUNT;
1362cfbc6c4cSSara Sharon 
13633d1d87abSJohannes Berg 		/*
13643d1d87abSJohannes Berg 		 * We can't really do much here, but if this fails we can't
13653d1d87abSJohannes Berg 		 * transmit anyway - so just don't transmit the frame etc.
13663d1d87abSJohannes Berg 		 * and let them back up ... we've tried our best to allocate
13673d1d87abSJohannes Berg 		 * a queue in the function itself.
13683d1d87abSJohannes Berg 		 */
13693d1d87abSJohannes Berg 		if (iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, tid)) {
13703d1d87abSJohannes Berg 			list_del_init(&mvmtxq->list);
13713d1d87abSJohannes Berg 			continue;
13723d1d87abSJohannes Berg 		}
13733d1d87abSJohannes Berg 
1374cfbc6c4cSSara Sharon 		list_del_init(&mvmtxq->list);
1375f5ae2f93SJohannes Berg 		local_bh_disable();
1376cfbc6c4cSSara Sharon 		iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1377f5ae2f93SJohannes Berg 		local_bh_enable();
137824afba76SLiad Kaufman 	}
137924afba76SLiad Kaufman 
138024afba76SLiad Kaufman 	mutex_unlock(&mvm->mutex);
138124afba76SLiad Kaufman }
138224afba76SLiad Kaufman 
138324afba76SLiad Kaufman static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1384d5216a28SLiad Kaufman 				      struct ieee80211_sta *sta,
1385d5216a28SLiad Kaufman 				      enum nl80211_iftype vif_type)
138624afba76SLiad Kaufman {
138724afba76SLiad Kaufman 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
138824afba76SLiad Kaufman 	int queue;
138924afba76SLiad Kaufman 
1390396952eeSSara Sharon 	/* queue reserving is disabled on new TX path */
1391396952eeSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1392396952eeSSara Sharon 		return 0;
1393396952eeSSara Sharon 
1394724fe771SJohannes Berg 	/* run the general cleanup/unsharing of queues */
1395724fe771SJohannes Berg 	iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
13969794c64fSLiad Kaufman 
139724afba76SLiad Kaufman 	/* Make sure we have free resources for this STA */
1398d5216a28SLiad Kaufman 	if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
13991c14089eSJohannes Berg 	    !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].tid_bitmap &&
1400cf961e16SLiad Kaufman 	    (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1401cf961e16SLiad Kaufman 	     IWL_MVM_QUEUE_FREE))
1402d5216a28SLiad Kaufman 		queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1403d5216a28SLiad Kaufman 	else
14049794c64fSLiad Kaufman 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
14059794c64fSLiad Kaufman 						IWL_MVM_DQA_MIN_DATA_QUEUE,
140624afba76SLiad Kaufman 						IWL_MVM_DQA_MAX_DATA_QUEUE);
140724afba76SLiad Kaufman 	if (queue < 0) {
1408724fe771SJohannes Berg 		/* try again - this time kick out a queue if needed */
1409724fe771SJohannes Berg 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1410724fe771SJohannes Berg 		if (queue < 0) {
141124afba76SLiad Kaufman 			IWL_ERR(mvm, "No available queues for new station\n");
141224afba76SLiad Kaufman 			return -ENOSPC;
1413724fe771SJohannes Berg 		}
141424afba76SLiad Kaufman 	}
1415cf961e16SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
141624afba76SLiad Kaufman 
141724afba76SLiad Kaufman 	mvmsta->reserved_queue = queue;
141824afba76SLiad Kaufman 
141924afba76SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
142024afba76SLiad Kaufman 			    queue, mvmsta->sta_id);
142124afba76SLiad Kaufman 
142224afba76SLiad Kaufman 	return 0;
142324afba76SLiad Kaufman }
142424afba76SLiad Kaufman 
14258d98ae6eSLiad Kaufman /*
14268d98ae6eSLiad Kaufman  * In DQA mode, after a HW restart the queues should be allocated as before, in
14278d98ae6eSLiad Kaufman  * order to avoid race conditions when there are shared queues. This function
14288d98ae6eSLiad Kaufman  * does the re-mapping and queue allocation.
14298d98ae6eSLiad Kaufman  *
14308d98ae6eSLiad Kaufman  * Note that re-enabling aggregations isn't done in this function.
14318d98ae6eSLiad Kaufman  */
14328d98ae6eSLiad Kaufman static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1433cfbc6c4cSSara Sharon 						 struct ieee80211_sta *sta)
14348d98ae6eSLiad Kaufman {
1435cfbc6c4cSSara Sharon 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1436cfbc6c4cSSara Sharon 	unsigned int wdg =
14378d98ae6eSLiad Kaufman 		iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
14388d98ae6eSLiad Kaufman 	int i;
14398d98ae6eSLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
14408d98ae6eSLiad Kaufman 		.sta_id = mvm_sta->sta_id,
14418d98ae6eSLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
14428d98ae6eSLiad Kaufman 	};
14438d98ae6eSLiad Kaufman 
144403c902bfSJohannes Berg 	/* Make sure reserved queue is still marked as such (if allocated) */
144503c902bfSJohannes Berg 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
14468d98ae6eSLiad Kaufman 		mvm->queue_info[mvm_sta->reserved_queue].status =
14478d98ae6eSLiad Kaufman 			IWL_MVM_QUEUE_RESERVED;
14488d98ae6eSLiad Kaufman 
14498d98ae6eSLiad Kaufman 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
14508d98ae6eSLiad Kaufman 		struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
14518d98ae6eSLiad Kaufman 		int txq_id = tid_data->txq_id;
14528d98ae6eSLiad Kaufman 		int ac;
14538d98ae6eSLiad Kaufman 
14546862fceeSSara Sharon 		if (txq_id == IWL_MVM_INVALID_QUEUE)
14558d98ae6eSLiad Kaufman 			continue;
14568d98ae6eSLiad Kaufman 
14578d98ae6eSLiad Kaufman 		ac = tid_to_mac80211_ac[i];
14588d98ae6eSLiad Kaufman 
1459310181ecSSara Sharon 		if (iwl_mvm_has_new_tx_api(mvm)) {
1460310181ecSSara Sharon 			IWL_DEBUG_TX_QUEUES(mvm,
1461310181ecSSara Sharon 					    "Re-mapping sta %d tid %d\n",
1462310181ecSSara Sharon 					    mvm_sta->sta_id, i);
1463cfbc6c4cSSara Sharon 			txq_id = iwl_mvm_tvqm_enable_txq(mvm, mvm_sta->sta_id,
1464cfbc6c4cSSara Sharon 							 i, wdg);
146591cf5dedSJohannes Berg 			/*
146691cf5dedSJohannes Berg 			 * on failures, just set it to IWL_MVM_INVALID_QUEUE
146791cf5dedSJohannes Berg 			 * to try again later, we have no other good way of
146891cf5dedSJohannes Berg 			 * failing here
146991cf5dedSJohannes Berg 			 */
147091cf5dedSJohannes Berg 			if (txq_id < 0)
147191cf5dedSJohannes Berg 				txq_id = IWL_MVM_INVALID_QUEUE;
1472310181ecSSara Sharon 			tid_data->txq_id = txq_id;
14735d39051aSLiad Kaufman 
14745d39051aSLiad Kaufman 			/*
14755d39051aSLiad Kaufman 			 * Since we don't set the seq number after reset, and HW
14765d39051aSLiad Kaufman 			 * sets it now, FW reset will cause the seq num to start
14775d39051aSLiad Kaufman 			 * at 0 again, so driver will need to update it
14785d39051aSLiad Kaufman 			 * internally as well, so it keeps in sync with real val
14795d39051aSLiad Kaufman 			 */
14805d39051aSLiad Kaufman 			tid_data->seq_number = 0;
1481310181ecSSara Sharon 		} else {
1482310181ecSSara Sharon 			u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1483310181ecSSara Sharon 
14848d98ae6eSLiad Kaufman 			cfg.tid = i;
1485cf6c6ea3SEmmanuel Grumbach 			cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
14868d98ae6eSLiad Kaufman 			cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1487310181ecSSara Sharon 					 txq_id ==
1488310181ecSSara Sharon 					 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
14898d98ae6eSLiad Kaufman 
14908d98ae6eSLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm,
14918d98ae6eSLiad Kaufman 					    "Re-mapping sta %d tid %d to queue %d\n",
14928d98ae6eSLiad Kaufman 					    mvm_sta->sta_id, i, txq_id);
14938d98ae6eSLiad Kaufman 
1494cfbc6c4cSSara Sharon 			iwl_mvm_enable_txq(mvm, sta, txq_id, seq, &cfg, wdg);
14958d98ae6eSLiad Kaufman 			mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
14968d98ae6eSLiad Kaufman 		}
149734e10860SSara Sharon 	}
14988d98ae6eSLiad Kaufman }
14998d98ae6eSLiad Kaufman 
1500732d06e9SShaul Triebitz static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1501732d06e9SShaul Triebitz 				      struct iwl_mvm_int_sta *sta,
1502732d06e9SShaul Triebitz 				      const u8 *addr,
1503732d06e9SShaul Triebitz 				      u16 mac_id, u16 color)
1504732d06e9SShaul Triebitz {
1505732d06e9SShaul Triebitz 	struct iwl_mvm_add_sta_cmd cmd;
1506732d06e9SShaul Triebitz 	int ret;
15073f497de9SLuca Coelho 	u32 status = ADD_STA_SUCCESS;
1508732d06e9SShaul Triebitz 
1509732d06e9SShaul Triebitz 	lockdep_assert_held(&mvm->mutex);
1510732d06e9SShaul Triebitz 
1511732d06e9SShaul Triebitz 	memset(&cmd, 0, sizeof(cmd));
1512732d06e9SShaul Triebitz 	cmd.sta_id = sta->sta_id;
15132c2c3647SNathan Errera 
15142c2c3647SNathan Errera 	if (iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP, ADD_STA,
15152c2c3647SNathan Errera 				  0) >= 12 &&
15162c2c3647SNathan Errera 	    sta->type == IWL_STA_AUX_ACTIVITY)
15172c2c3647SNathan Errera 		cmd.mac_id_n_color = cpu_to_le32(mac_id);
15182c2c3647SNathan Errera 	else
1519732d06e9SShaul Triebitz 		cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1520732d06e9SShaul Triebitz 								     color));
15212c2c3647SNathan Errera 
1522732d06e9SShaul Triebitz 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1523732d06e9SShaul Triebitz 		cmd.station_type = sta->type;
1524732d06e9SShaul Triebitz 
1525732d06e9SShaul Triebitz 	if (!iwl_mvm_has_new_tx_api(mvm))
1526732d06e9SShaul Triebitz 		cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1527732d06e9SShaul Triebitz 	cmd.tid_disable_tx = cpu_to_le16(0xffff);
1528732d06e9SShaul Triebitz 
1529732d06e9SShaul Triebitz 	if (addr)
1530732d06e9SShaul Triebitz 		memcpy(cmd.addr, addr, ETH_ALEN);
1531732d06e9SShaul Triebitz 
1532732d06e9SShaul Triebitz 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1533732d06e9SShaul Triebitz 					  iwl_mvm_add_sta_cmd_size(mvm),
1534732d06e9SShaul Triebitz 					  &cmd, &status);
1535732d06e9SShaul Triebitz 	if (ret)
1536732d06e9SShaul Triebitz 		return ret;
1537732d06e9SShaul Triebitz 
1538732d06e9SShaul Triebitz 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1539732d06e9SShaul Triebitz 	case ADD_STA_SUCCESS:
1540732d06e9SShaul Triebitz 		IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1541732d06e9SShaul Triebitz 		return 0;
1542732d06e9SShaul Triebitz 	default:
1543732d06e9SShaul Triebitz 		ret = -EIO;
1544732d06e9SShaul Triebitz 		IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1545732d06e9SShaul Triebitz 			status);
1546732d06e9SShaul Triebitz 		break;
1547732d06e9SShaul Triebitz 	}
1548732d06e9SShaul Triebitz 	return ret;
1549732d06e9SShaul Triebitz }
1550732d06e9SShaul Triebitz 
1551e705c121SKalle Valo int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1552e705c121SKalle Valo 		    struct ieee80211_vif *vif,
1553e705c121SKalle Valo 		    struct ieee80211_sta *sta)
1554e705c121SKalle Valo {
1555e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1556e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1557a571f5f6SSara Sharon 	struct iwl_mvm_rxq_dup_data *dup_data;
1558e705c121SKalle Valo 	int i, ret, sta_id;
1559732d06e9SShaul Triebitz 	bool sta_update = false;
1560732d06e9SShaul Triebitz 	unsigned int sta_flags = 0;
1561e705c121SKalle Valo 
1562e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1563e705c121SKalle Valo 
1564e705c121SKalle Valo 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1565e705c121SKalle Valo 		sta_id = iwl_mvm_find_free_sta_id(mvm,
1566e705c121SKalle Valo 						  ieee80211_vif_type_p2p(vif));
1567e705c121SKalle Valo 	else
1568e705c121SKalle Valo 		sta_id = mvm_sta->sta_id;
1569e705c121SKalle Valo 
15700ae98812SSara Sharon 	if (sta_id == IWL_MVM_INVALID_STA)
1571e705c121SKalle Valo 		return -ENOSPC;
1572e705c121SKalle Valo 
1573e705c121SKalle Valo 	spin_lock_init(&mvm_sta->lock);
1574e705c121SKalle Valo 
1575c8f54701SJohannes Berg 	/* if this is a HW restart re-alloc existing queues */
1576c8f54701SJohannes Berg 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1577732d06e9SShaul Triebitz 		struct iwl_mvm_int_sta tmp_sta = {
1578732d06e9SShaul Triebitz 			.sta_id = sta_id,
1579732d06e9SShaul Triebitz 			.type = mvm_sta->sta_type,
1580732d06e9SShaul Triebitz 		};
1581732d06e9SShaul Triebitz 
1582732d06e9SShaul Triebitz 		/*
1583732d06e9SShaul Triebitz 		 * First add an empty station since allocating
1584732d06e9SShaul Triebitz 		 * a queue requires a valid station
1585732d06e9SShaul Triebitz 		 */
1586732d06e9SShaul Triebitz 		ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1587732d06e9SShaul Triebitz 						 mvmvif->id, mvmvif->color);
1588732d06e9SShaul Triebitz 		if (ret)
1589732d06e9SShaul Triebitz 			goto err;
1590732d06e9SShaul Triebitz 
1591cfbc6c4cSSara Sharon 		iwl_mvm_realloc_queues_after_restart(mvm, sta);
1592732d06e9SShaul Triebitz 		sta_update = true;
1593732d06e9SShaul Triebitz 		sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
15948d98ae6eSLiad Kaufman 		goto update_fw;
15958d98ae6eSLiad Kaufman 	}
15968d98ae6eSLiad Kaufman 
1597e705c121SKalle Valo 	mvm_sta->sta_id = sta_id;
1598e705c121SKalle Valo 	mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1599e705c121SKalle Valo 						      mvmvif->color);
1600e705c121SKalle Valo 	mvm_sta->vif = vif;
1601286ca8ebSLuca Coelho 	if (!mvm->trans->trans_cfg->gen2)
1602e705c121SKalle Valo 		mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1603a58bb468SLiad Kaufman 	else
1604a58bb468SLiad Kaufman 		mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
1605e705c121SKalle Valo 	mvm_sta->tx_protection = 0;
1606e705c121SKalle Valo 	mvm_sta->tt_tx_protection = false;
1607ced19f26SSara Sharon 	mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
1608e705c121SKalle Valo 
1609e705c121SKalle Valo 	/* HW restart, don't assume the memory has been zeroed */
1610e705c121SKalle Valo 	mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1611e705c121SKalle Valo 	mvm_sta->tfd_queue_msk = 0;
1612e705c121SKalle Valo 
1613e705c121SKalle Valo 	/* for HW restart - reset everything but the sequence number */
161424afba76SLiad Kaufman 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1615e705c121SKalle Valo 		u16 seq = mvm_sta->tid_data[i].seq_number;
1616e705c121SKalle Valo 		memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1617e705c121SKalle Valo 		mvm_sta->tid_data[i].seq_number = seq;
161824afba76SLiad Kaufman 
161924afba76SLiad Kaufman 		/*
162024afba76SLiad Kaufman 		 * Mark all queues for this STA as unallocated and defer TX
162124afba76SLiad Kaufman 		 * frames until the queue is allocated
162224afba76SLiad Kaufman 		 */
16236862fceeSSara Sharon 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1624e705c121SKalle Valo 	}
1625cfbc6c4cSSara Sharon 
1626cfbc6c4cSSara Sharon 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1627cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
1628cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1629cfbc6c4cSSara Sharon 
1630cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1631cfbc6c4cSSara Sharon 		INIT_LIST_HEAD(&mvmtxq->list);
1632fba8248eSSara Sharon 		atomic_set(&mvmtxq->tx_request, 0);
1633cfbc6c4cSSara Sharon 	}
1634cfbc6c4cSSara Sharon 
1635e705c121SKalle Valo 	mvm_sta->agg_tids = 0;
1636e705c121SKalle Valo 
1637a571f5f6SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) &&
1638a571f5f6SSara Sharon 	    !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
163992c4dca6SJohannes Berg 		int q;
164092c4dca6SJohannes Berg 
1641a571f5f6SSara Sharon 		dup_data = kcalloc(mvm->trans->num_rx_queues,
164292c4dca6SJohannes Berg 				   sizeof(*dup_data), GFP_KERNEL);
1643a571f5f6SSara Sharon 		if (!dup_data)
1644a571f5f6SSara Sharon 			return -ENOMEM;
164592c4dca6SJohannes Berg 		/*
164692c4dca6SJohannes Berg 		 * Initialize all the last_seq values to 0xffff which can never
164792c4dca6SJohannes Berg 		 * compare equal to the frame's seq_ctrl in the check in
164892c4dca6SJohannes Berg 		 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
164992c4dca6SJohannes Berg 		 * number and fragmented packets don't reach that function.
165092c4dca6SJohannes Berg 		 *
165192c4dca6SJohannes Berg 		 * This thus allows receiving a packet with seqno 0 and the
165292c4dca6SJohannes Berg 		 * retry bit set as the very first packet on a new TID.
165392c4dca6SJohannes Berg 		 */
165492c4dca6SJohannes Berg 		for (q = 0; q < mvm->trans->num_rx_queues; q++)
165592c4dca6SJohannes Berg 			memset(dup_data[q].last_seq, 0xff,
165692c4dca6SJohannes Berg 			       sizeof(dup_data[q].last_seq));
1657a571f5f6SSara Sharon 		mvm_sta->dup_data = dup_data;
1658a571f5f6SSara Sharon 	}
1659a571f5f6SSara Sharon 
1660c8f54701SJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm)) {
1661d5216a28SLiad Kaufman 		ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1662d5216a28SLiad Kaufman 						 ieee80211_vif_type_p2p(vif));
166324afba76SLiad Kaufman 		if (ret)
166424afba76SLiad Kaufman 			goto err;
166524afba76SLiad Kaufman 	}
166624afba76SLiad Kaufman 
16679f66a397SGregory Greenman 	/*
16689f66a397SGregory Greenman 	 * if rs is registered with mac80211, then "add station" will be handled
16699f66a397SGregory Greenman 	 * via the corresponding ops, otherwise need to notify rate scaling here
16709f66a397SGregory Greenman 	 */
16714243edb4SEmmanuel Grumbach 	if (iwl_mvm_has_tlc_offload(mvm))
16729f66a397SGregory Greenman 		iwl_mvm_rs_add_sta(mvm, mvm_sta);
16730f8084cdSMordechay Goodstein 	else
1674f5d88fa3SGregory Greenman 		spin_lock_init(&mvm_sta->lq_sta.rs_drv.pers.lock);
16759f66a397SGregory Greenman 
16760dde2440SAvraham Stern 	iwl_mvm_toggle_tx_ant(mvm, &mvm_sta->tx_ant);
16770dde2440SAvraham Stern 
16788d98ae6eSLiad Kaufman update_fw:
1679732d06e9SShaul Triebitz 	ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
1680e705c121SKalle Valo 	if (ret)
1681e705c121SKalle Valo 		goto err;
1682e705c121SKalle Valo 
1683e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION) {
1684e705c121SKalle Valo 		if (!sta->tdls) {
16850ae98812SSara Sharon 			WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
1686e705c121SKalle Valo 			mvmvif->ap_sta_id = sta_id;
1687e705c121SKalle Valo 		} else {
16880ae98812SSara Sharon 			WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
1689e705c121SKalle Valo 		}
1690e705c121SKalle Valo 	}
1691e705c121SKalle Valo 
1692e705c121SKalle Valo 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1693e705c121SKalle Valo 
1694e705c121SKalle Valo 	return 0;
1695e705c121SKalle Valo 
1696e705c121SKalle Valo err:
1697e705c121SKalle Valo 	return ret;
1698e705c121SKalle Valo }
1699e705c121SKalle Valo 
1700e705c121SKalle Valo int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1701e705c121SKalle Valo 		      bool drain)
1702e705c121SKalle Valo {
1703e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
1704e705c121SKalle Valo 	int ret;
1705e705c121SKalle Valo 	u32 status;
1706e705c121SKalle Valo 
1707e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1708e705c121SKalle Valo 
1709e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1710e705c121SKalle Valo 	cmd.sta_id = mvmsta->sta_id;
1711e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
1712e705c121SKalle Valo 	cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1713e705c121SKalle Valo 	cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1714e705c121SKalle Valo 
1715e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
1716854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1717854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
1718e705c121SKalle Valo 					  &cmd, &status);
1719e705c121SKalle Valo 	if (ret)
1720e705c121SKalle Valo 		return ret;
1721e705c121SKalle Valo 
1722837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1723e705c121SKalle Valo 	case ADD_STA_SUCCESS:
1724e705c121SKalle Valo 		IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1725e705c121SKalle Valo 			       mvmsta->sta_id);
1726e705c121SKalle Valo 		break;
1727e705c121SKalle Valo 	default:
1728e705c121SKalle Valo 		ret = -EIO;
1729e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1730e705c121SKalle Valo 			mvmsta->sta_id);
1731e705c121SKalle Valo 		break;
1732e705c121SKalle Valo 	}
1733e705c121SKalle Valo 
1734e705c121SKalle Valo 	return ret;
1735e705c121SKalle Valo }
1736e705c121SKalle Valo 
1737e705c121SKalle Valo /*
1738e705c121SKalle Valo  * Remove a station from the FW table. Before sending the command to remove
1739e705c121SKalle Valo  * the station validate that the station is indeed known to the driver (sanity
1740e705c121SKalle Valo  * only).
1741e705c121SKalle Valo  */
1742e705c121SKalle Valo static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1743e705c121SKalle Valo {
1744e705c121SKalle Valo 	struct ieee80211_sta *sta;
1745e705c121SKalle Valo 	struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1746e705c121SKalle Valo 		.sta_id = sta_id,
1747e705c121SKalle Valo 	};
1748e705c121SKalle Valo 	int ret;
1749e705c121SKalle Valo 
1750e705c121SKalle Valo 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1751e705c121SKalle Valo 					lockdep_is_held(&mvm->mutex));
1752e705c121SKalle Valo 
1753e705c121SKalle Valo 	/* Note: internal stations are marked as error values */
1754e705c121SKalle Valo 	if (!sta) {
1755e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid station id\n");
1756e705c121SKalle Valo 		return -EINVAL;
1757e705c121SKalle Valo 	}
1758e705c121SKalle Valo 
1759e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1760e705c121SKalle Valo 				   sizeof(rm_sta_cmd), &rm_sta_cmd);
1761e705c121SKalle Valo 	if (ret) {
1762e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1763e705c121SKalle Valo 		return ret;
1764e705c121SKalle Valo 	}
1765e705c121SKalle Valo 
1766e705c121SKalle Valo 	return 0;
1767e705c121SKalle Valo }
1768e705c121SKalle Valo 
176924afba76SLiad Kaufman static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
177024afba76SLiad Kaufman 				       struct ieee80211_vif *vif,
1771cfbc6c4cSSara Sharon 				       struct ieee80211_sta *sta)
177224afba76SLiad Kaufman {
1773cfbc6c4cSSara Sharon 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
177424afba76SLiad Kaufman 	int i;
177524afba76SLiad Kaufman 
177624afba76SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
177724afba76SLiad Kaufman 
177824afba76SLiad Kaufman 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
17796862fceeSSara Sharon 		if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
178024afba76SLiad Kaufman 			continue;
178124afba76SLiad Kaufman 
1782cfbc6c4cSSara Sharon 		iwl_mvm_disable_txq(mvm, sta, mvm_sta->tid_data[i].txq_id, i,
1783cfbc6c4cSSara Sharon 				    0);
17846862fceeSSara Sharon 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
178524afba76SLiad Kaufman 	}
1786cfbc6c4cSSara Sharon 
1787cfbc6c4cSSara Sharon 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1788cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
1789cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1790cfbc6c4cSSara Sharon 
1791cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1792cfbc6c4cSSara Sharon 	}
179324afba76SLiad Kaufman }
179424afba76SLiad Kaufman 
1795d6d517b7SSara Sharon int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1796d6d517b7SSara Sharon 				  struct iwl_mvm_sta *mvm_sta)
1797d6d517b7SSara Sharon {
1798bec9522aSSharon Dvir 	int i;
1799d6d517b7SSara Sharon 
1800d6d517b7SSara Sharon 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1801d6d517b7SSara Sharon 		u16 txq_id;
1802bec9522aSSharon Dvir 		int ret;
1803d6d517b7SSara Sharon 
1804d6d517b7SSara Sharon 		spin_lock_bh(&mvm_sta->lock);
1805d6d517b7SSara Sharon 		txq_id = mvm_sta->tid_data[i].txq_id;
1806d6d517b7SSara Sharon 		spin_unlock_bh(&mvm_sta->lock);
1807d6d517b7SSara Sharon 
1808d6d517b7SSara Sharon 		if (txq_id == IWL_MVM_INVALID_QUEUE)
1809d6d517b7SSara Sharon 			continue;
1810d6d517b7SSara Sharon 
1811d6d517b7SSara Sharon 		ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1812d6d517b7SSara Sharon 		if (ret)
1813bec9522aSSharon Dvir 			return ret;
1814d6d517b7SSara Sharon 	}
1815d6d517b7SSara Sharon 
1816bec9522aSSharon Dvir 	return 0;
1817d6d517b7SSara Sharon }
1818d6d517b7SSara Sharon 
1819e705c121SKalle Valo int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1820e705c121SKalle Valo 		   struct ieee80211_vif *vif,
1821e705c121SKalle Valo 		   struct ieee80211_sta *sta)
1822e705c121SKalle Valo {
1823e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1824e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
182594c3e614SSara Sharon 	u8 sta_id = mvm_sta->sta_id;
1826e705c121SKalle Valo 	int ret;
1827e705c121SKalle Valo 
1828e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1829e705c121SKalle Valo 
1830a571f5f6SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
1831a571f5f6SSara Sharon 		kfree(mvm_sta->dup_data);
1832a571f5f6SSara Sharon 
1833e705c121SKalle Valo 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1834e705c121SKalle Valo 	if (ret)
1835e705c121SKalle Valo 		return ret;
1836c8f54701SJohannes Berg 
1837e705c121SKalle Valo 	/* flush its queues here since we are freeing mvm_sta */
1838f9084775SNathan Errera 	ret = iwl_mvm_flush_sta(mvm, mvm_sta, false);
1839e705c121SKalle Valo 	if (ret)
1840e705c121SKalle Valo 		return ret;
1841d6d517b7SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
1842d6d517b7SSara Sharon 		ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1843d6d517b7SSara Sharon 	} else {
1844d6d517b7SSara Sharon 		u32 q_mask = mvm_sta->tfd_queue_msk;
1845d6d517b7SSara Sharon 
1846a1a57877SSara Sharon 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1847d6d517b7SSara Sharon 						     q_mask);
1848d6d517b7SSara Sharon 	}
1849e705c121SKalle Valo 	if (ret)
1850e705c121SKalle Valo 		return ret;
1851c8f54701SJohannes Berg 
1852e705c121SKalle Valo 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1853e705c121SKalle Valo 
1854cfbc6c4cSSara Sharon 	iwl_mvm_disable_sta_queues(mvm, vif, sta);
185556214749SLiad Kaufman 
185656214749SLiad Kaufman 	/* If there is a TXQ still marked as reserved - free it */
1857c8f54701SJohannes Berg 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1858a0315deaSLiad Kaufman 		u8 reserved_txq = mvm_sta->reserved_queue;
1859a0315deaSLiad Kaufman 		enum iwl_mvm_queue_status *status;
1860a0315deaSLiad Kaufman 
1861a0315deaSLiad Kaufman 		/*
1862a0315deaSLiad Kaufman 		 * If no traffic has gone through the reserved TXQ - it
1863a0315deaSLiad Kaufman 		 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1864a0315deaSLiad Kaufman 		 * should be manually marked as free again
1865a0315deaSLiad Kaufman 		 */
1866a0315deaSLiad Kaufman 		status = &mvm->queue_info[reserved_txq].status;
1867a0315deaSLiad Kaufman 		if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1868a0315deaSLiad Kaufman 			 (*status != IWL_MVM_QUEUE_FREE),
1869a0315deaSLiad Kaufman 			 "sta_id %d reserved txq %d status %d",
1870f3f240f9SJohannes Berg 			 sta_id, reserved_txq, *status))
1871a0315deaSLiad Kaufman 			return -EINVAL;
1872a0315deaSLiad Kaufman 
1873a0315deaSLiad Kaufman 		*status = IWL_MVM_QUEUE_FREE;
1874a0315deaSLiad Kaufman 	}
1875a0315deaSLiad Kaufman 
1876e3118ad7SLiad Kaufman 	if (vif->type == NL80211_IFTYPE_STATION &&
187794c3e614SSara Sharon 	    mvmvif->ap_sta_id == sta_id) {
1878e3118ad7SLiad Kaufman 		/* if associated - we can't remove the AP STA now */
1879e705c121SKalle Valo 		if (vif->bss_conf.assoc)
1880e705c121SKalle Valo 			return ret;
1881e705c121SKalle Valo 
1882e705c121SKalle Valo 		/* unassoc - go ahead - remove the AP STA now */
18830ae98812SSara Sharon 		mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1884e705c121SKalle Valo 	}
1885e705c121SKalle Valo 
1886e705c121SKalle Valo 	/*
1887e705c121SKalle Valo 	 * This shouldn't happen - the TDLS channel switch should be canceled
1888e705c121SKalle Valo 	 * before the STA is removed.
1889e705c121SKalle Valo 	 */
189094c3e614SSara Sharon 	if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
18910ae98812SSara Sharon 		mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1892e705c121SKalle Valo 		cancel_delayed_work(&mvm->tdls_cs.dwork);
1893e705c121SKalle Valo 	}
1894e705c121SKalle Valo 
1895e705c121SKalle Valo 	/*
1896e705c121SKalle Valo 	 * Make sure that the tx response code sees the station as -EBUSY and
1897e705c121SKalle Valo 	 * calls the drain worker.
1898e705c121SKalle Valo 	 */
1899e705c121SKalle Valo 	spin_lock_bh(&mvm_sta->lock);
1900e705c121SKalle Valo 	spin_unlock_bh(&mvm_sta->lock);
1901e705c121SKalle Valo 
1902e705c121SKalle Valo 	ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1903e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1904e705c121SKalle Valo 
1905e705c121SKalle Valo 	return ret;
1906e705c121SKalle Valo }
1907e705c121SKalle Valo 
1908e705c121SKalle Valo int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1909e705c121SKalle Valo 		      struct ieee80211_vif *vif,
1910e705c121SKalle Valo 		      u8 sta_id)
1911e705c121SKalle Valo {
1912e705c121SKalle Valo 	int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1913e705c121SKalle Valo 
1914e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1915e705c121SKalle Valo 
1916e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1917e705c121SKalle Valo 	return ret;
1918e705c121SKalle Valo }
1919e705c121SKalle Valo 
19200e39eb03SChaya Rachel Ivgi int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1921e705c121SKalle Valo 			     struct iwl_mvm_int_sta *sta,
1922ced19f26SSara Sharon 			     u32 qmask, enum nl80211_iftype iftype,
1923ced19f26SSara Sharon 			     enum iwl_sta_type type)
1924e705c121SKalle Valo {
1925df65c8d1SAvraham Stern 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1926df65c8d1SAvraham Stern 	    sta->sta_id == IWL_MVM_INVALID_STA) {
1927e705c121SKalle Valo 		sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
19280ae98812SSara Sharon 		if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
1929e705c121SKalle Valo 			return -ENOSPC;
1930e705c121SKalle Valo 	}
1931e705c121SKalle Valo 
1932e705c121SKalle Valo 	sta->tfd_queue_msk = qmask;
1933ced19f26SSara Sharon 	sta->type = type;
1934e705c121SKalle Valo 
1935e705c121SKalle Valo 	/* put a non-NULL value so iterating over the stations won't stop */
1936e705c121SKalle Valo 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1937e705c121SKalle Valo 	return 0;
1938e705c121SKalle Valo }
1939e705c121SKalle Valo 
194026d6c16bSSara Sharon void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
1941e705c121SKalle Valo {
1942e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
1943e705c121SKalle Valo 	memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
19440ae98812SSara Sharon 	sta->sta_id = IWL_MVM_INVALID_STA;
1945e705c121SKalle Valo }
1946e705c121SKalle Valo 
194791cf5dedSJohannes Berg static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 queue,
1948b13f43a4SEmmanuel Grumbach 					  u8 sta_id, u8 fifo)
1949e705c121SKalle Valo {
19509617040eSEmmanuel Grumbach 	unsigned int wdg_timeout =
19519617040eSEmmanuel Grumbach 		mvm->trans->trans_cfg->base_params->wd_timeout;
195228d0793eSLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
1953b13f43a4SEmmanuel Grumbach 		.fifo = fifo,
1954b13f43a4SEmmanuel Grumbach 		.sta_id = sta_id,
195528d0793eSLiad Kaufman 		.tid = IWL_MAX_TID_COUNT,
195628d0793eSLiad Kaufman 		.aggregate = false,
195728d0793eSLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
195828d0793eSLiad Kaufman 	};
195928d0793eSLiad Kaufman 
196091cf5dedSJohannes Berg 	WARN_ON(iwl_mvm_has_new_tx_api(mvm));
196191cf5dedSJohannes Berg 
196291cf5dedSJohannes Berg 	iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
196328d0793eSLiad Kaufman }
196491cf5dedSJohannes Berg 
196591cf5dedSJohannes Berg static int iwl_mvm_enable_aux_snif_queue_tvqm(struct iwl_mvm *mvm, u8 sta_id)
196691cf5dedSJohannes Berg {
19679617040eSEmmanuel Grumbach 	unsigned int wdg_timeout =
19689617040eSEmmanuel Grumbach 		mvm->trans->trans_cfg->base_params->wd_timeout;
196991cf5dedSJohannes Berg 
197091cf5dedSJohannes Berg 	WARN_ON(!iwl_mvm_has_new_tx_api(mvm));
197191cf5dedSJohannes Berg 
197291cf5dedSJohannes Berg 	return iwl_mvm_tvqm_enable_txq(mvm, sta_id, IWL_MAX_TID_COUNT,
197391cf5dedSJohannes Berg 				       wdg_timeout);
197491cf5dedSJohannes Berg }
197591cf5dedSJohannes Berg 
197691cf5dedSJohannes Berg static int iwl_mvm_add_int_sta_with_queue(struct iwl_mvm *mvm, int macidx,
1977be82ecd3SAvraham Stern 					  int maccolor, u8 *addr,
197891cf5dedSJohannes Berg 					  struct iwl_mvm_int_sta *sta,
197991cf5dedSJohannes Berg 					  u16 *queue, int fifo)
198091cf5dedSJohannes Berg {
198191cf5dedSJohannes Berg 	int ret;
198291cf5dedSJohannes Berg 
198391cf5dedSJohannes Berg 	/* Map queue to fifo - needs to happen before adding station */
198491cf5dedSJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm))
198591cf5dedSJohannes Berg 		iwl_mvm_enable_aux_snif_queue(mvm, *queue, sta->sta_id, fifo);
198691cf5dedSJohannes Berg 
1987be82ecd3SAvraham Stern 	ret = iwl_mvm_add_int_sta_common(mvm, sta, addr, macidx, maccolor);
198891cf5dedSJohannes Berg 	if (ret) {
198991cf5dedSJohannes Berg 		if (!iwl_mvm_has_new_tx_api(mvm))
199091cf5dedSJohannes Berg 			iwl_mvm_disable_txq(mvm, NULL, *queue,
199191cf5dedSJohannes Berg 					    IWL_MAX_TID_COUNT, 0);
199291cf5dedSJohannes Berg 		return ret;
199391cf5dedSJohannes Berg 	}
199491cf5dedSJohannes Berg 
199591cf5dedSJohannes Berg 	/*
199691cf5dedSJohannes Berg 	 * For 22000 firmware and on we cannot add queue to a station unknown
199791cf5dedSJohannes Berg 	 * to firmware so enable queue here - after the station was added
199891cf5dedSJohannes Berg 	 */
199991cf5dedSJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm)) {
200091cf5dedSJohannes Berg 		int txq;
200191cf5dedSJohannes Berg 
200291cf5dedSJohannes Berg 		txq = iwl_mvm_enable_aux_snif_queue_tvqm(mvm, sta->sta_id);
200391cf5dedSJohannes Berg 		if (txq < 0) {
200491cf5dedSJohannes Berg 			iwl_mvm_rm_sta_common(mvm, sta->sta_id);
200591cf5dedSJohannes Berg 			return txq;
200691cf5dedSJohannes Berg 		}
200791cf5dedSJohannes Berg 
200891cf5dedSJohannes Berg 		*queue = txq;
200991cf5dedSJohannes Berg 	}
201091cf5dedSJohannes Berg 
201191cf5dedSJohannes Berg 	return 0;
2012c5a719eeSSara Sharon }
2013c5a719eeSSara Sharon 
20142c2c3647SNathan Errera int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm, u32 lmac_id)
2015c5a719eeSSara Sharon {
2016c5a719eeSSara Sharon 	int ret;
2017c5a719eeSSara Sharon 
2018c5a719eeSSara Sharon 	lockdep_assert_held(&mvm->mutex);
2019c5a719eeSSara Sharon 
2020c5a719eeSSara Sharon 	/* Allocate aux station and assign to it the aux queue */
2021c5a719eeSSara Sharon 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
2022ced19f26SSara Sharon 				       NL80211_IFTYPE_UNSPECIFIED,
2023ced19f26SSara Sharon 				       IWL_STA_AUX_ACTIVITY);
2024c5a719eeSSara Sharon 	if (ret)
2025c5a719eeSSara Sharon 		return ret;
2026c5a719eeSSara Sharon 
20272c2c3647SNathan Errera 	/*
20282c2c3647SNathan Errera 	 * In CDB NICs we need to specify which lmac to use for aux activity
20292c2c3647SNathan Errera 	 * using the mac_id argument place to send lmac_id to the function
20302c2c3647SNathan Errera 	 */
20312c2c3647SNathan Errera 	ret = iwl_mvm_add_int_sta_with_queue(mvm, lmac_id, 0, NULL,
203291cf5dedSJohannes Berg 					     &mvm->aux_sta, &mvm->aux_queue,
2033b13f43a4SEmmanuel Grumbach 					     IWL_MVM_TX_FIFO_MCAST);
2034c5a719eeSSara Sharon 	if (ret) {
2035e705c121SKalle Valo 		iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2036e705c121SKalle Valo 		return ret;
2037e705c121SKalle Valo 	}
2038e705c121SKalle Valo 
2039c5a719eeSSara Sharon 	return 0;
2040c5a719eeSSara Sharon }
2041c5a719eeSSara Sharon 
20420e39eb03SChaya Rachel Ivgi int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
20430e39eb03SChaya Rachel Ivgi {
20440e39eb03SChaya Rachel Ivgi 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
20450e39eb03SChaya Rachel Ivgi 
20460e39eb03SChaya Rachel Ivgi 	lockdep_assert_held(&mvm->mutex);
2047b13f43a4SEmmanuel Grumbach 
204891cf5dedSJohannes Berg 	return iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
2049be82ecd3SAvraham Stern 					      NULL, &mvm->snif_sta,
2050be82ecd3SAvraham Stern 					      &mvm->snif_queue,
2051b13f43a4SEmmanuel Grumbach 					      IWL_MVM_TX_FIFO_BE);
20520e39eb03SChaya Rachel Ivgi }
20530e39eb03SChaya Rachel Ivgi 
20540e39eb03SChaya Rachel Ivgi int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
20550e39eb03SChaya Rachel Ivgi {
20560e39eb03SChaya Rachel Ivgi 	int ret;
20570e39eb03SChaya Rachel Ivgi 
20580e39eb03SChaya Rachel Ivgi 	lockdep_assert_held(&mvm->mutex);
20590e39eb03SChaya Rachel Ivgi 
2060*e223e42aSGregory Greenman 	if (WARN_ON_ONCE(mvm->snif_sta.sta_id == IWL_MVM_INVALID_STA))
2061*e223e42aSGregory Greenman 		return -EINVAL;
2062*e223e42aSGregory Greenman 
2063cfbc6c4cSSara Sharon 	iwl_mvm_disable_txq(mvm, NULL, mvm->snif_queue, IWL_MAX_TID_COUNT, 0);
20640e39eb03SChaya Rachel Ivgi 	ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
20650e39eb03SChaya Rachel Ivgi 	if (ret)
20660e39eb03SChaya Rachel Ivgi 		IWL_WARN(mvm, "Failed sending remove station\n");
20670e39eb03SChaya Rachel Ivgi 
20680e39eb03SChaya Rachel Ivgi 	return ret;
20690e39eb03SChaya Rachel Ivgi }
20700e39eb03SChaya Rachel Ivgi 
2071f327236dSSharon int iwl_mvm_rm_aux_sta(struct iwl_mvm *mvm)
2072f327236dSSharon {
2073f327236dSSharon 	int ret;
2074f327236dSSharon 
2075f327236dSSharon 	lockdep_assert_held(&mvm->mutex);
2076f327236dSSharon 
2077*e223e42aSGregory Greenman 	if (WARN_ON_ONCE(mvm->aux_sta.sta_id == IWL_MVM_INVALID_STA))
2078*e223e42aSGregory Greenman 		return -EINVAL;
2079*e223e42aSGregory Greenman 
2080f327236dSSharon 	iwl_mvm_disable_txq(mvm, NULL, mvm->aux_queue, IWL_MAX_TID_COUNT, 0);
2081f327236dSSharon 	ret = iwl_mvm_rm_sta_common(mvm, mvm->aux_sta.sta_id);
2082f327236dSSharon 	if (ret)
2083f327236dSSharon 		IWL_WARN(mvm, "Failed sending remove station\n");
2084f327236dSSharon 	iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2085f327236dSSharon 
2086f327236dSSharon 	return ret;
2087f327236dSSharon }
2088f327236dSSharon 
20890e39eb03SChaya Rachel Ivgi void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
20900e39eb03SChaya Rachel Ivgi {
20910e39eb03SChaya Rachel Ivgi 	iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
20920e39eb03SChaya Rachel Ivgi }
20930e39eb03SChaya Rachel Ivgi 
2094e705c121SKalle Valo /*
2095e705c121SKalle Valo  * Send the add station command for the vif's broadcast station.
2096e705c121SKalle Valo  * Assumes that the station was already allocated.
2097e705c121SKalle Valo  *
2098e705c121SKalle Valo  * @mvm: the mvm component
2099e705c121SKalle Valo  * @vif: the interface to which the broadcast station is added
2100e705c121SKalle Valo  * @bsta: the broadcast station to add.
2101e705c121SKalle Valo  */
2102e705c121SKalle Valo int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2103e705c121SKalle Valo {
2104e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2105e705c121SKalle Valo 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2106e705c121SKalle Valo 	static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2107e705c121SKalle Valo 	const u8 *baddr = _baddr;
21087daa7624SJohannes Berg 	int queue;
2109df88c08dSLiad Kaufman 	int ret;
2110c5a719eeSSara Sharon 	unsigned int wdg_timeout =
2111c5a719eeSSara Sharon 		iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2112de24f638SLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
2113de24f638SLiad Kaufman 		.fifo = IWL_MVM_TX_FIFO_VO,
2114de24f638SLiad Kaufman 		.sta_id = mvmvif->bcast_sta.sta_id,
2115de24f638SLiad Kaufman 		.tid = IWL_MAX_TID_COUNT,
2116de24f638SLiad Kaufman 		.aggregate = false,
2117de24f638SLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
2118de24f638SLiad Kaufman 	};
2119de24f638SLiad Kaufman 
2120c5a719eeSSara Sharon 	lockdep_assert_held(&mvm->mutex);
2121c5a719eeSSara Sharon 
2122c8f54701SJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm)) {
21234d339989SLiad Kaufman 		if (vif->type == NL80211_IFTYPE_AP ||
2124f8510d67SNathan Chancellor 		    vif->type == NL80211_IFTYPE_ADHOC) {
212549f71713SSara Sharon 			queue = mvm->probe_queue;
2126f8510d67SNathan Chancellor 		} else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
212749f71713SSara Sharon 			queue = mvm->p2p_dev_queue;
2128f8510d67SNathan Chancellor 		} else {
2129f8510d67SNathan Chancellor 			WARN(1, "Missing required TXQ for adding bcast STA\n");
2130de24f638SLiad Kaufman 			return -EINVAL;
2131f8510d67SNathan Chancellor 		}
2132de24f638SLiad Kaufman 
2133df88c08dSLiad Kaufman 		bsta->tfd_queue_msk |= BIT(queue);
2134c5a719eeSSara Sharon 
2135cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2136de24f638SLiad Kaufman 	}
2137de24f638SLiad Kaufman 
2138e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_ADHOC)
2139e705c121SKalle Valo 		baddr = vif->bss_conf.bssid;
2140e705c121SKalle Valo 
21410ae98812SSara Sharon 	if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
2142e705c121SKalle Valo 		return -ENOSPC;
2143e705c121SKalle Valo 
2144df88c08dSLiad Kaufman 	ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2145e705c121SKalle Valo 					 mvmvif->id, mvmvif->color);
2146df88c08dSLiad Kaufman 	if (ret)
2147df88c08dSLiad Kaufman 		return ret;
2148df88c08dSLiad Kaufman 
2149df88c08dSLiad Kaufman 	/*
21502f7a3863SLuca Coelho 	 * For 22000 firmware and on we cannot add queue to a station unknown
2151c5a719eeSSara Sharon 	 * to firmware so enable queue here - after the station was added
2152df88c08dSLiad Kaufman 	 */
2153310181ecSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
2154cfbc6c4cSSara Sharon 		queue = iwl_mvm_tvqm_enable_txq(mvm, bsta->sta_id,
2155310181ecSSara Sharon 						IWL_MAX_TID_COUNT,
2156c5a719eeSSara Sharon 						wdg_timeout);
215791cf5dedSJohannes Berg 		if (queue < 0) {
215891cf5dedSJohannes Berg 			iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
215991cf5dedSJohannes Berg 			return queue;
216091cf5dedSJohannes Berg 		}
21617daa7624SJohannes Berg 
21627b758a11SLuca Coelho 		if (vif->type == NL80211_IFTYPE_AP ||
21637b758a11SLuca Coelho 		    vif->type == NL80211_IFTYPE_ADHOC)
2164310181ecSSara Sharon 			mvm->probe_queue = queue;
2165310181ecSSara Sharon 		else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
2166310181ecSSara Sharon 			mvm->p2p_dev_queue = queue;
2167310181ecSSara Sharon 	}
2168df88c08dSLiad Kaufman 
2169df88c08dSLiad Kaufman 	return 0;
2170df88c08dSLiad Kaufman }
2171df88c08dSLiad Kaufman 
2172df88c08dSLiad Kaufman static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2173df88c08dSLiad Kaufman 					  struct ieee80211_vif *vif)
2174df88c08dSLiad Kaufman {
2175df88c08dSLiad Kaufman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2176d167e81aSMordechai Goodstein 	int queue;
2177df88c08dSLiad Kaufman 
2178df88c08dSLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
2179df88c08dSLiad Kaufman 
2180f9084775SNathan Errera 	iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true);
2181d49394a1SSara Sharon 
2182d167e81aSMordechai Goodstein 	switch (vif->type) {
2183d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_AP:
2184d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_ADHOC:
2185d167e81aSMordechai Goodstein 		queue = mvm->probe_queue;
2186d167e81aSMordechai Goodstein 		break;
2187d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_P2P_DEVICE:
2188d167e81aSMordechai Goodstein 		queue = mvm->p2p_dev_queue;
2189d167e81aSMordechai Goodstein 		break;
2190d167e81aSMordechai Goodstein 	default:
2191d167e81aSMordechai Goodstein 		WARN(1, "Can't free bcast queue on vif type %d\n",
2192d167e81aSMordechai Goodstein 		     vif->type);
2193d167e81aSMordechai Goodstein 		return;
2194df88c08dSLiad Kaufman 	}
2195df88c08dSLiad Kaufman 
2196cfbc6c4cSSara Sharon 	iwl_mvm_disable_txq(mvm, NULL, queue, IWL_MAX_TID_COUNT, 0);
2197d167e81aSMordechai Goodstein 	if (iwl_mvm_has_new_tx_api(mvm))
2198d167e81aSMordechai Goodstein 		return;
2199d167e81aSMordechai Goodstein 
2200d167e81aSMordechai Goodstein 	WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
2201d167e81aSMordechai Goodstein 	mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
2202e705c121SKalle Valo }
2203e705c121SKalle Valo 
2204e705c121SKalle Valo /* Send the FW a request to remove the station from it's internal data
2205e705c121SKalle Valo  * structures, but DO NOT remove the entry from the local data structures. */
2206e705c121SKalle Valo int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2207e705c121SKalle Valo {
2208e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2209e705c121SKalle Valo 	int ret;
2210e705c121SKalle Valo 
2211e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2212e705c121SKalle Valo 
2213df88c08dSLiad Kaufman 	iwl_mvm_free_bcast_sta_queues(mvm, vif);
2214df88c08dSLiad Kaufman 
2215e705c121SKalle Valo 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
2216e705c121SKalle Valo 	if (ret)
2217e705c121SKalle Valo 		IWL_WARN(mvm, "Failed sending remove station\n");
2218e705c121SKalle Valo 	return ret;
2219e705c121SKalle Valo }
2220e705c121SKalle Valo 
2221e705c121SKalle Valo int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2222e705c121SKalle Valo {
2223e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2224e705c121SKalle Valo 
2225e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2226e705c121SKalle Valo 
2227c8f54701SJohannes Berg 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, 0,
2228ced19f26SSara Sharon 					ieee80211_vif_type_p2p(vif),
2229ced19f26SSara Sharon 					IWL_STA_GENERAL_PURPOSE);
2230e705c121SKalle Valo }
2231e705c121SKalle Valo 
2232e705c121SKalle Valo /* Allocate a new station entry for the broadcast station to the given vif,
2233e705c121SKalle Valo  * and send it to the FW.
2234e705c121SKalle Valo  * Note that each P2P mac should have its own broadcast station.
2235e705c121SKalle Valo  *
2236e705c121SKalle Valo  * @mvm: the mvm component
2237e705c121SKalle Valo  * @vif: the interface to which the broadcast station is added
2238e705c121SKalle Valo  * @bsta: the broadcast station to add. */
2239d197358bSLuca Coelho int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2240e705c121SKalle Valo {
2241e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2242e705c121SKalle Valo 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2243e705c121SKalle Valo 	int ret;
2244e705c121SKalle Valo 
2245e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2246e705c121SKalle Valo 
2247e705c121SKalle Valo 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2248e705c121SKalle Valo 	if (ret)
2249e705c121SKalle Valo 		return ret;
2250e705c121SKalle Valo 
2251e705c121SKalle Valo 	ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2252e705c121SKalle Valo 
2253e705c121SKalle Valo 	if (ret)
2254e705c121SKalle Valo 		iwl_mvm_dealloc_int_sta(mvm, bsta);
2255e705c121SKalle Valo 
2256e705c121SKalle Valo 	return ret;
2257e705c121SKalle Valo }
2258e705c121SKalle Valo 
2259e705c121SKalle Valo void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2260e705c121SKalle Valo {
2261e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2262e705c121SKalle Valo 
2263e705c121SKalle Valo 	iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2264e705c121SKalle Valo }
2265e705c121SKalle Valo 
2266e705c121SKalle Valo /*
2267e705c121SKalle Valo  * Send the FW a request to remove the station from it's internal data
2268e705c121SKalle Valo  * structures, and in addition remove it from the local data structure.
2269e705c121SKalle Valo  */
2270d197358bSLuca Coelho int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2271e705c121SKalle Valo {
2272e705c121SKalle Valo 	int ret;
2273e705c121SKalle Valo 
2274e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2275e705c121SKalle Valo 
2276e705c121SKalle Valo 	ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2277e705c121SKalle Valo 
2278e705c121SKalle Valo 	iwl_mvm_dealloc_bcast_sta(mvm, vif);
2279e705c121SKalle Valo 
2280e705c121SKalle Valo 	return ret;
2281e705c121SKalle Valo }
2282e705c121SKalle Valo 
228326d6c16bSSara Sharon /*
228426d6c16bSSara Sharon  * Allocate a new station entry for the multicast station to the given vif,
228526d6c16bSSara Sharon  * and send it to the FW.
228626d6c16bSSara Sharon  * Note that each AP/GO mac should have its own multicast station.
228726d6c16bSSara Sharon  *
228826d6c16bSSara Sharon  * @mvm: the mvm component
228926d6c16bSSara Sharon  * @vif: the interface to which the multicast station is added
229026d6c16bSSara Sharon  */
229126d6c16bSSara Sharon int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
229226d6c16bSSara Sharon {
229326d6c16bSSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
229426d6c16bSSara Sharon 	struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
229526d6c16bSSara Sharon 	static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
229626d6c16bSSara Sharon 	const u8 *maddr = _maddr;
229726d6c16bSSara Sharon 	struct iwl_trans_txq_scd_cfg cfg = {
2298192a7e1fSJohannes Berg 		.fifo = vif->type == NL80211_IFTYPE_AP ?
2299192a7e1fSJohannes Berg 			IWL_MVM_TX_FIFO_MCAST : IWL_MVM_TX_FIFO_BE,
230026d6c16bSSara Sharon 		.sta_id = msta->sta_id,
23016508de03SIlan Peer 		.tid = 0,
230226d6c16bSSara Sharon 		.aggregate = false,
230326d6c16bSSara Sharon 		.frame_limit = IWL_FRAME_LIMIT,
230426d6c16bSSara Sharon 	};
230526d6c16bSSara Sharon 	unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
230626d6c16bSSara Sharon 	int ret;
230726d6c16bSSara Sharon 
230826d6c16bSSara Sharon 	lockdep_assert_held(&mvm->mutex);
230926d6c16bSSara Sharon 
2310ee48b722SLiad Kaufman 	if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2311ee48b722SLiad Kaufman 		    vif->type != NL80211_IFTYPE_ADHOC))
231226d6c16bSSara Sharon 		return -ENOTSUPP;
231326d6c16bSSara Sharon 
2314ced19f26SSara Sharon 	/*
2315fc07bd8cSSara Sharon 	 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2316fc07bd8cSSara Sharon 	 * invalid, so make sure we use the queue we want.
2317fc07bd8cSSara Sharon 	 * Note that this is done here as we want to avoid making DQA
2318fc07bd8cSSara Sharon 	 * changes in mac80211 layer.
2319fc07bd8cSSara Sharon 	 */
2320cfbc6c4cSSara Sharon 	if (vif->type == NL80211_IFTYPE_ADHOC)
2321cfbc6c4cSSara Sharon 		mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2322fc07bd8cSSara Sharon 
2323fc07bd8cSSara Sharon 	/*
2324ced19f26SSara Sharon 	 * While in previous FWs we had to exclude cab queue from TFD queue
2325ced19f26SSara Sharon 	 * mask, now it is needed as any other queue.
2326ced19f26SSara Sharon 	 */
2327ced19f26SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm) &&
2328ced19f26SSara Sharon 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2329cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2330cfbc6c4cSSara Sharon 				   timeout);
2331cfbc6c4cSSara Sharon 		msta->tfd_queue_msk |= BIT(mvmvif->cab_queue);
2332ced19f26SSara Sharon 	}
233326d6c16bSSara Sharon 	ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
233426d6c16bSSara Sharon 					 mvmvif->id, mvmvif->color);
233591cf5dedSJohannes Berg 	if (ret)
233691cf5dedSJohannes Berg 		goto err;
233726d6c16bSSara Sharon 
233826d6c16bSSara Sharon 	/*
233926d6c16bSSara Sharon 	 * Enable cab queue after the ADD_STA command is sent.
23402f7a3863SLuca Coelho 	 * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
2341ced19f26SSara Sharon 	 * command with unknown station id, and for FW that doesn't support
2342ced19f26SSara Sharon 	 * station API since the cab queue is not included in the
2343ced19f26SSara Sharon 	 * tfd_queue_mask.
234426d6c16bSSara Sharon 	 */
2345310181ecSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
2346cfbc6c4cSSara Sharon 		int queue = iwl_mvm_tvqm_enable_txq(mvm, msta->sta_id,
23476508de03SIlan Peer 						    0,
234826d6c16bSSara Sharon 						    timeout);
234991cf5dedSJohannes Berg 		if (queue < 0) {
235091cf5dedSJohannes Berg 			ret = queue;
235191cf5dedSJohannes Berg 			goto err;
235291cf5dedSJohannes Berg 		}
2353e2af3fabSSara Sharon 		mvmvif->cab_queue = queue;
2354ced19f26SSara Sharon 	} else if (!fw_has_api(&mvm->fw->ucode_capa,
2355fc07bd8cSSara Sharon 			       IWL_UCODE_TLV_API_STA_TYPE))
2356cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2357cfbc6c4cSSara Sharon 				   timeout);
235826d6c16bSSara Sharon 
235926d6c16bSSara Sharon 	return 0;
236091cf5dedSJohannes Berg err:
236191cf5dedSJohannes Berg 	iwl_mvm_dealloc_int_sta(mvm, msta);
236291cf5dedSJohannes Berg 	return ret;
236326d6c16bSSara Sharon }
236426d6c16bSSara Sharon 
236528916a16SEmmanuel Grumbach static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
236628916a16SEmmanuel Grumbach 				    struct ieee80211_key_conf *keyconf,
236728916a16SEmmanuel Grumbach 				    bool mcast)
236828916a16SEmmanuel Grumbach {
236928916a16SEmmanuel Grumbach 	union {
237028916a16SEmmanuel Grumbach 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
237128916a16SEmmanuel Grumbach 		struct iwl_mvm_add_sta_key_cmd cmd;
237228916a16SEmmanuel Grumbach 	} u = {};
237328916a16SEmmanuel Grumbach 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
237428916a16SEmmanuel Grumbach 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
237528916a16SEmmanuel Grumbach 	__le16 key_flags;
237628916a16SEmmanuel Grumbach 	int ret, size;
237728916a16SEmmanuel Grumbach 	u32 status;
237828916a16SEmmanuel Grumbach 
237928916a16SEmmanuel Grumbach 	/* This is a valid situation for GTK removal */
238028916a16SEmmanuel Grumbach 	if (sta_id == IWL_MVM_INVALID_STA)
238128916a16SEmmanuel Grumbach 		return 0;
238228916a16SEmmanuel Grumbach 
238328916a16SEmmanuel Grumbach 	key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
238428916a16SEmmanuel Grumbach 				 STA_KEY_FLG_KEYID_MSK);
238528916a16SEmmanuel Grumbach 	key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
238628916a16SEmmanuel Grumbach 	key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
238728916a16SEmmanuel Grumbach 
238828916a16SEmmanuel Grumbach 	if (mcast)
238928916a16SEmmanuel Grumbach 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
239028916a16SEmmanuel Grumbach 
239128916a16SEmmanuel Grumbach 	/*
239228916a16SEmmanuel Grumbach 	 * The fields assigned here are in the same location at the start
239328916a16SEmmanuel Grumbach 	 * of the command, so we can do this union trick.
239428916a16SEmmanuel Grumbach 	 */
239528916a16SEmmanuel Grumbach 	u.cmd.common.key_flags = key_flags;
239628916a16SEmmanuel Grumbach 	u.cmd.common.key_offset = keyconf->hw_key_idx;
239728916a16SEmmanuel Grumbach 	u.cmd.common.sta_id = sta_id;
239828916a16SEmmanuel Grumbach 
239928916a16SEmmanuel Grumbach 	size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
240028916a16SEmmanuel Grumbach 
240128916a16SEmmanuel Grumbach 	status = ADD_STA_SUCCESS;
240228916a16SEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
240328916a16SEmmanuel Grumbach 					  &status);
240428916a16SEmmanuel Grumbach 
240528916a16SEmmanuel Grumbach 	switch (status) {
240628916a16SEmmanuel Grumbach 	case ADD_STA_SUCCESS:
240728916a16SEmmanuel Grumbach 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
240828916a16SEmmanuel Grumbach 		break;
240928916a16SEmmanuel Grumbach 	default:
241028916a16SEmmanuel Grumbach 		ret = -EIO;
241128916a16SEmmanuel Grumbach 		IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
241228916a16SEmmanuel Grumbach 		break;
241328916a16SEmmanuel Grumbach 	}
241428916a16SEmmanuel Grumbach 
241528916a16SEmmanuel Grumbach 	return ret;
241628916a16SEmmanuel Grumbach }
241728916a16SEmmanuel Grumbach 
241826d6c16bSSara Sharon /*
241926d6c16bSSara Sharon  * Send the FW a request to remove the station from it's internal data
242026d6c16bSSara Sharon  * structures, and in addition remove it from the local data structure.
242126d6c16bSSara Sharon  */
242226d6c16bSSara Sharon int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
242326d6c16bSSara Sharon {
242426d6c16bSSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
242526d6c16bSSara Sharon 	int ret;
242626d6c16bSSara Sharon 
242726d6c16bSSara Sharon 	lockdep_assert_held(&mvm->mutex);
242826d6c16bSSara Sharon 
2429f9084775SNathan Errera 	iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true);
2430d49394a1SSara Sharon 
2431cfbc6c4cSSara Sharon 	iwl_mvm_disable_txq(mvm, NULL, mvmvif->cab_queue, 0, 0);
243226d6c16bSSara Sharon 
243326d6c16bSSara Sharon 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
243426d6c16bSSara Sharon 	if (ret)
243526d6c16bSSara Sharon 		IWL_WARN(mvm, "Failed sending remove station\n");
243626d6c16bSSara Sharon 
243726d6c16bSSara Sharon 	return ret;
243826d6c16bSSara Sharon }
243926d6c16bSSara Sharon 
2440e705c121SKalle Valo #define IWL_MAX_RX_BA_SESSIONS 16
2441e705c121SKalle Valo 
2442b915c101SSara Sharon static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
244310b2b201SSara Sharon {
24446b2dbce5SEmmanuel Grumbach 	struct iwl_mvm_rss_sync_notif notif = {
2445b915c101SSara Sharon 		.metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
2446b915c101SSara Sharon 		.metadata.sync = 1,
2447b915c101SSara Sharon 		.delba.baid = baid,
2448b915c101SSara Sharon 	};
2449b915c101SSara Sharon 	iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
245010b2b201SSara Sharon };
245110b2b201SSara Sharon 
2452b915c101SSara Sharon static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2453b915c101SSara Sharon 				 struct iwl_mvm_baid_data *data)
2454b915c101SSara Sharon {
2455b915c101SSara Sharon 	int i;
2456b915c101SSara Sharon 
2457b915c101SSara Sharon 	iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2458b915c101SSara Sharon 
2459b915c101SSara Sharon 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2460b915c101SSara Sharon 		int j;
2461b915c101SSara Sharon 		struct iwl_mvm_reorder_buffer *reorder_buf =
2462b915c101SSara Sharon 			&data->reorder_buf[i];
2463dfdddd92SJohannes Berg 		struct iwl_mvm_reorder_buf_entry *entries =
2464dfdddd92SJohannes Berg 			&data->entries[i * data->entries_per_queue];
2465b915c101SSara Sharon 
24660690405fSSara Sharon 		spin_lock_bh(&reorder_buf->lock);
24670690405fSSara Sharon 		if (likely(!reorder_buf->num_stored)) {
24680690405fSSara Sharon 			spin_unlock_bh(&reorder_buf->lock);
2469b915c101SSara Sharon 			continue;
24700690405fSSara Sharon 		}
2471b915c101SSara Sharon 
2472b915c101SSara Sharon 		/*
2473b915c101SSara Sharon 		 * This shouldn't happen in regular DELBA since the internal
2474b915c101SSara Sharon 		 * delBA notification should trigger a release of all frames in
2475b915c101SSara Sharon 		 * the reorder buffer.
2476b915c101SSara Sharon 		 */
2477b915c101SSara Sharon 		WARN_ON(1);
2478b915c101SSara Sharon 
2479b915c101SSara Sharon 		for (j = 0; j < reorder_buf->buf_size; j++)
2480dfdddd92SJohannes Berg 			__skb_queue_purge(&entries[j].e.frames);
24810690405fSSara Sharon 		/*
24820690405fSSara Sharon 		 * Prevent timer re-arm. This prevents a very far fetched case
24830690405fSSara Sharon 		 * where we timed out on the notification. There may be prior
24840690405fSSara Sharon 		 * RX frames pending in the RX queue before the notification
24850690405fSSara Sharon 		 * that might get processed between now and the actual deletion
24860690405fSSara Sharon 		 * and we would re-arm the timer although we are deleting the
24870690405fSSara Sharon 		 * reorder buffer.
24880690405fSSara Sharon 		 */
24890690405fSSara Sharon 		reorder_buf->removed = true;
24900690405fSSara Sharon 		spin_unlock_bh(&reorder_buf->lock);
24910690405fSSara Sharon 		del_timer_sync(&reorder_buf->reorder_timer);
2492b915c101SSara Sharon 	}
2493b915c101SSara Sharon }
2494b915c101SSara Sharon 
2495b915c101SSara Sharon static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2496b915c101SSara Sharon 					struct iwl_mvm_baid_data *data,
2497514c3069SLuca Coelho 					u16 ssn, u16 buf_size)
2498b915c101SSara Sharon {
2499b915c101SSara Sharon 	int i;
2500b915c101SSara Sharon 
2501b915c101SSara Sharon 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2502b915c101SSara Sharon 		struct iwl_mvm_reorder_buffer *reorder_buf =
2503b915c101SSara Sharon 			&data->reorder_buf[i];
2504dfdddd92SJohannes Berg 		struct iwl_mvm_reorder_buf_entry *entries =
2505dfdddd92SJohannes Berg 			&data->entries[i * data->entries_per_queue];
2506b915c101SSara Sharon 		int j;
2507b915c101SSara Sharon 
2508b915c101SSara Sharon 		reorder_buf->num_stored = 0;
2509b915c101SSara Sharon 		reorder_buf->head_sn = ssn;
2510b915c101SSara Sharon 		reorder_buf->buf_size = buf_size;
25110690405fSSara Sharon 		/* rx reorder timer */
25128cef5344SKees Cook 		timer_setup(&reorder_buf->reorder_timer,
25138cef5344SKees Cook 			    iwl_mvm_reorder_timer_expired, 0);
25140690405fSSara Sharon 		spin_lock_init(&reorder_buf->lock);
25150690405fSSara Sharon 		reorder_buf->mvm = mvm;
2516b915c101SSara Sharon 		reorder_buf->queue = i;
25175d43eab6SSara Sharon 		reorder_buf->valid = false;
2518b915c101SSara Sharon 		for (j = 0; j < reorder_buf->buf_size; j++)
2519dfdddd92SJohannes Berg 			__skb_queue_head_init(&entries[j].e.frames);
2520b915c101SSara Sharon 	}
252110b2b201SSara Sharon }
252210b2b201SSara Sharon 
2523e705c121SKalle Valo int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2524514c3069SLuca Coelho 		       int tid, u16 ssn, bool start, u16 buf_size, u16 timeout)
2525e705c121SKalle Valo {
2526e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2527e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
252810b2b201SSara Sharon 	struct iwl_mvm_baid_data *baid_data = NULL;
2529e705c121SKalle Valo 	int ret;
2530e705c121SKalle Valo 	u32 status;
2531e705c121SKalle Valo 
2532e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2533e705c121SKalle Valo 
2534e705c121SKalle Valo 	if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
2535e705c121SKalle Valo 		IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2536e705c121SKalle Valo 		return -ENOSPC;
2537e705c121SKalle Valo 	}
2538e705c121SKalle Valo 
253910b2b201SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) && start) {
2540dfdddd92SJohannes Berg 		u16 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2541dfdddd92SJohannes Berg 
2542dfdddd92SJohannes Berg 		/* sparse doesn't like the __align() so don't check */
2543dfdddd92SJohannes Berg #ifndef __CHECKER__
2544dfdddd92SJohannes Berg 		/*
2545dfdddd92SJohannes Berg 		 * The division below will be OK if either the cache line size
2546dfdddd92SJohannes Berg 		 * can be divided by the entry size (ALIGN will round up) or if
2547dfdddd92SJohannes Berg 		 * if the entry size can be divided by the cache line size, in
2548dfdddd92SJohannes Berg 		 * which case the ALIGN() will do nothing.
2549dfdddd92SJohannes Berg 		 */
2550dfdddd92SJohannes Berg 		BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2551dfdddd92SJohannes Berg 			     sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2552dfdddd92SJohannes Berg #endif
2553dfdddd92SJohannes Berg 
2554dfdddd92SJohannes Berg 		/*
2555dfdddd92SJohannes Berg 		 * Upward align the reorder buffer size to fill an entire cache
2556dfdddd92SJohannes Berg 		 * line for each queue, to avoid sharing cache lines between
2557dfdddd92SJohannes Berg 		 * different queues.
2558dfdddd92SJohannes Berg 		 */
2559dfdddd92SJohannes Berg 		reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2560dfdddd92SJohannes Berg 
256110b2b201SSara Sharon 		/*
256210b2b201SSara Sharon 		 * Allocate here so if allocation fails we can bail out early
256310b2b201SSara Sharon 		 * before starting the BA session in the firmware
256410b2b201SSara Sharon 		 */
2565b915c101SSara Sharon 		baid_data = kzalloc(sizeof(*baid_data) +
2566b915c101SSara Sharon 				    mvm->trans->num_rx_queues *
2567dfdddd92SJohannes Berg 				    reorder_buf_size,
2568b915c101SSara Sharon 				    GFP_KERNEL);
256910b2b201SSara Sharon 		if (!baid_data)
257010b2b201SSara Sharon 			return -ENOMEM;
2571dfdddd92SJohannes Berg 
2572dfdddd92SJohannes Berg 		/*
2573dfdddd92SJohannes Berg 		 * This division is why we need the above BUILD_BUG_ON(),
2574dfdddd92SJohannes Berg 		 * if that doesn't hold then this will not be right.
2575dfdddd92SJohannes Berg 		 */
2576dfdddd92SJohannes Berg 		baid_data->entries_per_queue =
2577dfdddd92SJohannes Berg 			reorder_buf_size / sizeof(baid_data->entries[0]);
257810b2b201SSara Sharon 	}
257910b2b201SSara Sharon 
2580e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2581e705c121SKalle Valo 	cmd.sta_id = mvm_sta->sta_id;
2582e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
2583e705c121SKalle Valo 	if (start) {
2584e705c121SKalle Valo 		cmd.add_immediate_ba_tid = (u8) tid;
2585e705c121SKalle Valo 		cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
2586514c3069SLuca Coelho 		cmd.rx_ba_window = cpu_to_le16(buf_size);
2587e705c121SKalle Valo 	} else {
2588e705c121SKalle Valo 		cmd.remove_immediate_ba_tid = (u8) tid;
2589e705c121SKalle Valo 	}
2590e705c121SKalle Valo 	cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2591e705c121SKalle Valo 				  STA_MODIFY_REMOVE_BA_TID;
2592e705c121SKalle Valo 
2593e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
2594854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2595854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
2596e705c121SKalle Valo 					  &cmd, &status);
2597e705c121SKalle Valo 	if (ret)
259810b2b201SSara Sharon 		goto out_free;
2599e705c121SKalle Valo 
2600837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2601e705c121SKalle Valo 	case ADD_STA_SUCCESS:
260235263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2603e705c121SKalle Valo 			     start ? "start" : "stopp");
2604e705c121SKalle Valo 		break;
2605e705c121SKalle Valo 	case ADD_STA_IMMEDIATE_BA_FAILURE:
2606e705c121SKalle Valo 		IWL_WARN(mvm, "RX BA Session refused by fw\n");
2607e705c121SKalle Valo 		ret = -ENOSPC;
2608e705c121SKalle Valo 		break;
2609e705c121SKalle Valo 	default:
2610e705c121SKalle Valo 		ret = -EIO;
2611e705c121SKalle Valo 		IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2612e705c121SKalle Valo 			start ? "start" : "stopp", status);
2613e705c121SKalle Valo 		break;
2614e705c121SKalle Valo 	}
2615e705c121SKalle Valo 
261610b2b201SSara Sharon 	if (ret)
261710b2b201SSara Sharon 		goto out_free;
261810b2b201SSara Sharon 
261910b2b201SSara Sharon 	if (start) {
262010b2b201SSara Sharon 		u8 baid;
262110b2b201SSara Sharon 
2622e705c121SKalle Valo 		mvm->rx_ba_sessions++;
262310b2b201SSara Sharon 
262410b2b201SSara Sharon 		if (!iwl_mvm_has_new_rx_api(mvm))
262510b2b201SSara Sharon 			return 0;
262610b2b201SSara Sharon 
262710b2b201SSara Sharon 		if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
262810b2b201SSara Sharon 			ret = -EINVAL;
262910b2b201SSara Sharon 			goto out_free;
263010b2b201SSara Sharon 		}
263110b2b201SSara Sharon 		baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
263210b2b201SSara Sharon 			    IWL_ADD_STA_BAID_SHIFT);
263310b2b201SSara Sharon 		baid_data->baid = baid;
263410b2b201SSara Sharon 		baid_data->timeout = timeout;
263510b2b201SSara Sharon 		baid_data->last_rx = jiffies;
26368cef5344SKees Cook 		baid_data->rcu_ptr = &mvm->baid_map[baid];
26378cef5344SKees Cook 		timer_setup(&baid_data->session_timer,
26388cef5344SKees Cook 			    iwl_mvm_rx_agg_session_expired, 0);
263910b2b201SSara Sharon 		baid_data->mvm = mvm;
264010b2b201SSara Sharon 		baid_data->tid = tid;
264110b2b201SSara Sharon 		baid_data->sta_id = mvm_sta->sta_id;
264210b2b201SSara Sharon 
264310b2b201SSara Sharon 		mvm_sta->tid_to_baid[tid] = baid;
264410b2b201SSara Sharon 		if (timeout)
264510b2b201SSara Sharon 			mod_timer(&baid_data->session_timer,
264610b2b201SSara Sharon 				  TU_TO_EXP_TIME(timeout * 2));
264710b2b201SSara Sharon 
26483f1c4c58SSara Sharon 		iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size);
264910b2b201SSara Sharon 		/*
265010b2b201SSara Sharon 		 * protect the BA data with RCU to cover a case where our
265110b2b201SSara Sharon 		 * internal RX sync mechanism will timeout (not that it's
265210b2b201SSara Sharon 		 * supposed to happen) and we will free the session data while
265310b2b201SSara Sharon 		 * RX is being processed in parallel
265410b2b201SSara Sharon 		 */
265535263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
265635263a03SSara Sharon 			     mvm_sta->sta_id, tid, baid);
265710b2b201SSara Sharon 		WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
265810b2b201SSara Sharon 		rcu_assign_pointer(mvm->baid_map[baid], baid_data);
265960dec523SSara Sharon 	} else  {
266010b2b201SSara Sharon 		u8 baid = mvm_sta->tid_to_baid[tid];
266110b2b201SSara Sharon 
266260dec523SSara Sharon 		if (mvm->rx_ba_sessions > 0)
2663e705c121SKalle Valo 			/* check that restart flow didn't zero the counter */
2664e705c121SKalle Valo 			mvm->rx_ba_sessions--;
266510b2b201SSara Sharon 		if (!iwl_mvm_has_new_rx_api(mvm))
266610b2b201SSara Sharon 			return 0;
2667e705c121SKalle Valo 
266810b2b201SSara Sharon 		if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
266910b2b201SSara Sharon 			return -EINVAL;
267010b2b201SSara Sharon 
267110b2b201SSara Sharon 		baid_data = rcu_access_pointer(mvm->baid_map[baid]);
267210b2b201SSara Sharon 		if (WARN_ON(!baid_data))
267310b2b201SSara Sharon 			return -EINVAL;
267410b2b201SSara Sharon 
267510b2b201SSara Sharon 		/* synchronize all rx queues so we can safely delete */
2676b915c101SSara Sharon 		iwl_mvm_free_reorder(mvm, baid_data);
267710b2b201SSara Sharon 		del_timer_sync(&baid_data->session_timer);
267810b2b201SSara Sharon 		RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
267910b2b201SSara Sharon 		kfree_rcu(baid_data, rcu_head);
268035263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
268110b2b201SSara Sharon 	}
268210b2b201SSara Sharon 	return 0;
268310b2b201SSara Sharon 
268410b2b201SSara Sharon out_free:
268510b2b201SSara Sharon 	kfree(baid_data);
2686e705c121SKalle Valo 	return ret;
2687e705c121SKalle Valo }
2688e705c121SKalle Valo 
26899794c64fSLiad Kaufman int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2690e705c121SKalle Valo 		       int tid, u8 queue, bool start)
2691e705c121SKalle Valo {
2692e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2693e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
2694e705c121SKalle Valo 	int ret;
2695e705c121SKalle Valo 	u32 status;
2696e705c121SKalle Valo 
2697e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2698e705c121SKalle Valo 
2699e705c121SKalle Valo 	if (start) {
2700e705c121SKalle Valo 		mvm_sta->tfd_queue_msk |= BIT(queue);
2701e705c121SKalle Valo 		mvm_sta->tid_disable_agg &= ~BIT(tid);
2702e705c121SKalle Valo 	} else {
2703cf961e16SLiad Kaufman 		/* In DQA-mode the queue isn't removed on agg termination */
2704e705c121SKalle Valo 		mvm_sta->tid_disable_agg |= BIT(tid);
2705e705c121SKalle Valo 	}
2706e705c121SKalle Valo 
2707e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2708e705c121SKalle Valo 	cmd.sta_id = mvm_sta->sta_id;
2709e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
2710bb49701bSSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm))
2711bb49701bSSara Sharon 		cmd.modify_mask = STA_MODIFY_QUEUES;
2712bb49701bSSara Sharon 	cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
2713e705c121SKalle Valo 	cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2714e705c121SKalle Valo 	cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2715e705c121SKalle Valo 
2716e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
2717854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2718854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
2719e705c121SKalle Valo 					  &cmd, &status);
2720e705c121SKalle Valo 	if (ret)
2721e705c121SKalle Valo 		return ret;
2722e705c121SKalle Valo 
2723837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2724e705c121SKalle Valo 	case ADD_STA_SUCCESS:
2725e705c121SKalle Valo 		break;
2726e705c121SKalle Valo 	default:
2727e705c121SKalle Valo 		ret = -EIO;
2728e705c121SKalle Valo 		IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2729e705c121SKalle Valo 			start ? "start" : "stopp", status);
2730e705c121SKalle Valo 		break;
2731e705c121SKalle Valo 	}
2732e705c121SKalle Valo 
2733e705c121SKalle Valo 	return ret;
2734e705c121SKalle Valo }
2735e705c121SKalle Valo 
2736e705c121SKalle Valo const u8 tid_to_mac80211_ac[] = {
2737e705c121SKalle Valo 	IEEE80211_AC_BE,
2738e705c121SKalle Valo 	IEEE80211_AC_BK,
2739e705c121SKalle Valo 	IEEE80211_AC_BK,
2740e705c121SKalle Valo 	IEEE80211_AC_BE,
2741e705c121SKalle Valo 	IEEE80211_AC_VI,
2742e705c121SKalle Valo 	IEEE80211_AC_VI,
2743e705c121SKalle Valo 	IEEE80211_AC_VO,
2744e705c121SKalle Valo 	IEEE80211_AC_VO,
27459794c64fSLiad Kaufman 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2746e705c121SKalle Valo };
2747e705c121SKalle Valo 
2748e705c121SKalle Valo static const u8 tid_to_ucode_ac[] = {
2749e705c121SKalle Valo 	AC_BE,
2750e705c121SKalle Valo 	AC_BK,
2751e705c121SKalle Valo 	AC_BK,
2752e705c121SKalle Valo 	AC_BE,
2753e705c121SKalle Valo 	AC_VI,
2754e705c121SKalle Valo 	AC_VI,
2755e705c121SKalle Valo 	AC_VO,
2756e705c121SKalle Valo 	AC_VO,
2757e705c121SKalle Valo };
2758e705c121SKalle Valo 
2759e705c121SKalle Valo int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2760e705c121SKalle Valo 			     struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2761e705c121SKalle Valo {
2762e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2763e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data;
2764dd32162dSLiad Kaufman 	u16 normalized_ssn;
2765b0d795a9SMordechay Goodstein 	u16 txq_id;
2766e705c121SKalle Valo 	int ret;
2767e705c121SKalle Valo 
2768e705c121SKalle Valo 	if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2769e705c121SKalle Valo 		return -EINVAL;
2770e705c121SKalle Valo 
2771bd800e41SNaftali Goldstein 	if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
2772bd800e41SNaftali Goldstein 	    mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2773bd800e41SNaftali Goldstein 		IWL_ERR(mvm,
2774bd800e41SNaftali Goldstein 			"Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
2775e705c121SKalle Valo 			mvmsta->tid_data[tid].state);
2776e705c121SKalle Valo 		return -ENXIO;
2777e705c121SKalle Valo 	}
2778e705c121SKalle Valo 
2779e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2780e705c121SKalle Valo 
2781bd8f3fc6SLiad Kaufman 	if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE &&
2782bd8f3fc6SLiad Kaufman 	    iwl_mvm_has_new_tx_api(mvm)) {
2783bd8f3fc6SLiad Kaufman 		u8 ac = tid_to_mac80211_ac[tid];
2784bd8f3fc6SLiad Kaufman 
2785bd8f3fc6SLiad Kaufman 		ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
2786bd8f3fc6SLiad Kaufman 		if (ret)
2787bd8f3fc6SLiad Kaufman 			return ret;
2788bd8f3fc6SLiad Kaufman 	}
2789bd8f3fc6SLiad Kaufman 
2790e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2791e705c121SKalle Valo 
2792cf961e16SLiad Kaufman 	/*
2793cf961e16SLiad Kaufman 	 * Note the possible cases:
27944a6d2e52SAvraham Stern 	 *  1. An enabled TXQ - TXQ needs to become agg'ed
27954a6d2e52SAvraham Stern 	 *  2. The TXQ hasn't yet been enabled, so find a free one and mark
27964a6d2e52SAvraham Stern 	 *	it as reserved
2797cf961e16SLiad Kaufman 	 */
2798cf961e16SLiad Kaufman 	txq_id = mvmsta->tid_data[tid].txq_id;
279934e10860SSara Sharon 	if (txq_id == IWL_MVM_INVALID_QUEUE) {
2800b0d795a9SMordechay Goodstein 		ret = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2801c8f54701SJohannes Berg 					      IWL_MVM_DQA_MIN_DATA_QUEUE,
2802c8f54701SJohannes Berg 					      IWL_MVM_DQA_MAX_DATA_QUEUE);
2803b0d795a9SMordechay Goodstein 		if (ret < 0) {
2804e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to allocate agg queue\n");
2805f3f240f9SJohannes Berg 			goto out;
2806e705c121SKalle Valo 		}
2807cf961e16SLiad Kaufman 
2808b0d795a9SMordechay Goodstein 		txq_id = ret;
2809b0d795a9SMordechay Goodstein 
2810cf961e16SLiad Kaufman 		/* TXQ hasn't yet been enabled, so mark it only as reserved */
2811cf961e16SLiad Kaufman 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2812b0d795a9SMordechay Goodstein 	} else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) {
2813b0d795a9SMordechay Goodstein 		ret = -ENXIO;
2814b0d795a9SMordechay Goodstein 		IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n",
2815b0d795a9SMordechay Goodstein 			tid, IWL_MAX_HW_QUEUES - 1);
2816b0d795a9SMordechay Goodstein 		goto out;
2817b0d795a9SMordechay Goodstein 
28184a6d2e52SAvraham Stern 	} else if (unlikely(mvm->queue_info[txq_id].status ==
28194a6d2e52SAvraham Stern 			    IWL_MVM_QUEUE_SHARED)) {
28204a6d2e52SAvraham Stern 		ret = -ENXIO;
28214a6d2e52SAvraham Stern 		IWL_DEBUG_TX_QUEUES(mvm,
28224a6d2e52SAvraham Stern 				    "Can't start tid %d agg on shared queue!\n",
28234a6d2e52SAvraham Stern 				    tid);
2824f3f240f9SJohannes Berg 		goto out;
2825cf961e16SLiad Kaufman 	}
28269f9af3d7SLiad Kaufman 
2827cf961e16SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm,
2828cf961e16SLiad Kaufman 			    "AGG for tid %d will be on queue #%d\n",
2829cf961e16SLiad Kaufman 			    tid, txq_id);
2830cf961e16SLiad Kaufman 
2831e705c121SKalle Valo 	tid_data = &mvmsta->tid_data[tid];
2832e705c121SKalle Valo 	tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2833e705c121SKalle Valo 	tid_data->txq_id = txq_id;
2834e705c121SKalle Valo 	*ssn = tid_data->ssn;
2835e705c121SKalle Valo 
2836e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm,
2837e705c121SKalle Valo 			    "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2838e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2839e705c121SKalle Valo 			    tid_data->next_reclaimed);
2840e705c121SKalle Valo 
2841dd32162dSLiad Kaufman 	/*
28422f7a3863SLuca Coelho 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
2843dd32162dSLiad Kaufman 	 * to align the wrap around of ssn so we compare relevant values.
2844dd32162dSLiad Kaufman 	 */
2845dd32162dSLiad Kaufman 	normalized_ssn = tid_data->ssn;
2846286ca8ebSLuca Coelho 	if (mvm->trans->trans_cfg->gen2)
2847dd32162dSLiad Kaufman 		normalized_ssn &= 0xff;
2848dd32162dSLiad Kaufman 
2849dd32162dSLiad Kaufman 	if (normalized_ssn == tid_data->next_reclaimed) {
2850e705c121SKalle Valo 		tid_data->state = IWL_AGG_STARTING;
28512ce113deSJohannes Berg 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
2852e705c121SKalle Valo 	} else {
2853e705c121SKalle Valo 		tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
28547e0ca723SMordechay Goodstein 		ret = IEEE80211_AMPDU_TX_START_DELAY_ADDBA;
28552ce113deSJohannes Berg 	}
2856e705c121SKalle Valo 
28579f9af3d7SLiad Kaufman out:
2858e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2859e705c121SKalle Valo 
2860e705c121SKalle Valo 	return ret;
2861e705c121SKalle Valo }
2862e705c121SKalle Valo 
2863e705c121SKalle Valo int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2864514c3069SLuca Coelho 			    struct ieee80211_sta *sta, u16 tid, u16 buf_size,
2865bb81bb68SEmmanuel Grumbach 			    bool amsdu)
2866e705c121SKalle Valo {
2867e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2868e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2869e705c121SKalle Valo 	unsigned int wdg_timeout =
2870e705c121SKalle Valo 		iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
2871eea76c36SEmmanuel Grumbach 	int queue, ret;
2872cf961e16SLiad Kaufman 	bool alloc_queue = true;
28739f9af3d7SLiad Kaufman 	enum iwl_mvm_queue_status queue_status;
2874e705c121SKalle Valo 	u16 ssn;
2875e705c121SKalle Valo 
2876eea76c36SEmmanuel Grumbach 	struct iwl_trans_txq_scd_cfg cfg = {
2877eea76c36SEmmanuel Grumbach 		.sta_id = mvmsta->sta_id,
2878eea76c36SEmmanuel Grumbach 		.tid = tid,
2879eea76c36SEmmanuel Grumbach 		.frame_limit = buf_size,
2880eea76c36SEmmanuel Grumbach 		.aggregate = true,
2881eea76c36SEmmanuel Grumbach 	};
2882eea76c36SEmmanuel Grumbach 
2883ecaf71deSGregory Greenman 	/*
2884ecaf71deSGregory Greenman 	 * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
2885ecaf71deSGregory Greenman 	 * manager, so this function should never be called in this case.
2886ecaf71deSGregory Greenman 	 */
28874243edb4SEmmanuel Grumbach 	if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
2888ecaf71deSGregory Greenman 		return -EINVAL;
2889ecaf71deSGregory Greenman 
2890e705c121SKalle Valo 	BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2891e705c121SKalle Valo 		     != IWL_MAX_TID_COUNT);
2892e705c121SKalle Valo 
2893e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2894e705c121SKalle Valo 	ssn = tid_data->ssn;
2895e705c121SKalle Valo 	queue = tid_data->txq_id;
2896e705c121SKalle Valo 	tid_data->state = IWL_AGG_ON;
2897e705c121SKalle Valo 	mvmsta->agg_tids |= BIT(tid);
2898e705c121SKalle Valo 	tid_data->ssn = 0xffff;
2899bb81bb68SEmmanuel Grumbach 	tid_data->amsdu_in_ampdu_allowed = amsdu;
2900e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2901e705c121SKalle Valo 
290234e10860SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
290334e10860SSara Sharon 		/*
29040ec9257bSSara Sharon 		 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
29050ec9257bSSara Sharon 		 * would have failed, so if we are here there is no need to
29060ec9257bSSara Sharon 		 * allocate a queue.
29070ec9257bSSara Sharon 		 * However, if aggregation size is different than the default
29080ec9257bSSara Sharon 		 * size, the scheduler should be reconfigured.
29090ec9257bSSara Sharon 		 * We cannot do this with the new TX API, so return unsupported
29100ec9257bSSara Sharon 		 * for now, until it will be offloaded to firmware..
29110ec9257bSSara Sharon 		 * Note that if SCD default value changes - this condition
29120ec9257bSSara Sharon 		 * should be updated as well.
291334e10860SSara Sharon 		 */
29140ec9257bSSara Sharon 		if (buf_size < IWL_FRAME_LIMIT)
291534e10860SSara Sharon 			return -ENOTSUPP;
291634e10860SSara Sharon 
291734e10860SSara Sharon 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
291834e10860SSara Sharon 		if (ret)
291934e10860SSara Sharon 			return -EIO;
292034e10860SSara Sharon 		goto out;
292134e10860SSara Sharon 	}
292234e10860SSara Sharon 
2923eea76c36SEmmanuel Grumbach 	cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
2924e705c121SKalle Valo 
29259f9af3d7SLiad Kaufman 	queue_status = mvm->queue_info[queue].status;
29269f9af3d7SLiad Kaufman 
2927cf961e16SLiad Kaufman 	/* Maybe there is no need to even alloc a queue... */
2928cf961e16SLiad Kaufman 	if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2929cf961e16SLiad Kaufman 		alloc_queue = false;
2930cf961e16SLiad Kaufman 
2931cf961e16SLiad Kaufman 	/*
2932cf961e16SLiad Kaufman 	 * Only reconfig the SCD for the queue if the window size has
2933cf961e16SLiad Kaufman 	 * changed from current (become smaller)
2934cf961e16SLiad Kaufman 	 */
29350ec9257bSSara Sharon 	if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
2936cf961e16SLiad Kaufman 		/*
2937cf961e16SLiad Kaufman 		 * If reconfiguring an existing queue, it first must be
2938cf961e16SLiad Kaufman 		 * drained
2939cf961e16SLiad Kaufman 		 */
2940a1a57877SSara Sharon 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2941cf961e16SLiad Kaufman 						     BIT(queue));
2942cf961e16SLiad Kaufman 		if (ret) {
2943cf961e16SLiad Kaufman 			IWL_ERR(mvm,
2944cf961e16SLiad Kaufman 				"Error draining queue before reconfig\n");
2945cf961e16SLiad Kaufman 			return ret;
2946cf961e16SLiad Kaufman 		}
2947cf961e16SLiad Kaufman 
2948cf961e16SLiad Kaufman 		ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2949cf961e16SLiad Kaufman 					   mvmsta->sta_id, tid,
2950cf961e16SLiad Kaufman 					   buf_size, ssn);
2951cf961e16SLiad Kaufman 		if (ret) {
2952cf961e16SLiad Kaufman 			IWL_ERR(mvm,
2953cf961e16SLiad Kaufman 				"Error reconfiguring TXQ #%d\n", queue);
2954cf961e16SLiad Kaufman 			return ret;
2955cf961e16SLiad Kaufman 		}
2956cf961e16SLiad Kaufman 	}
2957cf961e16SLiad Kaufman 
2958cf961e16SLiad Kaufman 	if (alloc_queue)
2959cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, sta, queue, ssn,
2960cf961e16SLiad Kaufman 				   &cfg, wdg_timeout);
2961e705c121SKalle Valo 
29629f9af3d7SLiad Kaufman 	/* Send ADD_STA command to enable aggs only if the queue isn't shared */
29639f9af3d7SLiad Kaufman 	if (queue_status != IWL_MVM_QUEUE_SHARED) {
2964e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2965e705c121SKalle Valo 		if (ret)
2966e705c121SKalle Valo 			return -EIO;
29679f9af3d7SLiad Kaufman 	}
2968e705c121SKalle Valo 
2969e705c121SKalle Valo 	/* No need to mark as reserved */
2970cf961e16SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
2971e705c121SKalle Valo 
297234e10860SSara Sharon out:
2973e705c121SKalle Valo 	/*
2974e705c121SKalle Valo 	 * Even though in theory the peer could have different
2975e705c121SKalle Valo 	 * aggregation reorder buffer sizes for different sessions,
2976e705c121SKalle Valo 	 * our ucode doesn't allow for that and has a global limit
2977e705c121SKalle Valo 	 * for each station. Therefore, use the minimum of all the
2978e705c121SKalle Valo 	 * aggregation sessions and our default value.
2979e705c121SKalle Valo 	 */
2980e705c121SKalle Valo 	mvmsta->max_agg_bufsize =
2981e705c121SKalle Valo 		min(mvmsta->max_agg_bufsize, buf_size);
2982ecaf71deSGregory Greenman 	mvmsta->lq_sta.rs_drv.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2983e705c121SKalle Valo 
2984e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2985e705c121SKalle Valo 		     sta->addr, tid);
2986e705c121SKalle Valo 
2987cd4d6b0bSGregory Greenman 	return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.rs_drv.lq);
2988e705c121SKalle Valo }
2989e705c121SKalle Valo 
299034e10860SSara Sharon static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
299134e10860SSara Sharon 					struct iwl_mvm_sta *mvmsta,
29924b387906SAvraham Stern 					struct iwl_mvm_tid_data *tid_data)
299334e10860SSara Sharon {
29944b387906SAvraham Stern 	u16 txq_id = tid_data->txq_id;
29954b387906SAvraham Stern 
2996f3f240f9SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
2997f3f240f9SJohannes Berg 
299834e10860SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm))
299934e10860SSara Sharon 		return;
300034e10860SSara Sharon 
300134e10860SSara Sharon 	/*
300234e10860SSara Sharon 	 * The TXQ is marked as reserved only if no traffic came through yet
300334e10860SSara Sharon 	 * This means no traffic has been sent on this TID (agg'd or not), so
300434e10860SSara Sharon 	 * we no longer have use for the queue. Since it hasn't even been
300534e10860SSara Sharon 	 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
300634e10860SSara Sharon 	 * free.
300734e10860SSara Sharon 	 */
30084b387906SAvraham Stern 	if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
300934e10860SSara Sharon 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
30104b387906SAvraham Stern 		tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
30114b387906SAvraham Stern 	}
301234e10860SSara Sharon }
301334e10860SSara Sharon 
3014e705c121SKalle Valo int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3015e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid)
3016e705c121SKalle Valo {
3017e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3018e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3019e705c121SKalle Valo 	u16 txq_id;
3020e705c121SKalle Valo 	int err;
3021e705c121SKalle Valo 
3022e705c121SKalle Valo 	/*
3023e705c121SKalle Valo 	 * If mac80211 is cleaning its state, then say that we finished since
3024e705c121SKalle Valo 	 * our state has been cleared anyway.
3025e705c121SKalle Valo 	 */
3026e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3027e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3028e705c121SKalle Valo 		return 0;
3029e705c121SKalle Valo 	}
3030e705c121SKalle Valo 
3031e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3032e705c121SKalle Valo 
3033e705c121SKalle Valo 	txq_id = tid_data->txq_id;
3034e705c121SKalle Valo 
3035e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
3036e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
3037e705c121SKalle Valo 
3038e705c121SKalle Valo 	mvmsta->agg_tids &= ~BIT(tid);
3039e705c121SKalle Valo 
30404b387906SAvraham Stern 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3041e705c121SKalle Valo 
3042e705c121SKalle Valo 	switch (tid_data->state) {
3043e705c121SKalle Valo 	case IWL_AGG_ON:
3044e705c121SKalle Valo 		tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3045e705c121SKalle Valo 
3046e705c121SKalle Valo 		IWL_DEBUG_TX_QUEUES(mvm,
3047e705c121SKalle Valo 				    "ssn = %d, next_recl = %d\n",
3048e705c121SKalle Valo 				    tid_data->ssn, tid_data->next_reclaimed);
3049e705c121SKalle Valo 
3050e705c121SKalle Valo 		tid_data->ssn = 0xffff;
3051e705c121SKalle Valo 		tid_data->state = IWL_AGG_OFF;
3052e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
3053e705c121SKalle Valo 
3054e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3055e705c121SKalle Valo 
3056e705c121SKalle Valo 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3057e705c121SKalle Valo 		return 0;
3058e705c121SKalle Valo 	case IWL_AGG_STARTING:
3059e705c121SKalle Valo 	case IWL_EMPTYING_HW_QUEUE_ADDBA:
3060e705c121SKalle Valo 		/*
3061e705c121SKalle Valo 		 * The agg session has been stopped before it was set up. This
3062e705c121SKalle Valo 		 * can happen when the AddBA timer times out for example.
3063e705c121SKalle Valo 		 */
3064e705c121SKalle Valo 
3065e705c121SKalle Valo 		/* No barriers since we are under mutex */
3066e705c121SKalle Valo 		lockdep_assert_held(&mvm->mutex);
3067e705c121SKalle Valo 
3068e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3069e705c121SKalle Valo 		tid_data->state = IWL_AGG_OFF;
3070e705c121SKalle Valo 		err = 0;
3071e705c121SKalle Valo 		break;
3072e705c121SKalle Valo 	default:
3073e705c121SKalle Valo 		IWL_ERR(mvm,
3074e705c121SKalle Valo 			"Stopping AGG while state not ON or starting for %d on %d (%d)\n",
3075e705c121SKalle Valo 			mvmsta->sta_id, tid, tid_data->state);
3076e705c121SKalle Valo 		IWL_ERR(mvm,
3077e705c121SKalle Valo 			"\ttid_data->txq_id = %d\n", tid_data->txq_id);
3078e705c121SKalle Valo 		err = -EINVAL;
3079e705c121SKalle Valo 	}
3080e705c121SKalle Valo 
3081e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3082e705c121SKalle Valo 
3083e705c121SKalle Valo 	return err;
3084e705c121SKalle Valo }
3085e705c121SKalle Valo 
3086e705c121SKalle Valo int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3087e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid)
3088e705c121SKalle Valo {
3089e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3090e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3091e705c121SKalle Valo 	u16 txq_id;
3092e705c121SKalle Valo 	enum iwl_mvm_agg_state old_state;
3093e705c121SKalle Valo 
3094e705c121SKalle Valo 	/*
3095e705c121SKalle Valo 	 * First set the agg state to OFF to avoid calling
3096e705c121SKalle Valo 	 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
3097e705c121SKalle Valo 	 */
3098e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3099e705c121SKalle Valo 	txq_id = tid_data->txq_id;
3100e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
3101e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
3102e705c121SKalle Valo 	old_state = tid_data->state;
3103e705c121SKalle Valo 	tid_data->state = IWL_AGG_OFF;
3104e705c121SKalle Valo 	mvmsta->agg_tids &= ~BIT(tid);
3105e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3106e705c121SKalle Valo 
31074b387906SAvraham Stern 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3108e705c121SKalle Valo 
3109e705c121SKalle Valo 	if (old_state >= IWL_AGG_ON) {
3110e705c121SKalle Valo 		iwl_mvm_drain_sta(mvm, mvmsta, true);
3111d167e81aSMordechai Goodstein 
3112d167e81aSMordechai Goodstein 		if (iwl_mvm_has_new_tx_api(mvm)) {
3113d167e81aSMordechai Goodstein 			if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
3114d167e81aSMordechai Goodstein 						   BIT(tid), 0))
3115d167e81aSMordechai Goodstein 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3116d167e81aSMordechai Goodstein 			iwl_trans_wait_txq_empty(mvm->trans, txq_id);
3117d167e81aSMordechai Goodstein 		} else {
3118e705c121SKalle Valo 			if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
3119e705c121SKalle Valo 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
31200b90964aSSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
3121d167e81aSMordechai Goodstein 		}
3122d6d517b7SSara Sharon 
3123e705c121SKalle Valo 		iwl_mvm_drain_sta(mvm, mvmsta, false);
3124e705c121SKalle Valo 
3125e705c121SKalle Valo 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3126e705c121SKalle Valo 	}
3127e705c121SKalle Valo 
3128e705c121SKalle Valo 	return 0;
3129e705c121SKalle Valo }
3130e705c121SKalle Valo 
3131e705c121SKalle Valo static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
3132e705c121SKalle Valo {
3133e705c121SKalle Valo 	int i, max = -1, max_offs = -1;
3134e705c121SKalle Valo 
3135e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3136e705c121SKalle Valo 
3137e705c121SKalle Valo 	/* Pick the unused key offset with the highest 'deleted'
3138e705c121SKalle Valo 	 * counter. Every time a key is deleted, all the counters
3139e705c121SKalle Valo 	 * are incremented and the one that was just deleted is
3140e705c121SKalle Valo 	 * reset to zero. Thus, the highest counter is the one
3141e705c121SKalle Valo 	 * that was deleted longest ago. Pick that one.
3142e705c121SKalle Valo 	 */
3143e705c121SKalle Valo 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3144e705c121SKalle Valo 		if (test_bit(i, mvm->fw_key_table))
3145e705c121SKalle Valo 			continue;
3146e705c121SKalle Valo 		if (mvm->fw_key_deleted[i] > max) {
3147e705c121SKalle Valo 			max = mvm->fw_key_deleted[i];
3148e705c121SKalle Valo 			max_offs = i;
3149e705c121SKalle Valo 		}
3150e705c121SKalle Valo 	}
3151e705c121SKalle Valo 
3152e705c121SKalle Valo 	if (max_offs < 0)
3153e705c121SKalle Valo 		return STA_KEY_IDX_INVALID;
3154e705c121SKalle Valo 
3155e705c121SKalle Valo 	return max_offs;
3156e705c121SKalle Valo }
3157e705c121SKalle Valo 
31585f7a1847SJohannes Berg static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
31594615fd15SEmmanuel Grumbach 					       struct ieee80211_vif *vif,
3160e705c121SKalle Valo 					       struct ieee80211_sta *sta)
3161e705c121SKalle Valo {
3162e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3163e705c121SKalle Valo 
31645f7a1847SJohannes Berg 	if (sta)
31655f7a1847SJohannes Berg 		return iwl_mvm_sta_from_mac80211(sta);
3166e705c121SKalle Valo 
3167e705c121SKalle Valo 	/*
3168e705c121SKalle Valo 	 * The device expects GTKs for station interfaces to be
3169e705c121SKalle Valo 	 * installed as GTKs for the AP station. If we have no
3170e705c121SKalle Valo 	 * station ID, then use AP's station ID.
3171e705c121SKalle Valo 	 */
3172e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
31730ae98812SSara Sharon 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
31744615fd15SEmmanuel Grumbach 		u8 sta_id = mvmvif->ap_sta_id;
31754615fd15SEmmanuel Grumbach 
31767d6a1ab6SEmmanuel Grumbach 		sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
31777d6a1ab6SEmmanuel Grumbach 					    lockdep_is_held(&mvm->mutex));
31787d6a1ab6SEmmanuel Grumbach 
31794615fd15SEmmanuel Grumbach 		/*
31804615fd15SEmmanuel Grumbach 		 * It is possible that the 'sta' parameter is NULL,
31814615fd15SEmmanuel Grumbach 		 * for example when a GTK is removed - the sta_id will then
31824615fd15SEmmanuel Grumbach 		 * be the AP ID, and no station was passed by mac80211.
31834615fd15SEmmanuel Grumbach 		 */
31847d6a1ab6SEmmanuel Grumbach 		if (IS_ERR_OR_NULL(sta))
31857d6a1ab6SEmmanuel Grumbach 			return NULL;
31867d6a1ab6SEmmanuel Grumbach 
31877d6a1ab6SEmmanuel Grumbach 		return iwl_mvm_sta_from_mac80211(sta);
31884615fd15SEmmanuel Grumbach 	}
3189e705c121SKalle Valo 
31905f7a1847SJohannes Berg 	return NULL;
3191e705c121SKalle Valo }
3192e705c121SKalle Valo 
3193e705c121SKalle Valo static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
319485aeb58cSDavid Spinadel 				u32 sta_id,
319545c458b4SSara Sharon 				struct ieee80211_key_conf *key, bool mcast,
31964615fd15SEmmanuel Grumbach 				u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
31974883145aSEmmanuel Grumbach 				u8 key_offset, bool mfp)
3198e705c121SKalle Valo {
319945c458b4SSara Sharon 	union {
320045c458b4SSara Sharon 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
320145c458b4SSara Sharon 		struct iwl_mvm_add_sta_key_cmd cmd;
320245c458b4SSara Sharon 	} u = {};
3203e705c121SKalle Valo 	__le16 key_flags;
3204e705c121SKalle Valo 	int ret;
3205e705c121SKalle Valo 	u32 status;
3206e705c121SKalle Valo 	u16 keyidx;
320745c458b4SSara Sharon 	u64 pn = 0;
320845c458b4SSara Sharon 	int i, size;
320945c458b4SSara Sharon 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
321045c458b4SSara Sharon 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3211e705c121SKalle Valo 
321285aeb58cSDavid Spinadel 	if (sta_id == IWL_MVM_INVALID_STA)
321385aeb58cSDavid Spinadel 		return -EINVAL;
321485aeb58cSDavid Spinadel 
321545c458b4SSara Sharon 	keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
3216e705c121SKalle Valo 		 STA_KEY_FLG_KEYID_MSK;
3217e705c121SKalle Valo 	key_flags = cpu_to_le16(keyidx);
3218e705c121SKalle Valo 	key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3219e705c121SKalle Valo 
322045c458b4SSara Sharon 	switch (key->cipher) {
3221e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
3222e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
322345c458b4SSara Sharon 		if (new_api) {
322445c458b4SSara Sharon 			memcpy((void *)&u.cmd.tx_mic_key,
322545c458b4SSara Sharon 			       &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
322645c458b4SSara Sharon 			       IWL_MIC_KEY_SIZE);
322745c458b4SSara Sharon 
322845c458b4SSara Sharon 			memcpy((void *)&u.cmd.rx_mic_key,
322945c458b4SSara Sharon 			       &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
323045c458b4SSara Sharon 			       IWL_MIC_KEY_SIZE);
323145c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
323245c458b4SSara Sharon 
323345c458b4SSara Sharon 		} else {
323445c458b4SSara Sharon 			u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3235e705c121SKalle Valo 			for (i = 0; i < 5; i++)
323645c458b4SSara Sharon 				u.cmd_v1.tkip_rx_ttak[i] =
323745c458b4SSara Sharon 					cpu_to_le16(tkip_p1k[i]);
323845c458b4SSara Sharon 		}
323945c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3240e705c121SKalle Valo 		break;
3241e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
3242e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
324345c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
324445c458b4SSara Sharon 		if (new_api)
324545c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
3246e705c121SKalle Valo 		break;
3247e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
3248e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
32495a2abdcaSGustavo A. R. Silva 		fallthrough;
3250e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
3251e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
325245c458b4SSara Sharon 		memcpy(u.cmd.common.key + 3, key->key, key->keylen);
3253e705c121SKalle Valo 		break;
32542a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
32552a53d166SAyala Beker 		key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
32565a2abdcaSGustavo A. R. Silva 		fallthrough;
32572a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
32582a53d166SAyala Beker 		key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
325945c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
326045c458b4SSara Sharon 		if (new_api)
326145c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
32622a53d166SAyala Beker 		break;
3263e705c121SKalle Valo 	default:
3264e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
326545c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3266e705c121SKalle Valo 	}
3267e705c121SKalle Valo 
3268e705c121SKalle Valo 	if (mcast)
3269e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
32704883145aSEmmanuel Grumbach 	if (mfp)
32714883145aSEmmanuel Grumbach 		key_flags |= cpu_to_le16(STA_KEY_MFP);
3272e705c121SKalle Valo 
327345c458b4SSara Sharon 	u.cmd.common.key_offset = key_offset;
327445c458b4SSara Sharon 	u.cmd.common.key_flags = key_flags;
327585aeb58cSDavid Spinadel 	u.cmd.common.sta_id = sta_id;
327645c458b4SSara Sharon 
327745c458b4SSara Sharon 	if (new_api) {
327845c458b4SSara Sharon 		u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
327945c458b4SSara Sharon 		size = sizeof(u.cmd);
328045c458b4SSara Sharon 	} else {
328145c458b4SSara Sharon 		size = sizeof(u.cmd_v1);
328245c458b4SSara Sharon 	}
3283e705c121SKalle Valo 
3284e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
3285e705c121SKalle Valo 	if (cmd_flags & CMD_ASYNC)
328645c458b4SSara Sharon 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
328745c458b4SSara Sharon 					   &u.cmd);
3288e705c121SKalle Valo 	else
328945c458b4SSara Sharon 		ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
329045c458b4SSara Sharon 						  &u.cmd, &status);
3291e705c121SKalle Valo 
3292e705c121SKalle Valo 	switch (status) {
3293e705c121SKalle Valo 	case ADD_STA_SUCCESS:
3294e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3295e705c121SKalle Valo 		break;
3296e705c121SKalle Valo 	default:
3297e705c121SKalle Valo 		ret = -EIO;
3298e705c121SKalle Valo 		IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3299e705c121SKalle Valo 		break;
3300e705c121SKalle Valo 	}
3301e705c121SKalle Valo 
3302e705c121SKalle Valo 	return ret;
3303e705c121SKalle Valo }
3304e705c121SKalle Valo 
3305e705c121SKalle Valo static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3306e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
3307e705c121SKalle Valo 				 u8 sta_id, bool remove_key)
3308e705c121SKalle Valo {
3309e705c121SKalle Valo 	struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3310e705c121SKalle Valo 
3311e705c121SKalle Valo 	/* verify the key details match the required command's expectations */
33128e160ab8SAyala Beker 	if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
33138e160ab8SAyala Beker 		    (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
33148e160ab8SAyala Beker 		    (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
33158e160ab8SAyala Beker 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
33168e160ab8SAyala Beker 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
33178e160ab8SAyala Beker 		return -EINVAL;
33188e160ab8SAyala Beker 
33198e160ab8SAyala Beker 	if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
33208e160ab8SAyala Beker 		    keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3321e705c121SKalle Valo 		return -EINVAL;
3322e705c121SKalle Valo 
3323e705c121SKalle Valo 	igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3324e705c121SKalle Valo 	igtk_cmd.sta_id = cpu_to_le32(sta_id);
3325e705c121SKalle Valo 
3326e705c121SKalle Valo 	if (remove_key) {
3327197288d5SLuca Coelho 		/* This is a valid situation for IGTK */
3328197288d5SLuca Coelho 		if (sta_id == IWL_MVM_INVALID_STA)
3329197288d5SLuca Coelho 			return 0;
3330197288d5SLuca Coelho 
3331e705c121SKalle Valo 		igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3332e705c121SKalle Valo 	} else {
3333e705c121SKalle Valo 		struct ieee80211_key_seq seq;
3334e705c121SKalle Valo 		const u8 *pn;
3335e705c121SKalle Valo 
3336aa950524SAyala Beker 		switch (keyconf->cipher) {
3337aa950524SAyala Beker 		case WLAN_CIPHER_SUITE_AES_CMAC:
3338aa950524SAyala Beker 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3339aa950524SAyala Beker 			break;
33408e160ab8SAyala Beker 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
33418e160ab8SAyala Beker 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
33428e160ab8SAyala Beker 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
33438e160ab8SAyala Beker 			break;
3344aa950524SAyala Beker 		default:
3345aa950524SAyala Beker 			return -EINVAL;
3346aa950524SAyala Beker 		}
3347aa950524SAyala Beker 
33488e160ab8SAyala Beker 		memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
33498e160ab8SAyala Beker 		if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
33508e160ab8SAyala Beker 			igtk_cmd.ctrl_flags |=
33518e160ab8SAyala Beker 				cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3352e705c121SKalle Valo 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3353e705c121SKalle Valo 		pn = seq.aes_cmac.pn;
3354e705c121SKalle Valo 		igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3355e705c121SKalle Valo 						       ((u64) pn[4] << 8) |
3356e705c121SKalle Valo 						       ((u64) pn[3] << 16) |
3357e705c121SKalle Valo 						       ((u64) pn[2] << 24) |
3358e705c121SKalle Valo 						       ((u64) pn[1] << 32) |
3359e705c121SKalle Valo 						       ((u64) pn[0] << 40));
3360e705c121SKalle Valo 	}
3361e705c121SKalle Valo 
3362e705c121SKalle Valo 	IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
3363e705c121SKalle Valo 		       remove_key ? "removing" : "installing",
3364e705c121SKalle Valo 		       igtk_cmd.sta_id);
3365e705c121SKalle Valo 
33668e160ab8SAyala Beker 	if (!iwl_mvm_has_new_rx_api(mvm)) {
33678e160ab8SAyala Beker 		struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
33688e160ab8SAyala Beker 			.ctrl_flags = igtk_cmd.ctrl_flags,
33698e160ab8SAyala Beker 			.key_id = igtk_cmd.key_id,
33708e160ab8SAyala Beker 			.sta_id = igtk_cmd.sta_id,
33718e160ab8SAyala Beker 			.receive_seq_cnt = igtk_cmd.receive_seq_cnt
33728e160ab8SAyala Beker 		};
33738e160ab8SAyala Beker 
33748e160ab8SAyala Beker 		memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
33758e160ab8SAyala Beker 		       ARRAY_SIZE(igtk_cmd_v1.igtk));
33768e160ab8SAyala Beker 		return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
33778e160ab8SAyala Beker 					    sizeof(igtk_cmd_v1), &igtk_cmd_v1);
33788e160ab8SAyala Beker 	}
3379e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3380e705c121SKalle Valo 				    sizeof(igtk_cmd), &igtk_cmd);
3381e705c121SKalle Valo }
3382e705c121SKalle Valo 
3383e705c121SKalle Valo 
3384e705c121SKalle Valo static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3385e705c121SKalle Valo 				       struct ieee80211_vif *vif,
3386e705c121SKalle Valo 				       struct ieee80211_sta *sta)
3387e705c121SKalle Valo {
3388e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3389e705c121SKalle Valo 
3390e705c121SKalle Valo 	if (sta)
3391e705c121SKalle Valo 		return sta->addr;
3392e705c121SKalle Valo 
3393e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
33940ae98812SSara Sharon 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3395e705c121SKalle Valo 		u8 sta_id = mvmvif->ap_sta_id;
3396e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3397e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
3398e705c121SKalle Valo 		return sta->addr;
3399e705c121SKalle Valo 	}
3400e705c121SKalle Valo 
3401e705c121SKalle Valo 
3402e705c121SKalle Valo 	return NULL;
3403e705c121SKalle Valo }
3404e705c121SKalle Valo 
3405e705c121SKalle Valo static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3406e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3407e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3408e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
34094615fd15SEmmanuel Grumbach 				 u8 key_offset,
3410e705c121SKalle Valo 				 bool mcast)
3411e705c121SKalle Valo {
3412e705c121SKalle Valo 	int ret;
3413e705c121SKalle Valo 	const u8 *addr;
3414e705c121SKalle Valo 	struct ieee80211_key_seq seq;
3415e705c121SKalle Valo 	u16 p1k[5];
341685aeb58cSDavid Spinadel 	u32 sta_id;
34174883145aSEmmanuel Grumbach 	bool mfp = false;
341885aeb58cSDavid Spinadel 
341985aeb58cSDavid Spinadel 	if (sta) {
342085aeb58cSDavid Spinadel 		struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
342185aeb58cSDavid Spinadel 
342285aeb58cSDavid Spinadel 		sta_id = mvm_sta->sta_id;
34234883145aSEmmanuel Grumbach 		mfp = sta->mfp;
342485aeb58cSDavid Spinadel 	} else if (vif->type == NL80211_IFTYPE_AP &&
342585aeb58cSDavid Spinadel 		   !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
342685aeb58cSDavid Spinadel 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
342785aeb58cSDavid Spinadel 
342885aeb58cSDavid Spinadel 		sta_id = mvmvif->mcast_sta.sta_id;
342985aeb58cSDavid Spinadel 	} else {
343085aeb58cSDavid Spinadel 		IWL_ERR(mvm, "Failed to find station id\n");
343185aeb58cSDavid Spinadel 		return -EINVAL;
343285aeb58cSDavid Spinadel 	}
3433e705c121SKalle Valo 
3434e705c121SKalle Valo 	switch (keyconf->cipher) {
3435e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
3436e705c121SKalle Valo 		addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3437e705c121SKalle Valo 		/* get phase 1 key from mac80211 */
3438e705c121SKalle Valo 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3439e705c121SKalle Valo 		ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
344085aeb58cSDavid Spinadel 		ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
34414883145aSEmmanuel Grumbach 					   seq.tkip.iv32, p1k, 0, key_offset,
34424883145aSEmmanuel Grumbach 					   mfp);
3443e705c121SKalle Valo 		break;
3444e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
3445e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
3446e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
34472a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
34482a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
344985aeb58cSDavid Spinadel 		ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
34504883145aSEmmanuel Grumbach 					   0, NULL, 0, key_offset, mfp);
3451e705c121SKalle Valo 		break;
3452e705c121SKalle Valo 	default:
345385aeb58cSDavid Spinadel 		ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
34544883145aSEmmanuel Grumbach 					   0, NULL, 0, key_offset, mfp);
3455e705c121SKalle Valo 	}
3456e705c121SKalle Valo 
3457e705c121SKalle Valo 	return ret;
3458e705c121SKalle Valo }
3459e705c121SKalle Valo 
3460e705c121SKalle Valo int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3461e705c121SKalle Valo 			struct ieee80211_vif *vif,
3462e705c121SKalle Valo 			struct ieee80211_sta *sta,
3463e705c121SKalle Valo 			struct ieee80211_key_conf *keyconf,
34644615fd15SEmmanuel Grumbach 			u8 key_offset)
3465e705c121SKalle Valo {
3466e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
34675f7a1847SJohannes Berg 	struct iwl_mvm_sta *mvm_sta;
346885aeb58cSDavid Spinadel 	u8 sta_id = IWL_MVM_INVALID_STA;
3469e705c121SKalle Valo 	int ret;
3470e705c121SKalle Valo 	static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3471e705c121SKalle Valo 
3472e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3473e705c121SKalle Valo 
347485aeb58cSDavid Spinadel 	if (vif->type != NL80211_IFTYPE_AP ||
347585aeb58cSDavid Spinadel 	    keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3476e705c121SKalle Valo 		/* Get the station id from the mvm local station table */
34775f7a1847SJohannes Berg 		mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
34785f7a1847SJohannes Berg 		if (!mvm_sta) {
34795f7a1847SJohannes Berg 			IWL_ERR(mvm, "Failed to find station\n");
3480e705c121SKalle Valo 			return -EINVAL;
3481e705c121SKalle Valo 		}
34825f7a1847SJohannes Berg 		sta_id = mvm_sta->sta_id;
3483e705c121SKalle Valo 
3484e705c121SKalle Valo 		/*
3485e705c121SKalle Valo 		 * It is possible that the 'sta' parameter is NULL, and thus
348685aeb58cSDavid Spinadel 		 * there is a need to retrieve the sta from the local station
348785aeb58cSDavid Spinadel 		 * table.
3488e705c121SKalle Valo 		 */
3489e705c121SKalle Valo 		if (!sta) {
349085aeb58cSDavid Spinadel 			sta = rcu_dereference_protected(
349185aeb58cSDavid Spinadel 				mvm->fw_id_to_mac_id[sta_id],
3492e705c121SKalle Valo 				lockdep_is_held(&mvm->mutex));
3493e705c121SKalle Valo 			if (IS_ERR_OR_NULL(sta)) {
3494e705c121SKalle Valo 				IWL_ERR(mvm, "Invalid station id\n");
3495e705c121SKalle Valo 				return -EINVAL;
3496e705c121SKalle Valo 			}
3497e705c121SKalle Valo 		}
3498e705c121SKalle Valo 
3499e705c121SKalle Valo 		if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3500e705c121SKalle Valo 			return -EINVAL;
3501e829b17cSBeni Lev 	} else {
3502e829b17cSBeni Lev 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3503e829b17cSBeni Lev 
3504e829b17cSBeni Lev 		sta_id = mvmvif->mcast_sta.sta_id;
3505e829b17cSBeni Lev 	}
3506e829b17cSBeni Lev 
3507e829b17cSBeni Lev 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3508e829b17cSBeni Lev 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3509e829b17cSBeni Lev 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3510e829b17cSBeni Lev 		ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3511e829b17cSBeni Lev 		goto end;
351285aeb58cSDavid Spinadel 	}
3513e705c121SKalle Valo 
35144615fd15SEmmanuel Grumbach 	/* If the key_offset is not pre-assigned, we need to find a
35154615fd15SEmmanuel Grumbach 	 * new offset to use.  In normal cases, the offset is not
35164615fd15SEmmanuel Grumbach 	 * pre-assigned, but during HW_RESTART we want to reuse the
35174615fd15SEmmanuel Grumbach 	 * same indices, so we pass them when this function is called.
35184615fd15SEmmanuel Grumbach 	 *
35194615fd15SEmmanuel Grumbach 	 * In D3 entry, we need to hardcoded the indices (because the
35204615fd15SEmmanuel Grumbach 	 * firmware hardcodes the PTK offset to 0).  In this case, we
35214615fd15SEmmanuel Grumbach 	 * need to make sure we don't overwrite the hw_key_idx in the
35224615fd15SEmmanuel Grumbach 	 * keyconf structure, because otherwise we cannot configure
35234615fd15SEmmanuel Grumbach 	 * the original ones back when resuming.
3524e705c121SKalle Valo 	 */
35254615fd15SEmmanuel Grumbach 	if (key_offset == STA_KEY_IDX_INVALID) {
35264615fd15SEmmanuel Grumbach 		key_offset  = iwl_mvm_set_fw_key_idx(mvm);
35274615fd15SEmmanuel Grumbach 		if (key_offset == STA_KEY_IDX_INVALID)
3528e705c121SKalle Valo 			return -ENOSPC;
35294615fd15SEmmanuel Grumbach 		keyconf->hw_key_idx = key_offset;
3530e705c121SKalle Valo 	}
3531e705c121SKalle Valo 
35324615fd15SEmmanuel Grumbach 	ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
35339c3deeb5SLuca Coelho 	if (ret)
3534e705c121SKalle Valo 		goto end;
3535e705c121SKalle Valo 
3536e705c121SKalle Valo 	/*
3537e705c121SKalle Valo 	 * For WEP, the same key is used for multicast and unicast. Upload it
3538e705c121SKalle Valo 	 * again, using the same key offset, and now pointing the other one
3539e705c121SKalle Valo 	 * to the same key slot (offset).
3540e705c121SKalle Valo 	 * If this fails, remove the original as well.
3541e705c121SKalle Valo 	 */
354285aeb58cSDavid Spinadel 	if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
354385aeb58cSDavid Spinadel 	     keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
354485aeb58cSDavid Spinadel 	    sta) {
35454615fd15SEmmanuel Grumbach 		ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
35464615fd15SEmmanuel Grumbach 					    key_offset, !mcast);
3547e705c121SKalle Valo 		if (ret) {
3548e705c121SKalle Valo 			__iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
35499c3deeb5SLuca Coelho 			goto end;
3550e705c121SKalle Valo 		}
3551e705c121SKalle Valo 	}
3552e705c121SKalle Valo 
35539c3deeb5SLuca Coelho 	__set_bit(key_offset, mvm->fw_key_table);
35549c3deeb5SLuca Coelho 
3555e705c121SKalle Valo end:
3556e705c121SKalle Valo 	IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3557e705c121SKalle Valo 		      keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3558e705c121SKalle Valo 		      sta ? sta->addr : zero_addr, ret);
3559e705c121SKalle Valo 	return ret;
3560e705c121SKalle Valo }
3561e705c121SKalle Valo 
3562e705c121SKalle Valo int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3563e705c121SKalle Valo 			   struct ieee80211_vif *vif,
3564e705c121SKalle Valo 			   struct ieee80211_sta *sta,
3565e705c121SKalle Valo 			   struct ieee80211_key_conf *keyconf)
3566e705c121SKalle Valo {
3567e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
35685f7a1847SJohannes Berg 	struct iwl_mvm_sta *mvm_sta;
35690ae98812SSara Sharon 	u8 sta_id = IWL_MVM_INVALID_STA;
3570e705c121SKalle Valo 	int ret, i;
3571e705c121SKalle Valo 
3572e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3573e705c121SKalle Valo 
35745f7a1847SJohannes Berg 	/* Get the station from the mvm local station table */
35755f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
357671793b7dSLuca Coelho 	if (mvm_sta)
3577cd4d23c1SIlan Peer 		sta_id = mvm_sta->sta_id;
357885aeb58cSDavid Spinadel 	else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
357985aeb58cSDavid Spinadel 		sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
358085aeb58cSDavid Spinadel 
3581e705c121SKalle Valo 
3582e705c121SKalle Valo 	IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3583e705c121SKalle Valo 		      keyconf->keyidx, sta_id);
3584e705c121SKalle Valo 
3585197288d5SLuca Coelho 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
35868e160ab8SAyala Beker 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3587197288d5SLuca Coelho 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3588e705c121SKalle Valo 		return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3589e705c121SKalle Valo 
3590e705c121SKalle Valo 	if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3591e705c121SKalle Valo 		IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3592e705c121SKalle Valo 			keyconf->hw_key_idx);
3593e705c121SKalle Valo 		return -ENOENT;
3594e705c121SKalle Valo 	}
3595e705c121SKalle Valo 
3596e705c121SKalle Valo 	/* track which key was deleted last */
3597e705c121SKalle Valo 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3598e705c121SKalle Valo 		if (mvm->fw_key_deleted[i] < U8_MAX)
3599e705c121SKalle Valo 			mvm->fw_key_deleted[i]++;
3600e705c121SKalle Valo 	}
3601e705c121SKalle Valo 	mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3602e705c121SKalle Valo 
360385aeb58cSDavid Spinadel 	if (sta && !mvm_sta) {
3604e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3605e705c121SKalle Valo 		return 0;
3606e705c121SKalle Valo 	}
3607e705c121SKalle Valo 
3608e705c121SKalle Valo 	ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3609e705c121SKalle Valo 	if (ret)
3610e705c121SKalle Valo 		return ret;
3611e705c121SKalle Valo 
3612e705c121SKalle Valo 	/* delete WEP key twice to get rid of (now useless) offset */
3613e705c121SKalle Valo 	if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3614e705c121SKalle Valo 	    keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3615e705c121SKalle Valo 		ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3616e705c121SKalle Valo 
3617e705c121SKalle Valo 	return ret;
3618e705c121SKalle Valo }
3619e705c121SKalle Valo 
3620e705c121SKalle Valo void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3621e705c121SKalle Valo 			     struct ieee80211_vif *vif,
3622e705c121SKalle Valo 			     struct ieee80211_key_conf *keyconf,
3623e705c121SKalle Valo 			     struct ieee80211_sta *sta, u32 iv32,
3624e705c121SKalle Valo 			     u16 *phase1key)
3625e705c121SKalle Valo {
3626e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta;
3627e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
36284883145aSEmmanuel Grumbach 	bool mfp = sta ? sta->mfp : false;
3629e705c121SKalle Valo 
3630e705c121SKalle Valo 	rcu_read_lock();
3631e705c121SKalle Valo 
36325f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
36335f7a1847SJohannes Berg 	if (WARN_ON_ONCE(!mvm_sta))
363412f17211SEmmanuel Grumbach 		goto unlock;
363585aeb58cSDavid Spinadel 	iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
36364883145aSEmmanuel Grumbach 			     iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx,
36374883145aSEmmanuel Grumbach 			     mfp);
363812f17211SEmmanuel Grumbach 
363912f17211SEmmanuel Grumbach  unlock:
3640e705c121SKalle Valo 	rcu_read_unlock();
3641e705c121SKalle Valo }
3642e705c121SKalle Valo 
3643e705c121SKalle Valo void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3644e705c121SKalle Valo 				struct ieee80211_sta *sta)
3645e705c121SKalle Valo {
3646e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3647e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3648e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3649e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3650e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_PS),
3651e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3652e705c121SKalle Valo 	};
3653e705c121SKalle Valo 	int ret;
3654e705c121SKalle Valo 
3655854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3656854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3657e705c121SKalle Valo 	if (ret)
3658e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3659e705c121SKalle Valo }
3660e705c121SKalle Valo 
3661e705c121SKalle Valo void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3662e705c121SKalle Valo 				       struct ieee80211_sta *sta,
3663e705c121SKalle Valo 				       enum ieee80211_frame_release_type reason,
3664e705c121SKalle Valo 				       u16 cnt, u16 tids, bool more_data,
36659a3fcf91SSara Sharon 				       bool single_sta_queue)
3666e705c121SKalle Valo {
3667e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3668e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3669e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3670e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3671e705c121SKalle Valo 		.modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3672e705c121SKalle Valo 		.sleep_tx_count = cpu_to_le16(cnt),
3673e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3674e705c121SKalle Valo 	};
3675e705c121SKalle Valo 	int tid, ret;
3676e705c121SKalle Valo 	unsigned long _tids = tids;
3677e705c121SKalle Valo 
3678e705c121SKalle Valo 	/* convert TIDs to ACs - we don't support TSPEC so that's OK
3679e705c121SKalle Valo 	 * Note that this field is reserved and unused by firmware not
3680e705c121SKalle Valo 	 * supporting GO uAPSD, so it's safe to always do this.
3681e705c121SKalle Valo 	 */
3682e705c121SKalle Valo 	for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3683e705c121SKalle Valo 		cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3684e705c121SKalle Valo 
36859a3fcf91SSara Sharon 	/* If we're releasing frames from aggregation or dqa queues then check
36869a3fcf91SSara Sharon 	 * if all the queues that we're releasing frames from, combined, have:
3687e705c121SKalle Valo 	 *  - more frames than the service period, in which case more_data
3688e705c121SKalle Valo 	 *    needs to be set
3689e705c121SKalle Valo 	 *  - fewer than 'cnt' frames, in which case we need to adjust the
3690e705c121SKalle Valo 	 *    firmware command (but do that unconditionally)
3691e705c121SKalle Valo 	 */
36929a3fcf91SSara Sharon 	if (single_sta_queue) {
3693e705c121SKalle Valo 		int remaining = cnt;
369436be0eb6SEmmanuel Grumbach 		int sleep_tx_count;
3695e705c121SKalle Valo 
3696e705c121SKalle Valo 		spin_lock_bh(&mvmsta->lock);
3697e705c121SKalle Valo 		for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3698e705c121SKalle Valo 			struct iwl_mvm_tid_data *tid_data;
3699e705c121SKalle Valo 			u16 n_queued;
3700e705c121SKalle Valo 
3701e705c121SKalle Valo 			tid_data = &mvmsta->tid_data[tid];
3702e705c121SKalle Valo 
3703dd32162dSLiad Kaufman 			n_queued = iwl_mvm_tid_queued(mvm, tid_data);
3704e705c121SKalle Valo 			if (n_queued > remaining) {
3705e705c121SKalle Valo 				more_data = true;
3706e705c121SKalle Valo 				remaining = 0;
3707e705c121SKalle Valo 				break;
3708e705c121SKalle Valo 			}
3709e705c121SKalle Valo 			remaining -= n_queued;
3710e705c121SKalle Valo 		}
371136be0eb6SEmmanuel Grumbach 		sleep_tx_count = cnt - remaining;
371236be0eb6SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
371336be0eb6SEmmanuel Grumbach 			mvmsta->sleep_tx_count = sleep_tx_count;
3714e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
3715e705c121SKalle Valo 
371636be0eb6SEmmanuel Grumbach 		cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3717e705c121SKalle Valo 		if (WARN_ON(cnt - remaining == 0)) {
3718e705c121SKalle Valo 			ieee80211_sta_eosp(sta);
3719e705c121SKalle Valo 			return;
3720e705c121SKalle Valo 		}
3721e705c121SKalle Valo 	}
3722e705c121SKalle Valo 
3723e705c121SKalle Valo 	/* Note: this is ignored by firmware not supporting GO uAPSD */
3724e705c121SKalle Valo 	if (more_data)
3725ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
3726e705c121SKalle Valo 
3727e705c121SKalle Valo 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3728e705c121SKalle Valo 		mvmsta->next_status_eosp = true;
3729ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
3730e705c121SKalle Valo 	} else {
3731ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
3732e705c121SKalle Valo 	}
3733e705c121SKalle Valo 
3734156f92f2SEmmanuel Grumbach 	/* block the Tx queues until the FW updated the sleep Tx count */
3735156f92f2SEmmanuel Grumbach 	iwl_trans_block_txq_ptrs(mvm->trans, true);
3736156f92f2SEmmanuel Grumbach 
3737156f92f2SEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3738156f92f2SEmmanuel Grumbach 				   CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3739854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3740e705c121SKalle Valo 	if (ret)
3741e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3742e705c121SKalle Valo }
3743e705c121SKalle Valo 
3744e705c121SKalle Valo void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3745e705c121SKalle Valo 			   struct iwl_rx_cmd_buffer *rxb)
3746e705c121SKalle Valo {
3747e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3748e705c121SKalle Valo 	struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3749e705c121SKalle Valo 	struct ieee80211_sta *sta;
3750e705c121SKalle Valo 	u32 sta_id = le32_to_cpu(notif->sta_id);
3751e705c121SKalle Valo 
3752be9ae34eSNathan Errera 	if (WARN_ON_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations))
3753e705c121SKalle Valo 		return;
3754e705c121SKalle Valo 
3755e705c121SKalle Valo 	rcu_read_lock();
3756e705c121SKalle Valo 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3757e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(sta))
3758e705c121SKalle Valo 		ieee80211_sta_eosp(sta);
3759e705c121SKalle Valo 	rcu_read_unlock();
3760e705c121SKalle Valo }
3761e705c121SKalle Valo 
3762e705c121SKalle Valo void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3763e705c121SKalle Valo 				   struct iwl_mvm_sta *mvmsta, bool disable)
3764e705c121SKalle Valo {
3765e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3766e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3767e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3768e705c121SKalle Valo 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3769e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3770e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3771e705c121SKalle Valo 	};
3772e705c121SKalle Valo 	int ret;
3773e705c121SKalle Valo 
3774854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3775854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3776e705c121SKalle Valo 	if (ret)
3777e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3778e705c121SKalle Valo }
3779e705c121SKalle Valo 
3780e705c121SKalle Valo void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3781e705c121SKalle Valo 				      struct ieee80211_sta *sta,
3782e705c121SKalle Valo 				      bool disable)
3783e705c121SKalle Valo {
3784e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3785e705c121SKalle Valo 
3786e705c121SKalle Valo 	spin_lock_bh(&mvm_sta->lock);
3787e705c121SKalle Valo 
3788e705c121SKalle Valo 	if (mvm_sta->disable_tx == disable) {
3789e705c121SKalle Valo 		spin_unlock_bh(&mvm_sta->lock);
3790e705c121SKalle Valo 		return;
3791e705c121SKalle Valo 	}
3792e705c121SKalle Valo 
3793e705c121SKalle Valo 	mvm_sta->disable_tx = disable;
3794e705c121SKalle Valo 
3795c8f54701SJohannes Berg 	/* Tell mac80211 to start/stop queuing tx for this station */
3796e705c121SKalle Valo 	ieee80211_sta_block_awake(mvm->hw, sta, disable);
3797e705c121SKalle Valo 
3798e705c121SKalle Valo 	iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3799e705c121SKalle Valo 
3800e705c121SKalle Valo 	spin_unlock_bh(&mvm_sta->lock);
3801e705c121SKalle Valo }
3802e705c121SKalle Valo 
3803ced19f26SSara Sharon static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
3804ced19f26SSara Sharon 					      struct iwl_mvm_vif *mvmvif,
3805ced19f26SSara Sharon 					      struct iwl_mvm_int_sta *sta,
3806ced19f26SSara Sharon 					      bool disable)
3807ced19f26SSara Sharon {
3808ced19f26SSara Sharon 	u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
3809ced19f26SSara Sharon 	struct iwl_mvm_add_sta_cmd cmd = {
3810ced19f26SSara Sharon 		.add_modify = STA_MODE_MODIFY,
3811ced19f26SSara Sharon 		.sta_id = sta->sta_id,
3812ced19f26SSara Sharon 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3813ced19f26SSara Sharon 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3814ced19f26SSara Sharon 		.mac_id_n_color = cpu_to_le32(id),
3815ced19f26SSara Sharon 	};
3816ced19f26SSara Sharon 	int ret;
3817ced19f26SSara Sharon 
3818ced19f26SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, 0,
3819ced19f26SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3820ced19f26SSara Sharon 	if (ret)
3821ced19f26SSara Sharon 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3822ced19f26SSara Sharon }
3823ced19f26SSara Sharon 
3824e705c121SKalle Valo void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3825e705c121SKalle Valo 				       struct iwl_mvm_vif *mvmvif,
3826e705c121SKalle Valo 				       bool disable)
3827e705c121SKalle Valo {
3828e705c121SKalle Valo 	struct ieee80211_sta *sta;
3829e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta;
3830e705c121SKalle Valo 	int i;
3831e705c121SKalle Valo 
3832e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3833e705c121SKalle Valo 
3834e705c121SKalle Valo 	/* Block/unblock all the stations of the given mvmvif */
3835be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
3836e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3837e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
3838e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
3839e705c121SKalle Valo 			continue;
3840e705c121SKalle Valo 
3841e705c121SKalle Valo 		mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3842e705c121SKalle Valo 		if (mvm_sta->mac_id_n_color !=
3843e705c121SKalle Valo 		    FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3844e705c121SKalle Valo 			continue;
3845e705c121SKalle Valo 
3846e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3847e705c121SKalle Valo 	}
3848ced19f26SSara Sharon 
3849ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3850ced19f26SSara Sharon 		return;
3851ced19f26SSara Sharon 
3852ced19f26SSara Sharon 	/* Need to block/unblock also multicast station */
3853ced19f26SSara Sharon 	if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
3854ced19f26SSara Sharon 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3855ced19f26SSara Sharon 						  &mvmvif->mcast_sta, disable);
3856ced19f26SSara Sharon 
3857ced19f26SSara Sharon 	/*
3858ced19f26SSara Sharon 	 * Only unblock the broadcast station (FW blocks it for immediate
3859ced19f26SSara Sharon 	 * quiet, not the driver)
3860ced19f26SSara Sharon 	 */
3861ced19f26SSara Sharon 	if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
3862ced19f26SSara Sharon 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3863ced19f26SSara Sharon 						  &mvmvif->bcast_sta, disable);
3864e705c121SKalle Valo }
3865e705c121SKalle Valo 
3866e705c121SKalle Valo void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3867e705c121SKalle Valo {
3868e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3869e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
3870e705c121SKalle Valo 
3871e705c121SKalle Valo 	rcu_read_lock();
3872e705c121SKalle Valo 
3873e705c121SKalle Valo 	mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3874e705c121SKalle Valo 
3875e705c121SKalle Valo 	if (!WARN_ON(!mvmsta))
3876e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3877e705c121SKalle Valo 
3878e705c121SKalle Valo 	rcu_read_unlock();
3879e705c121SKalle Valo }
3880dd32162dSLiad Kaufman 
3881dd32162dSLiad Kaufman u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
3882dd32162dSLiad Kaufman {
3883dd32162dSLiad Kaufman 	u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3884dd32162dSLiad Kaufman 
3885dd32162dSLiad Kaufman 	/*
38862f7a3863SLuca Coelho 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
3887dd32162dSLiad Kaufman 	 * to align the wrap around of ssn so we compare relevant values.
3888dd32162dSLiad Kaufman 	 */
3889286ca8ebSLuca Coelho 	if (mvm->trans->trans_cfg->gen2)
3890dd32162dSLiad Kaufman 		sn &= 0xff;
3891dd32162dSLiad Kaufman 
3892dd32162dSLiad Kaufman 	return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
3893dd32162dSLiad Kaufman }
3894be82ecd3SAvraham Stern 
3895be82ecd3SAvraham Stern int iwl_mvm_add_pasn_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3896be82ecd3SAvraham Stern 			 struct iwl_mvm_int_sta *sta, u8 *addr, u32 cipher,
3897be82ecd3SAvraham Stern 			 u8 *key, u32 key_len)
3898be82ecd3SAvraham Stern {
3899be82ecd3SAvraham Stern 	int ret;
3900be82ecd3SAvraham Stern 	u16 queue;
3901be82ecd3SAvraham Stern 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3902be82ecd3SAvraham Stern 	struct ieee80211_key_conf *keyconf;
3903be82ecd3SAvraham Stern 
3904be82ecd3SAvraham Stern 	ret = iwl_mvm_allocate_int_sta(mvm, sta, 0,
3905be82ecd3SAvraham Stern 				       NL80211_IFTYPE_UNSPECIFIED,
3906be82ecd3SAvraham Stern 				       IWL_STA_LINK);
3907be82ecd3SAvraham Stern 	if (ret)
3908be82ecd3SAvraham Stern 		return ret;
3909be82ecd3SAvraham Stern 
3910be82ecd3SAvraham Stern 	ret = iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
3911be82ecd3SAvraham Stern 					     addr, sta, &queue,
3912be82ecd3SAvraham Stern 					     IWL_MVM_TX_FIFO_BE);
3913be82ecd3SAvraham Stern 	if (ret)
3914be82ecd3SAvraham Stern 		goto out;
3915be82ecd3SAvraham Stern 
3916be82ecd3SAvraham Stern 	keyconf = kzalloc(sizeof(*keyconf) + key_len, GFP_KERNEL);
3917be82ecd3SAvraham Stern 	if (!keyconf) {
3918be82ecd3SAvraham Stern 		ret = -ENOBUFS;
3919be82ecd3SAvraham Stern 		goto out;
3920be82ecd3SAvraham Stern 	}
3921be82ecd3SAvraham Stern 
3922be82ecd3SAvraham Stern 	keyconf->cipher = cipher;
3923be82ecd3SAvraham Stern 	memcpy(keyconf->key, key, key_len);
3924be82ecd3SAvraham Stern 	keyconf->keylen = key_len;
3925be82ecd3SAvraham Stern 
3926be82ecd3SAvraham Stern 	ret = iwl_mvm_send_sta_key(mvm, sta->sta_id, keyconf, false,
3927be82ecd3SAvraham Stern 				   0, NULL, 0, 0, true);
3928be82ecd3SAvraham Stern 	kfree(keyconf);
3929be82ecd3SAvraham Stern 	return 0;
3930be82ecd3SAvraham Stern out:
3931be82ecd3SAvraham Stern 	iwl_mvm_dealloc_int_sta(mvm, sta);
3932be82ecd3SAvraham Stern 	return ret;
3933be82ecd3SAvraham Stern }
3934