18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
3*3538c809SLuca Coelho  * Copyright (C) 2012-2015, 2018-2022 Intel Corporation
48e99ea8dSJohannes Berg  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
58e99ea8dSJohannes Berg  * Copyright (C) 2016-2017 Intel Deutschland GmbH
68e99ea8dSJohannes Berg  */
7e705c121SKalle Valo #include <net/mac80211.h>
8e705c121SKalle Valo 
9e705c121SKalle Valo #include "mvm.h"
10e705c121SKalle Valo #include "sta.h"
11e705c121SKalle Valo #include "rs.h"
12e705c121SKalle Valo 
13854c5705SSara Sharon /*
14854c5705SSara Sharon  * New version of ADD_STA_sta command added new fields at the end of the
15854c5705SSara Sharon  * structure, so sending the size of the relevant API's structure is enough to
16854c5705SSara Sharon  * support both API versions.
17854c5705SSara Sharon  */
18854c5705SSara Sharon static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
19854c5705SSara Sharon {
20ced19f26SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) ||
21ced19f26SSara Sharon 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
22ced19f26SSara Sharon 		return sizeof(struct iwl_mvm_add_sta_cmd);
23ced19f26SSara Sharon 	else
24ced19f26SSara Sharon 		return sizeof(struct iwl_mvm_add_sta_cmd_v7);
25854c5705SSara Sharon }
26854c5705SSara Sharon 
27e705c121SKalle Valo static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
28e705c121SKalle Valo 				    enum nl80211_iftype iftype)
29e705c121SKalle Valo {
30e705c121SKalle Valo 	int sta_id;
31e705c121SKalle Valo 	u32 reserved_ids = 0;
32e705c121SKalle Valo 
33be9ae34eSNathan Errera 	BUILD_BUG_ON(IWL_MVM_STATION_COUNT_MAX > 32);
34e705c121SKalle Valo 	WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
35e705c121SKalle Valo 
36e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
37e705c121SKalle Valo 
38e705c121SKalle Valo 	/* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
39e705c121SKalle Valo 	if (iftype != NL80211_IFTYPE_STATION)
40e705c121SKalle Valo 		reserved_ids = BIT(0);
41e705c121SKalle Valo 
42e705c121SKalle Valo 	/* Don't take rcu_read_lock() since we are protected by mvm->mutex */
43be9ae34eSNathan Errera 	for (sta_id = 0; sta_id < mvm->fw->ucode_capa.num_stations; sta_id++) {
44e705c121SKalle Valo 		if (BIT(sta_id) & reserved_ids)
45e705c121SKalle Valo 			continue;
46e705c121SKalle Valo 
47e705c121SKalle Valo 		if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
48e705c121SKalle Valo 					       lockdep_is_held(&mvm->mutex)))
49e705c121SKalle Valo 			return sta_id;
50e705c121SKalle Valo 	}
510ae98812SSara Sharon 	return IWL_MVM_INVALID_STA;
52e705c121SKalle Valo }
53e705c121SKalle Valo 
54e705c121SKalle Valo /* send station add/update command to firmware */
55e705c121SKalle Valo int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
5624afba76SLiad Kaufman 			   bool update, unsigned int flags)
57e705c121SKalle Valo {
58e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
59e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd add_sta_cmd = {
60e705c121SKalle Valo 		.sta_id = mvm_sta->sta_id,
61e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
62e705c121SKalle Valo 		.add_modify = update ? 1 : 0,
63e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
648addabf8SNaftali Goldstein 						 STA_FLG_MIMO_EN_MSK |
658addabf8SNaftali Goldstein 						 STA_FLG_RTS_MIMO_PROT),
66cf0cda19SLiad Kaufman 		.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
67e705c121SKalle Valo 	};
68e705c121SKalle Valo 	int ret;
69e705c121SKalle Valo 	u32 status;
70e705c121SKalle Valo 	u32 agg_size = 0, mpdu_dens = 0;
71e705c121SKalle Valo 
72ced19f26SSara Sharon 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
73ced19f26SSara Sharon 		add_sta_cmd.station_type = mvm_sta->sta_type;
74ced19f26SSara Sharon 
7524afba76SLiad Kaufman 	if (!update || (flags & STA_MODIFY_QUEUES)) {
76e705c121SKalle Valo 		memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
7724afba76SLiad Kaufman 
78bb49701bSSara Sharon 		if (!iwl_mvm_has_new_tx_api(mvm)) {
79bb49701bSSara Sharon 			add_sta_cmd.tfd_queue_msk =
80bb49701bSSara Sharon 				cpu_to_le32(mvm_sta->tfd_queue_msk);
81bb49701bSSara Sharon 
8224afba76SLiad Kaufman 			if (flags & STA_MODIFY_QUEUES)
8324afba76SLiad Kaufman 				add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
84bb49701bSSara Sharon 		} else {
85bb49701bSSara Sharon 			WARN_ON(flags & STA_MODIFY_QUEUES);
86bb49701bSSara Sharon 		}
87e705c121SKalle Valo 	}
88e705c121SKalle Valo 
89e705c121SKalle Valo 	switch (sta->bandwidth) {
905dca295dSIlan Peer 	case IEEE80211_STA_RX_BW_320:
91e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_160:
92e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
935a2abdcaSGustavo A. R. Silva 		fallthrough;
94e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_80:
95e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
965a2abdcaSGustavo A. R. Silva 		fallthrough;
97e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_40:
98e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
995a2abdcaSGustavo A. R. Silva 		fallthrough;
100e705c121SKalle Valo 	case IEEE80211_STA_RX_BW_20:
101e705c121SKalle Valo 		if (sta->ht_cap.ht_supported)
102e705c121SKalle Valo 			add_sta_cmd.station_flags |=
103e705c121SKalle Valo 				cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
104e705c121SKalle Valo 		break;
105e705c121SKalle Valo 	}
106e705c121SKalle Valo 
107e705c121SKalle Valo 	switch (sta->rx_nss) {
108e705c121SKalle Valo 	case 1:
109e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
110e705c121SKalle Valo 		break;
111e705c121SKalle Valo 	case 2:
112e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
113e705c121SKalle Valo 		break;
114e705c121SKalle Valo 	case 3 ... 8:
115e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
116e705c121SKalle Valo 		break;
117e705c121SKalle Valo 	}
118e705c121SKalle Valo 
119e705c121SKalle Valo 	switch (sta->smps_mode) {
120e705c121SKalle Valo 	case IEEE80211_SMPS_AUTOMATIC:
121e705c121SKalle Valo 	case IEEE80211_SMPS_NUM_MODES:
122e705c121SKalle Valo 		WARN_ON(1);
123e705c121SKalle Valo 		break;
124e705c121SKalle Valo 	case IEEE80211_SMPS_STATIC:
125e705c121SKalle Valo 		/* override NSS */
126e705c121SKalle Valo 		add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
127e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
128e705c121SKalle Valo 		break;
129e705c121SKalle Valo 	case IEEE80211_SMPS_DYNAMIC:
130e705c121SKalle Valo 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
131e705c121SKalle Valo 		break;
132e705c121SKalle Valo 	case IEEE80211_SMPS_OFF:
133e705c121SKalle Valo 		/* nothing */
134e705c121SKalle Valo 		break;
135e705c121SKalle Valo 	}
136e705c121SKalle Valo 
137e705c121SKalle Valo 	if (sta->ht_cap.ht_supported) {
138e705c121SKalle Valo 		add_sta_cmd.station_flags_msk |=
139e705c121SKalle Valo 			cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
140e705c121SKalle Valo 				    STA_FLG_AGG_MPDU_DENS_MSK);
141e705c121SKalle Valo 
142e705c121SKalle Valo 		mpdu_dens = sta->ht_cap.ampdu_density;
143e705c121SKalle Valo 	}
144e705c121SKalle Valo 
145eae94cf8SLuca Coelho 	if (mvm_sta->vif->bss_conf.chandef.chan->band == NL80211_BAND_6GHZ) {
146eae94cf8SLuca Coelho 		add_sta_cmd.station_flags_msk |=
147eae94cf8SLuca Coelho 			cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
148eae94cf8SLuca Coelho 				    STA_FLG_AGG_MPDU_DENS_MSK);
149c8a2e7a2SMordechay Goodstein 
150eae94cf8SLuca Coelho 		mpdu_dens = le16_get_bits(sta->he_6ghz_capa.capa,
151eae94cf8SLuca Coelho 					  IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
152eae94cf8SLuca Coelho 		agg_size = le16_get_bits(sta->he_6ghz_capa.capa,
153eae94cf8SLuca Coelho 				IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
154eae94cf8SLuca Coelho 	} else
155e705c121SKalle Valo 	if (sta->vht_cap.vht_supported) {
156e705c121SKalle Valo 		agg_size = sta->vht_cap.cap &
157e705c121SKalle Valo 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
158e705c121SKalle Valo 		agg_size >>=
159e705c121SKalle Valo 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
160e705c121SKalle Valo 	} else if (sta->ht_cap.ht_supported) {
161e705c121SKalle Valo 		agg_size = sta->ht_cap.ampdu_factor;
162e705c121SKalle Valo 	}
163e705c121SKalle Valo 
164c8a2e7a2SMordechay Goodstein 	/* D6.0 10.12.2 A-MPDU length limit rules
165c8a2e7a2SMordechay Goodstein 	 * A STA indicates the maximum length of the A-MPDU preEOF padding
166c8a2e7a2SMordechay Goodstein 	 * that it can receive in an HE PPDU in the Maximum A-MPDU Length
167c8a2e7a2SMordechay Goodstein 	 * Exponent field in its HT Capabilities, VHT Capabilities,
168c8a2e7a2SMordechay Goodstein 	 * and HE 6 GHz Band Capabilities elements (if present) and the
169c8a2e7a2SMordechay Goodstein 	 * Maximum AMPDU Length Exponent Extension field in its HE
170c8a2e7a2SMordechay Goodstein 	 * Capabilities element
171c8a2e7a2SMordechay Goodstein 	 */
172c8a2e7a2SMordechay Goodstein 	if (sta->he_cap.has_he)
173c8a2e7a2SMordechay Goodstein 		agg_size += u8_get_bits(sta->he_cap.he_cap_elem.mac_cap_info[3],
174c8a2e7a2SMordechay Goodstein 					IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
175c8a2e7a2SMordechay Goodstein 
176c8a2e7a2SMordechay Goodstein 	/* Limit to max A-MPDU supported by FW */
177c8a2e7a2SMordechay Goodstein 	if (agg_size > (STA_FLG_MAX_AGG_SIZE_4M >> STA_FLG_MAX_AGG_SIZE_SHIFT))
178c8a2e7a2SMordechay Goodstein 		agg_size = (STA_FLG_MAX_AGG_SIZE_4M >>
179c8a2e7a2SMordechay Goodstein 			    STA_FLG_MAX_AGG_SIZE_SHIFT);
180c8a2e7a2SMordechay Goodstein 
181e705c121SKalle Valo 	add_sta_cmd.station_flags |=
182e705c121SKalle Valo 		cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
183e705c121SKalle Valo 	add_sta_cmd.station_flags |=
184e705c121SKalle Valo 		cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
185d94c5a82SGregory Greenman 	if (mvm_sta->sta_state >= IEEE80211_STA_ASSOC)
18619c52f46SEmmanuel Grumbach 		add_sta_cmd.assoc_id = cpu_to_le16(sta->aid);
187e705c121SKalle Valo 
18865e25482SJohannes Berg 	if (sta->wme) {
18965e25482SJohannes Berg 		add_sta_cmd.modify_mask |= STA_MODIFY_UAPSD_ACS;
19065e25482SJohannes Berg 
19165e25482SJohannes Berg 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
192c80eb570SEmmanuel Grumbach 			add_sta_cmd.uapsd_acs |= BIT(AC_BK);
19365e25482SJohannes Berg 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
194c80eb570SEmmanuel Grumbach 			add_sta_cmd.uapsd_acs |= BIT(AC_BE);
19565e25482SJohannes Berg 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
196c80eb570SEmmanuel Grumbach 			add_sta_cmd.uapsd_acs |= BIT(AC_VI);
19765e25482SJohannes Berg 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
198c80eb570SEmmanuel Grumbach 			add_sta_cmd.uapsd_acs |= BIT(AC_VO);
199c80eb570SEmmanuel Grumbach 		add_sta_cmd.uapsd_acs |= add_sta_cmd.uapsd_acs << 4;
200e71ca5eaSEmmanuel Grumbach 		add_sta_cmd.sp_length = sta->max_sp ? sta->max_sp * 2 : 128;
20165e25482SJohannes Berg 	}
20265e25482SJohannes Berg 
203e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
204854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
205854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
206e705c121SKalle Valo 					  &add_sta_cmd, &status);
207e705c121SKalle Valo 	if (ret)
208e705c121SKalle Valo 		return ret;
209e705c121SKalle Valo 
210837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
211e705c121SKalle Valo 	case ADD_STA_SUCCESS:
212e705c121SKalle Valo 		IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
213e705c121SKalle Valo 		break;
214e705c121SKalle Valo 	default:
215e705c121SKalle Valo 		ret = -EIO;
216e705c121SKalle Valo 		IWL_ERR(mvm, "ADD_STA failed\n");
217e705c121SKalle Valo 		break;
218e705c121SKalle Valo 	}
219e705c121SKalle Valo 
220e705c121SKalle Valo 	return ret;
221e705c121SKalle Valo }
222e705c121SKalle Valo 
2238cef5344SKees Cook static void iwl_mvm_rx_agg_session_expired(struct timer_list *t)
22410b2b201SSara Sharon {
2258cef5344SKees Cook 	struct iwl_mvm_baid_data *data =
2268cef5344SKees Cook 		from_timer(data, t, session_timer);
2278cef5344SKees Cook 	struct iwl_mvm_baid_data __rcu **rcu_ptr = data->rcu_ptr;
22810b2b201SSara Sharon 	struct iwl_mvm_baid_data *ba_data;
22910b2b201SSara Sharon 	struct ieee80211_sta *sta;
23010b2b201SSara Sharon 	struct iwl_mvm_sta *mvm_sta;
23110b2b201SSara Sharon 	unsigned long timeout;
23210b2b201SSara Sharon 
23310b2b201SSara Sharon 	rcu_read_lock();
23410b2b201SSara Sharon 
23510b2b201SSara Sharon 	ba_data = rcu_dereference(*rcu_ptr);
23610b2b201SSara Sharon 
23710b2b201SSara Sharon 	if (WARN_ON(!ba_data))
23810b2b201SSara Sharon 		goto unlock;
23910b2b201SSara Sharon 
24010b2b201SSara Sharon 	if (!ba_data->timeout)
24110b2b201SSara Sharon 		goto unlock;
24210b2b201SSara Sharon 
24310b2b201SSara Sharon 	timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
24410b2b201SSara Sharon 	if (time_is_after_jiffies(timeout)) {
24510b2b201SSara Sharon 		mod_timer(&ba_data->session_timer, timeout);
24610b2b201SSara Sharon 		goto unlock;
24710b2b201SSara Sharon 	}
24810b2b201SSara Sharon 
24910b2b201SSara Sharon 	/* Timer expired */
25010b2b201SSara Sharon 	sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
25161dd8a8aSEmmanuel Grumbach 
25261dd8a8aSEmmanuel Grumbach 	/*
25361dd8a8aSEmmanuel Grumbach 	 * sta should be valid unless the following happens:
25461dd8a8aSEmmanuel Grumbach 	 * The firmware asserts which triggers a reconfig flow, but
25561dd8a8aSEmmanuel Grumbach 	 * the reconfig fails before we set the pointer to sta into
25661dd8a8aSEmmanuel Grumbach 	 * the fw_id_to_mac_id pointer table. Mac80211 can't stop
25761dd8a8aSEmmanuel Grumbach 	 * A-MDPU and hence the timer continues to run. Then, the
25861dd8a8aSEmmanuel Grumbach 	 * timer expires and sta is NULL.
25961dd8a8aSEmmanuel Grumbach 	 */
26061dd8a8aSEmmanuel Grumbach 	if (!sta)
26161dd8a8aSEmmanuel Grumbach 		goto unlock;
26261dd8a8aSEmmanuel Grumbach 
26310b2b201SSara Sharon 	mvm_sta = iwl_mvm_sta_from_mac80211(sta);
26420fc690fSNaftali Goldstein 	ieee80211_rx_ba_timer_expired(mvm_sta->vif,
26510b2b201SSara Sharon 				      sta->addr, ba_data->tid);
26610b2b201SSara Sharon unlock:
26710b2b201SSara Sharon 	rcu_read_unlock();
26810b2b201SSara Sharon }
26910b2b201SSara Sharon 
2709794c64fSLiad Kaufman /* Disable aggregations for a bitmap of TIDs for a given station */
2719794c64fSLiad Kaufman static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
2729794c64fSLiad Kaufman 					unsigned long disable_agg_tids,
2739794c64fSLiad Kaufman 					bool remove_queue)
2749794c64fSLiad Kaufman {
2759794c64fSLiad Kaufman 	struct iwl_mvm_add_sta_cmd cmd = {};
2769794c64fSLiad Kaufman 	struct ieee80211_sta *sta;
2779794c64fSLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
2789794c64fSLiad Kaufman 	u32 status;
2799794c64fSLiad Kaufman 	u8 sta_id;
2809794c64fSLiad Kaufman 
281bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
282bb49701bSSara Sharon 		return -EINVAL;
283bb49701bSSara Sharon 
2849794c64fSLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
2859794c64fSLiad Kaufman 
2869794c64fSLiad Kaufman 	rcu_read_lock();
2879794c64fSLiad Kaufman 
2889794c64fSLiad Kaufman 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
2899794c64fSLiad Kaufman 
2909794c64fSLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
2919794c64fSLiad Kaufman 		rcu_read_unlock();
2929794c64fSLiad Kaufman 		return -EINVAL;
2939794c64fSLiad Kaufman 	}
2949794c64fSLiad Kaufman 
2959794c64fSLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
2969794c64fSLiad Kaufman 
2979794c64fSLiad Kaufman 	mvmsta->tid_disable_agg |= disable_agg_tids;
2989794c64fSLiad Kaufman 
2999794c64fSLiad Kaufman 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
3009794c64fSLiad Kaufman 	cmd.sta_id = mvmsta->sta_id;
3019794c64fSLiad Kaufman 	cmd.add_modify = STA_MODE_MODIFY;
3029794c64fSLiad Kaufman 	cmd.modify_mask = STA_MODIFY_QUEUES;
3039794c64fSLiad Kaufman 	if (disable_agg_tids)
3049794c64fSLiad Kaufman 		cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
3059794c64fSLiad Kaufman 	if (remove_queue)
3069794c64fSLiad Kaufman 		cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
3079794c64fSLiad Kaufman 	cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
3089794c64fSLiad Kaufman 	cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
3099794c64fSLiad Kaufman 
3109794c64fSLiad Kaufman 	rcu_read_unlock();
3119794c64fSLiad Kaufman 
3129794c64fSLiad Kaufman 	/* Notify FW of queue removal from the STA queues */
3139794c64fSLiad Kaufman 	status = ADD_STA_SUCCESS;
314b2c1bf59SSara Sharon 	return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
3159794c64fSLiad Kaufman 					   iwl_mvm_add_sta_cmd_size(mvm),
3169794c64fSLiad Kaufman 					   &cmd, &status);
3179794c64fSLiad Kaufman }
3189794c64fSLiad Kaufman 
319cfbc6c4cSSara Sharon static int iwl_mvm_disable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
320c6ce1c74SJohannes Berg 			       u16 *queueptr, u8 tid, u8 flags)
32199448a8cSJohannes Berg {
322c6ce1c74SJohannes Berg 	int queue = *queueptr;
32399448a8cSJohannes Berg 	struct iwl_scd_txq_cfg_cmd cmd = {
32499448a8cSJohannes Berg 		.scd_queue = queue,
32599448a8cSJohannes Berg 		.action = SCD_CFG_DISABLE_QUEUE,
32699448a8cSJohannes Berg 	};
32799448a8cSJohannes Berg 	int ret;
32899448a8cSJohannes Berg 
32999448a8cSJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm)) {
33099448a8cSJohannes Berg 		iwl_trans_txq_free(mvm->trans, queue);
331c6ce1c74SJohannes Berg 		*queueptr = IWL_MVM_INVALID_QUEUE;
33299448a8cSJohannes Berg 
33399448a8cSJohannes Berg 		return 0;
33499448a8cSJohannes Berg 	}
33599448a8cSJohannes Berg 
336f3f240f9SJohannes Berg 	if (WARN_ON(mvm->queue_info[queue].tid_bitmap == 0))
33799448a8cSJohannes Berg 		return 0;
33899448a8cSJohannes Berg 
33999448a8cSJohannes Berg 	mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
34099448a8cSJohannes Berg 
3411c14089eSJohannes Berg 	cmd.action = mvm->queue_info[queue].tid_bitmap ?
34299448a8cSJohannes Berg 		SCD_CFG_ENABLE_QUEUE : SCD_CFG_DISABLE_QUEUE;
34399448a8cSJohannes Berg 	if (cmd.action == SCD_CFG_DISABLE_QUEUE)
34499448a8cSJohannes Berg 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_FREE;
34599448a8cSJohannes Berg 
34699448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
347cfbc6c4cSSara Sharon 			    "Disabling TXQ #%d tids=0x%x\n",
34899448a8cSJohannes Berg 			    queue,
349cfbc6c4cSSara Sharon 			    mvm->queue_info[queue].tid_bitmap);
35099448a8cSJohannes Berg 
35199448a8cSJohannes Berg 	/* If the queue is still enabled - nothing left to do in this func */
352f3f240f9SJohannes Berg 	if (cmd.action == SCD_CFG_ENABLE_QUEUE)
35399448a8cSJohannes Berg 		return 0;
35499448a8cSJohannes Berg 
35599448a8cSJohannes Berg 	cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
35699448a8cSJohannes Berg 	cmd.tid = mvm->queue_info[queue].txq_tid;
35799448a8cSJohannes Berg 
35899448a8cSJohannes Berg 	/* Make sure queue info is correct even though we overwrite it */
359cfbc6c4cSSara Sharon 	WARN(mvm->queue_info[queue].tid_bitmap,
360cfbc6c4cSSara Sharon 	     "TXQ #%d info out-of-sync - tids=0x%x\n",
361cfbc6c4cSSara Sharon 	     queue, mvm->queue_info[queue].tid_bitmap);
36299448a8cSJohannes Berg 
36399448a8cSJohannes Berg 	/* If we are here - the queue is freed and we can zero out these vals */
36499448a8cSJohannes Berg 	mvm->queue_info[queue].tid_bitmap = 0;
365cfbc6c4cSSara Sharon 
366cfbc6c4cSSara Sharon 	if (sta) {
367cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
368cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_tid(sta, tid);
369cfbc6c4cSSara Sharon 
370cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
371cfbc6c4cSSara Sharon 	}
37299448a8cSJohannes Berg 
37399448a8cSJohannes Berg 	/* Regardless if this is a reserved TXQ for a STA - mark it as false */
37499448a8cSJohannes Berg 	mvm->queue_info[queue].reserved = false;
37599448a8cSJohannes Berg 
37699448a8cSJohannes Berg 	iwl_trans_txq_disable(mvm->trans, queue, false);
37799448a8cSJohannes Berg 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, flags,
37899448a8cSJohannes Berg 				   sizeof(struct iwl_scd_txq_cfg_cmd), &cmd);
37999448a8cSJohannes Berg 
38099448a8cSJohannes Berg 	if (ret)
38199448a8cSJohannes Berg 		IWL_ERR(mvm, "Failed to disable queue %d (ret=%d)\n",
38299448a8cSJohannes Berg 			queue, ret);
38399448a8cSJohannes Berg 	return ret;
38499448a8cSJohannes Berg }
38599448a8cSJohannes Berg 
38642db09c1SLiad Kaufman static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
38742db09c1SLiad Kaufman {
38842db09c1SLiad Kaufman 	struct ieee80211_sta *sta;
38942db09c1SLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
39042db09c1SLiad Kaufman 	unsigned long tid_bitmap;
39142db09c1SLiad Kaufman 	unsigned long agg_tids = 0;
392806911daSSharon Dvir 	u8 sta_id;
39342db09c1SLiad Kaufman 	int tid;
39442db09c1SLiad Kaufman 
39542db09c1SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
39642db09c1SLiad Kaufman 
397bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
398bb49701bSSara Sharon 		return -EINVAL;
399bb49701bSSara Sharon 
40042db09c1SLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
40142db09c1SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
40242db09c1SLiad Kaufman 
40342db09c1SLiad Kaufman 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
40442db09c1SLiad Kaufman 					lockdep_is_held(&mvm->mutex));
40542db09c1SLiad Kaufman 
40642db09c1SLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
40742db09c1SLiad Kaufman 		return -EINVAL;
40842db09c1SLiad Kaufman 
40942db09c1SLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
41042db09c1SLiad Kaufman 
41142db09c1SLiad Kaufman 	spin_lock_bh(&mvmsta->lock);
41242db09c1SLiad Kaufman 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
41342db09c1SLiad Kaufman 		if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
41442db09c1SLiad Kaufman 			agg_tids |= BIT(tid);
41542db09c1SLiad Kaufman 	}
41642db09c1SLiad Kaufman 	spin_unlock_bh(&mvmsta->lock);
41742db09c1SLiad Kaufman 
41842db09c1SLiad Kaufman 	return agg_tids;
41942db09c1SLiad Kaufman }
42042db09c1SLiad Kaufman 
4219794c64fSLiad Kaufman /*
4229794c64fSLiad Kaufman  * Remove a queue from a station's resources.
4239794c64fSLiad Kaufman  * Note that this only marks as free. It DOESN'T delete a BA agreement, and
4249794c64fSLiad Kaufman  * doesn't disable the queue
4259794c64fSLiad Kaufman  */
4269794c64fSLiad Kaufman static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
4279794c64fSLiad Kaufman {
4289794c64fSLiad Kaufman 	struct ieee80211_sta *sta;
4299794c64fSLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
4309794c64fSLiad Kaufman 	unsigned long tid_bitmap;
4319794c64fSLiad Kaufman 	unsigned long disable_agg_tids = 0;
4329794c64fSLiad Kaufman 	u8 sta_id;
4339794c64fSLiad Kaufman 	int tid;
4349794c64fSLiad Kaufman 
4359794c64fSLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
4369794c64fSLiad Kaufman 
437bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
438bb49701bSSara Sharon 		return -EINVAL;
439bb49701bSSara Sharon 
4409794c64fSLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
4419794c64fSLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
4429794c64fSLiad Kaufman 
4439794c64fSLiad Kaufman 	rcu_read_lock();
4449794c64fSLiad Kaufman 
4459794c64fSLiad Kaufman 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
4469794c64fSLiad Kaufman 
4479794c64fSLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
4489794c64fSLiad Kaufman 		rcu_read_unlock();
4499794c64fSLiad Kaufman 		return 0;
4509794c64fSLiad Kaufman 	}
4519794c64fSLiad Kaufman 
4529794c64fSLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
4539794c64fSLiad Kaufman 
4549794c64fSLiad Kaufman 	spin_lock_bh(&mvmsta->lock);
45542db09c1SLiad Kaufman 	/* Unmap MAC queues and TIDs from this queue */
4569794c64fSLiad Kaufman 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
457cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
458cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_tid(sta, tid);
459cfbc6c4cSSara Sharon 
4609794c64fSLiad Kaufman 		if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
4619794c64fSLiad Kaufman 			disable_agg_tids |= BIT(tid);
4626862fceeSSara Sharon 		mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
463cfbc6c4cSSara Sharon 
464cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
4659794c64fSLiad Kaufman 	}
4669794c64fSLiad Kaufman 
46742db09c1SLiad Kaufman 	mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
4689794c64fSLiad Kaufman 	spin_unlock_bh(&mvmsta->lock);
4699794c64fSLiad Kaufman 
4709794c64fSLiad Kaufman 	rcu_read_unlock();
4719794c64fSLiad Kaufman 
47206bc6f6eSJohannes Berg 	/*
47306bc6f6eSJohannes Berg 	 * The TX path may have been using this TXQ_ID from the tid_data,
47406bc6f6eSJohannes Berg 	 * so make sure it's no longer running so that we can safely reuse
47506bc6f6eSJohannes Berg 	 * this TXQ later. We've set all the TIDs to IWL_MVM_INVALID_QUEUE
47606bc6f6eSJohannes Berg 	 * above, but nothing guarantees we've stopped using them. Thus,
47706bc6f6eSJohannes Berg 	 * without this, we could get to iwl_mvm_disable_txq() and remove
47806bc6f6eSJohannes Berg 	 * the queue while still sending frames to it.
47906bc6f6eSJohannes Berg 	 */
48006bc6f6eSJohannes Berg 	synchronize_net();
48106bc6f6eSJohannes Berg 
4829794c64fSLiad Kaufman 	return disable_agg_tids;
4839794c64fSLiad Kaufman }
4849794c64fSLiad Kaufman 
48501796ff2SSara Sharon static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
486cfbc6c4cSSara Sharon 				       struct ieee80211_sta *old_sta,
487724fe771SJohannes Berg 				       u8 new_sta_id)
48801796ff2SSara Sharon {
48901796ff2SSara Sharon 	struct iwl_mvm_sta *mvmsta;
490cfbc6c4cSSara Sharon 	u8 sta_id, tid;
49101796ff2SSara Sharon 	unsigned long disable_agg_tids = 0;
492724fe771SJohannes Berg 	bool same_sta;
493c6ce1c74SJohannes Berg 	u16 queue_tmp = queue;
49401796ff2SSara Sharon 	int ret;
49501796ff2SSara Sharon 
49601796ff2SSara Sharon 	lockdep_assert_held(&mvm->mutex);
49701796ff2SSara Sharon 
498bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
499bb49701bSSara Sharon 		return -EINVAL;
500bb49701bSSara Sharon 
50101796ff2SSara Sharon 	sta_id = mvm->queue_info[queue].ra_sta_id;
50201796ff2SSara Sharon 	tid = mvm->queue_info[queue].txq_tid;
50301796ff2SSara Sharon 
504724fe771SJohannes Berg 	same_sta = sta_id == new_sta_id;
505724fe771SJohannes Berg 
50601796ff2SSara Sharon 	mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
507e3df1e4bSSharon Dvir 	if (WARN_ON(!mvmsta))
508e3df1e4bSSharon Dvir 		return -EINVAL;
50901796ff2SSara Sharon 
51001796ff2SSara Sharon 	disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
51101796ff2SSara Sharon 	/* Disable the queue */
51201796ff2SSara Sharon 	if (disable_agg_tids)
51301796ff2SSara Sharon 		iwl_mvm_invalidate_sta_queue(mvm, queue,
51401796ff2SSara Sharon 					     disable_agg_tids, false);
51501796ff2SSara Sharon 
516c6ce1c74SJohannes Berg 	ret = iwl_mvm_disable_txq(mvm, old_sta, &queue_tmp, tid, 0);
51701796ff2SSara Sharon 	if (ret) {
51801796ff2SSara Sharon 		IWL_ERR(mvm,
51901796ff2SSara Sharon 			"Failed to free inactive queue %d (ret=%d)\n",
52001796ff2SSara Sharon 			queue, ret);
52101796ff2SSara Sharon 
52201796ff2SSara Sharon 		return ret;
52301796ff2SSara Sharon 	}
52401796ff2SSara Sharon 
52501796ff2SSara Sharon 	/* If TXQ is allocated to another STA, update removal in FW */
52601796ff2SSara Sharon 	if (!same_sta)
52701796ff2SSara Sharon 		iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
52801796ff2SSara Sharon 
52901796ff2SSara Sharon 	return 0;
53001796ff2SSara Sharon }
53101796ff2SSara Sharon 
53242db09c1SLiad Kaufman static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
53342db09c1SLiad Kaufman 				    unsigned long tfd_queue_mask, u8 ac)
53442db09c1SLiad Kaufman {
53542db09c1SLiad Kaufman 	int queue = 0;
53642db09c1SLiad Kaufman 	u8 ac_to_queue[IEEE80211_NUM_ACS];
53742db09c1SLiad Kaufman 	int i;
53842db09c1SLiad Kaufman 
53990d2d94cSJohannes Berg 	/*
54090d2d94cSJohannes Berg 	 * This protects us against grabbing a queue that's being reconfigured
54190d2d94cSJohannes Berg 	 * by the inactivity checker.
54290d2d94cSJohannes Berg 	 */
54390d2d94cSJohannes Berg 	lockdep_assert_held(&mvm->mutex);
54490d2d94cSJohannes Berg 
545bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
546bb49701bSSara Sharon 		return -EINVAL;
54742db09c1SLiad Kaufman 
54842db09c1SLiad Kaufman 	memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
54942db09c1SLiad Kaufman 
55042db09c1SLiad Kaufman 	/* See what ACs the existing queues for this STA have */
55142db09c1SLiad Kaufman 	for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
55242db09c1SLiad Kaufman 		/* Only DATA queues can be shared */
55342db09c1SLiad Kaufman 		if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
55442db09c1SLiad Kaufman 		    i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
55542db09c1SLiad Kaufman 			continue;
55642db09c1SLiad Kaufman 
55742db09c1SLiad Kaufman 		ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
55842db09c1SLiad Kaufman 	}
55942db09c1SLiad Kaufman 
56042db09c1SLiad Kaufman 	/*
56142db09c1SLiad Kaufman 	 * The queue to share is chosen only from DATA queues as follows (in
56242db09c1SLiad Kaufman 	 * descending priority):
56342db09c1SLiad Kaufman 	 * 1. An AC_BE queue
56442db09c1SLiad Kaufman 	 * 2. Same AC queue
56542db09c1SLiad Kaufman 	 * 3. Highest AC queue that is lower than new AC
56642db09c1SLiad Kaufman 	 * 4. Any existing AC (there always is at least 1 DATA queue)
56742db09c1SLiad Kaufman 	 */
56842db09c1SLiad Kaufman 
56942db09c1SLiad Kaufman 	/* Priority 1: An AC_BE queue */
57042db09c1SLiad Kaufman 	if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
57142db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_BE];
57242db09c1SLiad Kaufman 	/* Priority 2: Same AC queue */
57342db09c1SLiad Kaufman 	else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
57442db09c1SLiad Kaufman 		queue = ac_to_queue[ac];
57542db09c1SLiad Kaufman 	/* Priority 3a: If new AC is VO and VI exists - use VI */
57642db09c1SLiad Kaufman 	else if (ac == IEEE80211_AC_VO &&
57742db09c1SLiad Kaufman 		 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
57842db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VI];
57942db09c1SLiad Kaufman 	/* Priority 3b: No BE so only AC less than the new one is BK */
58042db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
58142db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_BK];
58242db09c1SLiad Kaufman 	/* Priority 4a: No BE nor BK - use VI if exists */
58342db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
58442db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VI];
58542db09c1SLiad Kaufman 	/* Priority 4b: No BE, BK nor VI - use VO if exists */
58642db09c1SLiad Kaufman 	else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
58742db09c1SLiad Kaufman 		queue = ac_to_queue[IEEE80211_AC_VO];
58842db09c1SLiad Kaufman 
58942db09c1SLiad Kaufman 	/* Make sure queue found (or not) is legal */
5909f9af3d7SLiad Kaufman 	if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
5919f9af3d7SLiad Kaufman 	    !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
5929f9af3d7SLiad Kaufman 	    (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
59342db09c1SLiad Kaufman 		IWL_ERR(mvm, "No DATA queues available to share\n");
5949f9af3d7SLiad Kaufman 		return -ENOSPC;
5959f9af3d7SLiad Kaufman 	}
5969f9af3d7SLiad Kaufman 
59742db09c1SLiad Kaufman 	return queue;
59842db09c1SLiad Kaufman }
59942db09c1SLiad Kaufman 
60058f2cc57SLiad Kaufman /*
6019f9af3d7SLiad Kaufman  * If a given queue has a higher AC than the TID stream that is being compared
6029f9af3d7SLiad Kaufman  * to, the queue needs to be redirected to the lower AC. This function does that
60358f2cc57SLiad Kaufman  * in such a case, otherwise - if no redirection required - it does nothing,
60458f2cc57SLiad Kaufman  * unless the %force param is true.
60558f2cc57SLiad Kaufman  */
606cfbc6c4cSSara Sharon static int iwl_mvm_redirect_queue(struct iwl_mvm *mvm, int queue, int tid,
60758f2cc57SLiad Kaufman 				  int ac, int ssn, unsigned int wdg_timeout,
608cfbc6c4cSSara Sharon 				  bool force, struct iwl_mvm_txq *txq)
60958f2cc57SLiad Kaufman {
61058f2cc57SLiad Kaufman 	struct iwl_scd_txq_cfg_cmd cmd = {
61158f2cc57SLiad Kaufman 		.scd_queue = queue,
612f7c692deSLiad Kaufman 		.action = SCD_CFG_DISABLE_QUEUE,
61358f2cc57SLiad Kaufman 	};
61458f2cc57SLiad Kaufman 	bool shared_queue;
61558f2cc57SLiad Kaufman 	int ret;
61658f2cc57SLiad Kaufman 
617bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
618bb49701bSSara Sharon 		return -EINVAL;
619bb49701bSSara Sharon 
62058f2cc57SLiad Kaufman 	/*
62158f2cc57SLiad Kaufman 	 * If the AC is lower than current one - FIFO needs to be redirected to
62258f2cc57SLiad Kaufman 	 * the lowest one of the streams in the queue. Check if this is needed
62358f2cc57SLiad Kaufman 	 * here.
62458f2cc57SLiad Kaufman 	 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
62558f2cc57SLiad Kaufman 	 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
62658f2cc57SLiad Kaufman 	 * we need to check if the numerical value of X is LARGER than of Y.
62758f2cc57SLiad Kaufman 	 */
62858f2cc57SLiad Kaufman 	if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
62958f2cc57SLiad Kaufman 		IWL_DEBUG_TX_QUEUES(mvm,
63058f2cc57SLiad Kaufman 				    "No redirection needed on TXQ #%d\n",
63158f2cc57SLiad Kaufman 				    queue);
63258f2cc57SLiad Kaufman 		return 0;
63358f2cc57SLiad Kaufman 	}
63458f2cc57SLiad Kaufman 
63558f2cc57SLiad Kaufman 	cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
63658f2cc57SLiad Kaufman 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
637edbe961cSLiad Kaufman 	cmd.tid = mvm->queue_info[queue].txq_tid;
6381c14089eSJohannes Berg 	shared_queue = hweight16(mvm->queue_info[queue].tid_bitmap) > 1;
63958f2cc57SLiad Kaufman 
6409f9af3d7SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
64158f2cc57SLiad Kaufman 			    queue, iwl_mvm_ac_to_tx_fifo[ac]);
64258f2cc57SLiad Kaufman 
643cfbc6c4cSSara Sharon 	/* Stop the queue and wait for it to empty */
644cfbc6c4cSSara Sharon 	txq->stopped = true;
645cfbc6c4cSSara Sharon 
646a1a57877SSara Sharon 	ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue));
64758f2cc57SLiad Kaufman 	if (ret) {
64858f2cc57SLiad Kaufman 		IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
64958f2cc57SLiad Kaufman 			queue);
65058f2cc57SLiad Kaufman 		ret = -EIO;
65158f2cc57SLiad Kaufman 		goto out;
65258f2cc57SLiad Kaufman 	}
65358f2cc57SLiad Kaufman 
65458f2cc57SLiad Kaufman 	/* Before redirecting the queue we need to de-activate it */
65558f2cc57SLiad Kaufman 	iwl_trans_txq_disable(mvm->trans, queue, false);
65658f2cc57SLiad Kaufman 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
65758f2cc57SLiad Kaufman 	if (ret)
65858f2cc57SLiad Kaufman 		IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
65958f2cc57SLiad Kaufman 			ret);
66058f2cc57SLiad Kaufman 
66158f2cc57SLiad Kaufman 	/* Make sure the SCD wrptr is correctly set before reconfiguring */
662ca3b9c6bSSara Sharon 	iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
66358f2cc57SLiad Kaufman 
664edbe961cSLiad Kaufman 	/* Update the TID "owner" of the queue */
665edbe961cSLiad Kaufman 	mvm->queue_info[queue].txq_tid = tid;
666edbe961cSLiad Kaufman 
66758f2cc57SLiad Kaufman 	/* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
66858f2cc57SLiad Kaufman 
66958f2cc57SLiad Kaufman 	/* Redirect to lower AC */
67058f2cc57SLiad Kaufman 	iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
6710ec9257bSSara Sharon 			     cmd.sta_id, tid, IWL_FRAME_LIMIT, ssn);
67258f2cc57SLiad Kaufman 
67358f2cc57SLiad Kaufman 	/* Update AC marking of the queue */
67458f2cc57SLiad Kaufman 	mvm->queue_info[queue].mac80211_ac = ac;
67558f2cc57SLiad Kaufman 
67658f2cc57SLiad Kaufman 	/*
67758f2cc57SLiad Kaufman 	 * Mark queue as shared in transport if shared
67858f2cc57SLiad Kaufman 	 * Note this has to be done after queue enablement because enablement
67958f2cc57SLiad Kaufman 	 * can also set this value, and there is no indication there to shared
68058f2cc57SLiad Kaufman 	 * queues
68158f2cc57SLiad Kaufman 	 */
68258f2cc57SLiad Kaufman 	if (shared_queue)
68358f2cc57SLiad Kaufman 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
68458f2cc57SLiad Kaufman 
68558f2cc57SLiad Kaufman out:
686cfbc6c4cSSara Sharon 	/* Continue using the queue */
687cfbc6c4cSSara Sharon 	txq->stopped = false;
68858f2cc57SLiad Kaufman 
68958f2cc57SLiad Kaufman 	return ret;
69058f2cc57SLiad Kaufman }
69158f2cc57SLiad Kaufman 
69299448a8cSJohannes Berg static int iwl_mvm_find_free_queue(struct iwl_mvm *mvm, u8 sta_id,
69399448a8cSJohannes Berg 				   u8 minq, u8 maxq)
69499448a8cSJohannes Berg {
69599448a8cSJohannes Berg 	int i;
69699448a8cSJohannes Berg 
697f3f240f9SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
69899448a8cSJohannes Berg 
699e5b72e3bSJohannes Berg 	if (WARN(maxq >= mvm->trans->trans_cfg->base_params->num_of_queues,
700e5b72e3bSJohannes Berg 		 "max queue %d >= num_of_queues (%d)", maxq,
701e5b72e3bSJohannes Berg 		 mvm->trans->trans_cfg->base_params->num_of_queues))
702e5b72e3bSJohannes Berg 		maxq = mvm->trans->trans_cfg->base_params->num_of_queues - 1;
703e5b72e3bSJohannes Berg 
70499448a8cSJohannes Berg 	/* This should not be hit with new TX path */
70599448a8cSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
70699448a8cSJohannes Berg 		return -ENOSPC;
70799448a8cSJohannes Berg 
70899448a8cSJohannes Berg 	/* Start by looking for a free queue */
70999448a8cSJohannes Berg 	for (i = minq; i <= maxq; i++)
7101c14089eSJohannes Berg 		if (mvm->queue_info[i].tid_bitmap == 0 &&
71199448a8cSJohannes Berg 		    mvm->queue_info[i].status == IWL_MVM_QUEUE_FREE)
71299448a8cSJohannes Berg 			return i;
71399448a8cSJohannes Berg 
71499448a8cSJohannes Berg 	return -ENOSPC;
71599448a8cSJohannes Berg }
71699448a8cSJohannes Berg 
717cfbc6c4cSSara Sharon static int iwl_mvm_tvqm_enable_txq(struct iwl_mvm *mvm,
71899448a8cSJohannes Berg 				   u8 sta_id, u8 tid, unsigned int timeout)
71999448a8cSJohannes Berg {
720d5399f11SMordechay Goodstein 	int queue, size;
72199448a8cSJohannes Berg 
72299448a8cSJohannes Berg 	if (tid == IWL_MAX_TID_COUNT) {
72399448a8cSJohannes Berg 		tid = IWL_MGMT_TID;
724ff911dcaSShaul Triebitz 		size = max_t(u32, IWL_MGMT_QUEUE_SIZE,
725ff911dcaSShaul Triebitz 			     mvm->trans->cfg->min_txq_size);
726d5399f11SMordechay Goodstein 	} else {
727d5399f11SMordechay Goodstein 		struct ieee80211_sta *sta;
728d5399f11SMordechay Goodstein 
729d5399f11SMordechay Goodstein 		rcu_read_lock();
730d5399f11SMordechay Goodstein 		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
731d5399f11SMordechay Goodstein 
732d5399f11SMordechay Goodstein 		/* this queue isn't used for traffic (cab_queue) */
733d5399f11SMordechay Goodstein 		if (IS_ERR_OR_NULL(sta)) {
734d5399f11SMordechay Goodstein 			size = IWL_MGMT_QUEUE_SIZE;
735d5399f11SMordechay Goodstein 		} else if (sta->he_cap.has_he) {
736d5399f11SMordechay Goodstein 			/* support for 256 ba size */
737d5399f11SMordechay Goodstein 			size = IWL_DEFAULT_QUEUE_SIZE_HE;
738d5399f11SMordechay Goodstein 		} else {
739d5399f11SMordechay Goodstein 			size = IWL_DEFAULT_QUEUE_SIZE;
74099448a8cSJohannes Berg 		}
74199448a8cSJohannes Berg 
742d5399f11SMordechay Goodstein 		rcu_read_unlock();
743d5399f11SMordechay Goodstein 	}
744d5399f11SMordechay Goodstein 
745d5399f11SMordechay Goodstein 	/* take the min with bc tbl entries allowed */
746d5399f11SMordechay Goodstein 	size = min_t(u32, size, mvm->trans->txqs.bc_tbl_size / sizeof(u16));
747d5399f11SMordechay Goodstein 
748d5399f11SMordechay Goodstein 	/* size needs to be power of 2 values for calculating read/write pointers */
749d5399f11SMordechay Goodstein 	size = rounddown_pow_of_two(size);
750d5399f11SMordechay Goodstein 
75192f78d4bSJohannes Berg 	do {
75292f78d4bSJohannes Berg 		__le16 enable = cpu_to_le16(TX_QUEUE_CFG_ENABLE_QUEUE);
75392f78d4bSJohannes Berg 
75492f78d4bSJohannes Berg 		queue = iwl_trans_txq_alloc(mvm->trans, enable,
75592f78d4bSJohannes Berg 					    sta_id, tid, SCD_QUEUE_CFG,
75692f78d4bSJohannes Berg 					    size, timeout);
75792f78d4bSJohannes Berg 
75892f78d4bSJohannes Berg 		if (queue < 0)
75999448a8cSJohannes Berg 			IWL_DEBUG_TX_QUEUES(mvm,
76092f78d4bSJohannes Berg 					    "Failed allocating TXQ of size %d for sta %d tid %d, ret: %d\n",
76192f78d4bSJohannes Berg 					    size, sta_id, tid, queue);
76292f78d4bSJohannes Berg 		size /= 2;
76392f78d4bSJohannes Berg 	} while (queue < 0 && size >= 16);
76492f78d4bSJohannes Berg 
76592f78d4bSJohannes Berg 	if (queue < 0)
76699448a8cSJohannes Berg 		return queue;
76799448a8cSJohannes Berg 
76899448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d for sta %d tid %d\n",
76999448a8cSJohannes Berg 			    queue, sta_id, tid);
77099448a8cSJohannes Berg 
77199448a8cSJohannes Berg 	return queue;
77299448a8cSJohannes Berg }
77399448a8cSJohannes Berg 
774310181ecSSara Sharon static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
775310181ecSSara Sharon 					struct ieee80211_sta *sta, u8 ac,
776310181ecSSara Sharon 					int tid)
777310181ecSSara Sharon {
778310181ecSSara Sharon 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
779cfbc6c4cSSara Sharon 	struct iwl_mvm_txq *mvmtxq =
780cfbc6c4cSSara Sharon 		iwl_mvm_txq_from_tid(sta, tid);
781310181ecSSara Sharon 	unsigned int wdg_timeout =
782310181ecSSara Sharon 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
783310181ecSSara Sharon 	int queue = -1;
784310181ecSSara Sharon 
785310181ecSSara Sharon 	lockdep_assert_held(&mvm->mutex);
786310181ecSSara Sharon 
787310181ecSSara Sharon 	IWL_DEBUG_TX_QUEUES(mvm,
788310181ecSSara Sharon 			    "Allocating queue for sta %d on tid %d\n",
789310181ecSSara Sharon 			    mvmsta->sta_id, tid);
790cfbc6c4cSSara Sharon 	queue = iwl_mvm_tvqm_enable_txq(mvm, mvmsta->sta_id, tid, wdg_timeout);
791310181ecSSara Sharon 	if (queue < 0)
792310181ecSSara Sharon 		return queue;
793310181ecSSara Sharon 
794cfbc6c4cSSara Sharon 	mvmtxq->txq_id = queue;
795cfbc6c4cSSara Sharon 	mvm->tvqm_info[queue].txq_tid = tid;
796cfbc6c4cSSara Sharon 	mvm->tvqm_info[queue].sta_id = mvmsta->sta_id;
797cfbc6c4cSSara Sharon 
798310181ecSSara Sharon 	IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
799310181ecSSara Sharon 
800310181ecSSara Sharon 	spin_lock_bh(&mvmsta->lock);
801310181ecSSara Sharon 	mvmsta->tid_data[tid].txq_id = queue;
802310181ecSSara Sharon 	spin_unlock_bh(&mvmsta->lock);
803310181ecSSara Sharon 
804310181ecSSara Sharon 	return 0;
805310181ecSSara Sharon }
806310181ecSSara Sharon 
807cfbc6c4cSSara Sharon static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm,
808cfbc6c4cSSara Sharon 				       struct ieee80211_sta *sta,
809cfbc6c4cSSara Sharon 				       int queue, u8 sta_id, u8 tid)
81099448a8cSJohannes Berg {
81199448a8cSJohannes Berg 	bool enable_queue = true;
81299448a8cSJohannes Berg 
81399448a8cSJohannes Berg 	/* Make sure this TID isn't already enabled */
81499448a8cSJohannes Berg 	if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) {
81599448a8cSJohannes Berg 		IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n",
81699448a8cSJohannes Berg 			queue, tid);
81799448a8cSJohannes Berg 		return false;
81899448a8cSJohannes Berg 	}
81999448a8cSJohannes Berg 
82099448a8cSJohannes Berg 	/* Update mappings and refcounts */
8211c14089eSJohannes Berg 	if (mvm->queue_info[queue].tid_bitmap)
82299448a8cSJohannes Berg 		enable_queue = false;
82399448a8cSJohannes Berg 
82499448a8cSJohannes Berg 	mvm->queue_info[queue].tid_bitmap |= BIT(tid);
82599448a8cSJohannes Berg 	mvm->queue_info[queue].ra_sta_id = sta_id;
82699448a8cSJohannes Berg 
82799448a8cSJohannes Berg 	if (enable_queue) {
82899448a8cSJohannes Berg 		if (tid != IWL_MAX_TID_COUNT)
82999448a8cSJohannes Berg 			mvm->queue_info[queue].mac80211_ac =
83099448a8cSJohannes Berg 				tid_to_mac80211_ac[tid];
83199448a8cSJohannes Berg 		else
83299448a8cSJohannes Berg 			mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO;
83399448a8cSJohannes Berg 
83499448a8cSJohannes Berg 		mvm->queue_info[queue].txq_tid = tid;
83599448a8cSJohannes Berg 	}
83699448a8cSJohannes Berg 
837cfbc6c4cSSara Sharon 	if (sta) {
838cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
839cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_tid(sta, tid);
840cfbc6c4cSSara Sharon 
841cfbc6c4cSSara Sharon 		mvmtxq->txq_id = queue;
842cfbc6c4cSSara Sharon 	}
843cfbc6c4cSSara Sharon 
84499448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
845cfbc6c4cSSara Sharon 			    "Enabling TXQ #%d tids=0x%x\n",
846cfbc6c4cSSara Sharon 			    queue, mvm->queue_info[queue].tid_bitmap);
84799448a8cSJohannes Berg 
84899448a8cSJohannes Berg 	return enable_queue;
84999448a8cSJohannes Berg }
85099448a8cSJohannes Berg 
851cfbc6c4cSSara Sharon static bool iwl_mvm_enable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
852cfbc6c4cSSara Sharon 			       int queue, u16 ssn,
85399448a8cSJohannes Berg 			       const struct iwl_trans_txq_scd_cfg *cfg,
85499448a8cSJohannes Berg 			       unsigned int wdg_timeout)
85599448a8cSJohannes Berg {
85699448a8cSJohannes Berg 	struct iwl_scd_txq_cfg_cmd cmd = {
85799448a8cSJohannes Berg 		.scd_queue = queue,
85899448a8cSJohannes Berg 		.action = SCD_CFG_ENABLE_QUEUE,
85999448a8cSJohannes Berg 		.window = cfg->frame_limit,
86099448a8cSJohannes Berg 		.sta_id = cfg->sta_id,
86199448a8cSJohannes Berg 		.ssn = cpu_to_le16(ssn),
86299448a8cSJohannes Berg 		.tx_fifo = cfg->fifo,
86399448a8cSJohannes Berg 		.aggregate = cfg->aggregate,
86499448a8cSJohannes Berg 		.tid = cfg->tid,
86599448a8cSJohannes Berg 	};
86699448a8cSJohannes Berg 	bool inc_ssn;
86799448a8cSJohannes Berg 
86899448a8cSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
86999448a8cSJohannes Berg 		return false;
87099448a8cSJohannes Berg 
87199448a8cSJohannes Berg 	/* Send the enabling command if we need to */
872cfbc6c4cSSara Sharon 	if (!iwl_mvm_update_txq_mapping(mvm, sta, queue, cfg->sta_id, cfg->tid))
87399448a8cSJohannes Berg 		return false;
87499448a8cSJohannes Berg 
87599448a8cSJohannes Berg 	inc_ssn = iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn,
87699448a8cSJohannes Berg 					   NULL, wdg_timeout);
87799448a8cSJohannes Berg 	if (inc_ssn)
87899448a8cSJohannes Berg 		le16_add_cpu(&cmd.ssn, 1);
87999448a8cSJohannes Berg 
88099448a8cSJohannes Berg 	WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd),
88199448a8cSJohannes Berg 	     "Failed to configure queue %d on FIFO %d\n", queue, cfg->fifo);
88299448a8cSJohannes Berg 
88399448a8cSJohannes Berg 	return inc_ssn;
88499448a8cSJohannes Berg }
88599448a8cSJohannes Berg 
886b3a87f11SJohannes Berg static void iwl_mvm_change_queue_tid(struct iwl_mvm *mvm, int queue)
88719aefa45SLiad Kaufman {
88819aefa45SLiad Kaufman 	struct iwl_scd_txq_cfg_cmd cmd = {
88919aefa45SLiad Kaufman 		.scd_queue = queue,
89019aefa45SLiad Kaufman 		.action = SCD_CFG_UPDATE_QUEUE_TID,
89119aefa45SLiad Kaufman 	};
89219aefa45SLiad Kaufman 	int tid;
89319aefa45SLiad Kaufman 	unsigned long tid_bitmap;
89419aefa45SLiad Kaufman 	int ret;
89519aefa45SLiad Kaufman 
89619aefa45SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
89719aefa45SLiad Kaufman 
898bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
899bb49701bSSara Sharon 		return;
900bb49701bSSara Sharon 
90119aefa45SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
90219aefa45SLiad Kaufman 
90319aefa45SLiad Kaufman 	if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
90419aefa45SLiad Kaufman 		return;
90519aefa45SLiad Kaufman 
90619aefa45SLiad Kaufman 	/* Find any TID for queue */
90719aefa45SLiad Kaufman 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
90819aefa45SLiad Kaufman 	cmd.tid = tid;
90919aefa45SLiad Kaufman 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
91019aefa45SLiad Kaufman 
91119aefa45SLiad Kaufman 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
912341ca402SLiad Kaufman 	if (ret) {
91319aefa45SLiad Kaufman 		IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
91419aefa45SLiad Kaufman 			queue, ret);
915341ca402SLiad Kaufman 		return;
916341ca402SLiad Kaufman 	}
917341ca402SLiad Kaufman 
918341ca402SLiad Kaufman 	mvm->queue_info[queue].txq_tid = tid;
91919aefa45SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
92019aefa45SLiad Kaufman 			    queue, tid);
92119aefa45SLiad Kaufman }
92219aefa45SLiad Kaufman 
9239f9af3d7SLiad Kaufman static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
9249f9af3d7SLiad Kaufman {
9259f9af3d7SLiad Kaufman 	struct ieee80211_sta *sta;
9269f9af3d7SLiad Kaufman 	struct iwl_mvm_sta *mvmsta;
927806911daSSharon Dvir 	u8 sta_id;
9289f9af3d7SLiad Kaufman 	int tid = -1;
9299f9af3d7SLiad Kaufman 	unsigned long tid_bitmap;
9309f9af3d7SLiad Kaufman 	unsigned int wdg_timeout;
9319f9af3d7SLiad Kaufman 	int ssn;
9329f9af3d7SLiad Kaufman 	int ret = true;
9339f9af3d7SLiad Kaufman 
934bb49701bSSara Sharon 	/* queue sharing is disabled on new TX path */
935bb49701bSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
936bb49701bSSara Sharon 		return;
937bb49701bSSara Sharon 
9389f9af3d7SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
9399f9af3d7SLiad Kaufman 
9409f9af3d7SLiad Kaufman 	sta_id = mvm->queue_info[queue].ra_sta_id;
9419f9af3d7SLiad Kaufman 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
9429f9af3d7SLiad Kaufman 
9439f9af3d7SLiad Kaufman 	/* Find TID for queue, and make sure it is the only one on the queue */
9449f9af3d7SLiad Kaufman 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
9459f9af3d7SLiad Kaufman 	if (tid_bitmap != BIT(tid)) {
9469f9af3d7SLiad Kaufman 		IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
9479f9af3d7SLiad Kaufman 			queue, tid_bitmap);
9489f9af3d7SLiad Kaufman 		return;
9499f9af3d7SLiad Kaufman 	}
9509f9af3d7SLiad Kaufman 
9519f9af3d7SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
9529f9af3d7SLiad Kaufman 			    tid);
9539f9af3d7SLiad Kaufman 
9549f9af3d7SLiad Kaufman 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
9559f9af3d7SLiad Kaufman 					lockdep_is_held(&mvm->mutex));
9569f9af3d7SLiad Kaufman 
9579f9af3d7SLiad Kaufman 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
9589f9af3d7SLiad Kaufman 		return;
9599f9af3d7SLiad Kaufman 
9609f9af3d7SLiad Kaufman 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
9619f9af3d7SLiad Kaufman 	wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
9629f9af3d7SLiad Kaufman 
9639f9af3d7SLiad Kaufman 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
9649f9af3d7SLiad Kaufman 
965cfbc6c4cSSara Sharon 	ret = iwl_mvm_redirect_queue(mvm, queue, tid,
9669f9af3d7SLiad Kaufman 				     tid_to_mac80211_ac[tid], ssn,
967cfbc6c4cSSara Sharon 				     wdg_timeout, true,
968cfbc6c4cSSara Sharon 				     iwl_mvm_txq_from_tid(sta, tid));
9699f9af3d7SLiad Kaufman 	if (ret) {
9709f9af3d7SLiad Kaufman 		IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
9719f9af3d7SLiad Kaufman 		return;
9729f9af3d7SLiad Kaufman 	}
9739f9af3d7SLiad Kaufman 
9749f9af3d7SLiad Kaufman 	/* If aggs should be turned back on - do it */
9759f9af3d7SLiad Kaufman 	if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
9769cd70e80SEmmanuel Grumbach 		struct iwl_mvm_add_sta_cmd cmd = {0};
9779f9af3d7SLiad Kaufman 
9789f9af3d7SLiad Kaufman 		mvmsta->tid_disable_agg &= ~BIT(tid);
9799f9af3d7SLiad Kaufman 
9809f9af3d7SLiad Kaufman 		cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
9819f9af3d7SLiad Kaufman 		cmd.sta_id = mvmsta->sta_id;
9829f9af3d7SLiad Kaufman 		cmd.add_modify = STA_MODE_MODIFY;
9839f9af3d7SLiad Kaufman 		cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
9849f9af3d7SLiad Kaufman 		cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
9859f9af3d7SLiad Kaufman 		cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
9869f9af3d7SLiad Kaufman 
9879f9af3d7SLiad Kaufman 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
9889f9af3d7SLiad Kaufman 					   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
9899f9af3d7SLiad Kaufman 		if (!ret) {
9909f9af3d7SLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm,
9919f9af3d7SLiad Kaufman 					    "TXQ #%d is now aggregated again\n",
9929f9af3d7SLiad Kaufman 					    queue);
9939f9af3d7SLiad Kaufman 
9949f9af3d7SLiad Kaufman 			/* Mark queue intenally as aggregating again */
9959f9af3d7SLiad Kaufman 			iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
9969f9af3d7SLiad Kaufman 		}
9979f9af3d7SLiad Kaufman 	}
9989f9af3d7SLiad Kaufman 
9999f9af3d7SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
10009f9af3d7SLiad Kaufman }
10019f9af3d7SLiad Kaufman 
100299448a8cSJohannes Berg /*
100399448a8cSJohannes Berg  * Remove inactive TIDs of a given queue.
100499448a8cSJohannes Berg  * If all queue TIDs are inactive - mark the queue as inactive
100599448a8cSJohannes Berg  * If only some the queue TIDs are inactive - unmap them from the queue
1006724fe771SJohannes Berg  *
1007724fe771SJohannes Berg  * Returns %true if all TIDs were removed and the queue could be reused.
100899448a8cSJohannes Berg  */
1009724fe771SJohannes Berg static bool iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
101099448a8cSJohannes Berg 					 struct iwl_mvm_sta *mvmsta, int queue,
101190d2d94cSJohannes Berg 					 unsigned long tid_bitmap,
1012b3a87f11SJohannes Berg 					 unsigned long *unshare_queues,
1013b3a87f11SJohannes Berg 					 unsigned long *changetid_queues)
101499448a8cSJohannes Berg {
101599448a8cSJohannes Berg 	int tid;
101699448a8cSJohannes Berg 
101799448a8cSJohannes Berg 	lockdep_assert_held(&mvmsta->lock);
1018f3f240f9SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
101999448a8cSJohannes Berg 
102099448a8cSJohannes Berg 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1021724fe771SJohannes Berg 		return false;
102299448a8cSJohannes Berg 
102399448a8cSJohannes Berg 	/* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
102499448a8cSJohannes Berg 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
102599448a8cSJohannes Berg 		/* If some TFDs are still queued - don't mark TID as inactive */
102699448a8cSJohannes Berg 		if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid]))
102799448a8cSJohannes Berg 			tid_bitmap &= ~BIT(tid);
102899448a8cSJohannes Berg 
102999448a8cSJohannes Berg 		/* Don't mark as inactive any TID that has an active BA */
103099448a8cSJohannes Berg 		if (mvmsta->tid_data[tid].state != IWL_AGG_OFF)
103199448a8cSJohannes Berg 			tid_bitmap &= ~BIT(tid);
103299448a8cSJohannes Berg 	}
103399448a8cSJohannes Berg 
1034724fe771SJohannes Berg 	/* If all TIDs in the queue are inactive - return it can be reused */
103599448a8cSJohannes Berg 	if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
1036724fe771SJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Queue %d is inactive\n", queue);
1037724fe771SJohannes Berg 		return true;
103899448a8cSJohannes Berg 	}
103999448a8cSJohannes Berg 
104099448a8cSJohannes Berg 	/*
104199448a8cSJohannes Berg 	 * If we are here, this is a shared queue and not all TIDs timed-out.
104299448a8cSJohannes Berg 	 * Remove the ones that did.
104399448a8cSJohannes Berg 	 */
104499448a8cSJohannes Berg 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1045d4530f63SJohannes Berg 		u16 q_tid_bitmap;
104699448a8cSJohannes Berg 
104799448a8cSJohannes Berg 		mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
104899448a8cSJohannes Berg 		mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
104999448a8cSJohannes Berg 
1050d4530f63SJohannes Berg 		q_tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1051b3a87f11SJohannes Berg 
1052b3a87f11SJohannes Berg 		/*
1053b3a87f11SJohannes Berg 		 * We need to take into account a situation in which a TXQ was
1054b3a87f11SJohannes Berg 		 * allocated to TID x, and then turned shared by adding TIDs y
1055b3a87f11SJohannes Berg 		 * and z. If TID x becomes inactive and is removed from the TXQ,
1056b3a87f11SJohannes Berg 		 * ownership must be given to one of the remaining TIDs.
1057b3a87f11SJohannes Berg 		 * This is mainly because if TID x continues - a new queue can't
1058b3a87f11SJohannes Berg 		 * be allocated for it as long as it is an owner of another TXQ.
1059b3a87f11SJohannes Berg 		 *
1060b3a87f11SJohannes Berg 		 * Mark this queue in the right bitmap, we'll send the command
1061b3a87f11SJohannes Berg 		 * to the firmware later.
1062b3a87f11SJohannes Berg 		 */
1063d4530f63SJohannes Berg 		if (!(q_tid_bitmap & BIT(mvm->queue_info[queue].txq_tid)))
1064b3a87f11SJohannes Berg 			set_bit(queue, changetid_queues);
1065b3a87f11SJohannes Berg 
106699448a8cSJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm,
106799448a8cSJohannes Berg 				    "Removing inactive TID %d from shared Q:%d\n",
106899448a8cSJohannes Berg 				    tid, queue);
106999448a8cSJohannes Berg 	}
107099448a8cSJohannes Berg 
107199448a8cSJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
107299448a8cSJohannes Berg 			    "TXQ #%d left with tid bitmap 0x%x\n", queue,
107399448a8cSJohannes Berg 			    mvm->queue_info[queue].tid_bitmap);
107499448a8cSJohannes Berg 
107599448a8cSJohannes Berg 	/*
107699448a8cSJohannes Berg 	 * There may be different TIDs with the same mac queues, so make
107799448a8cSJohannes Berg 	 * sure all TIDs have existing corresponding mac queues enabled
107899448a8cSJohannes Berg 	 */
107999448a8cSJohannes Berg 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
108099448a8cSJohannes Berg 
108199448a8cSJohannes Berg 	/* If the queue is marked as shared - "unshare" it */
10821c14089eSJohannes Berg 	if (hweight16(mvm->queue_info[queue].tid_bitmap) == 1 &&
108399448a8cSJohannes Berg 	    mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
108499448a8cSJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
108599448a8cSJohannes Berg 				    queue);
108690d2d94cSJohannes Berg 		set_bit(queue, unshare_queues);
108799448a8cSJohannes Berg 	}
1088724fe771SJohannes Berg 
1089724fe771SJohannes Berg 	return false;
109099448a8cSJohannes Berg }
109199448a8cSJohannes Berg 
1092724fe771SJohannes Berg /*
1093724fe771SJohannes Berg  * Check for inactivity - this includes checking if any queue
1094724fe771SJohannes Berg  * can be unshared and finding one (and only one) that can be
1095724fe771SJohannes Berg  * reused.
1096724fe771SJohannes Berg  * This function is also invoked as a sort of clean-up task,
1097724fe771SJohannes Berg  * in which case @alloc_for_sta is IWL_MVM_INVALID_STA.
1098724fe771SJohannes Berg  *
1099724fe771SJohannes Berg  * Returns the queue number, or -ENOSPC.
1100724fe771SJohannes Berg  */
1101724fe771SJohannes Berg static int iwl_mvm_inactivity_check(struct iwl_mvm *mvm, u8 alloc_for_sta)
110299448a8cSJohannes Berg {
110399448a8cSJohannes Berg 	unsigned long now = jiffies;
110490d2d94cSJohannes Berg 	unsigned long unshare_queues = 0;
1105b3a87f11SJohannes Berg 	unsigned long changetid_queues = 0;
1106724fe771SJohannes Berg 	int i, ret, free_queue = -ENOSPC;
1107cfbc6c4cSSara Sharon 	struct ieee80211_sta *queue_owner  = NULL;
110899448a8cSJohannes Berg 
1109df2a2245SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
1110df2a2245SJohannes Berg 
111199448a8cSJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm))
1112724fe771SJohannes Berg 		return -ENOSPC;
111399448a8cSJohannes Berg 
111499448a8cSJohannes Berg 	rcu_read_lock();
111599448a8cSJohannes Berg 
1116459ab045SJohannes Berg 	/* we skip the CMD queue below by starting at 1 */
1117459ab045SJohannes Berg 	BUILD_BUG_ON(IWL_MVM_DQA_CMD_QUEUE != 0);
1118459ab045SJohannes Berg 
1119459ab045SJohannes Berg 	for (i = 1; i < IWL_MAX_HW_QUEUES; i++) {
112099448a8cSJohannes Berg 		struct ieee80211_sta *sta;
112199448a8cSJohannes Berg 		struct iwl_mvm_sta *mvmsta;
112299448a8cSJohannes Berg 		u8 sta_id;
112399448a8cSJohannes Berg 		int tid;
112499448a8cSJohannes Berg 		unsigned long inactive_tid_bitmap = 0;
112599448a8cSJohannes Berg 		unsigned long queue_tid_bitmap;
112699448a8cSJohannes Berg 
112799448a8cSJohannes Berg 		queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
1128459ab045SJohannes Berg 		if (!queue_tid_bitmap)
1129459ab045SJohannes Berg 			continue;
113099448a8cSJohannes Berg 
113199448a8cSJohannes Berg 		/* If TXQ isn't in active use anyway - nothing to do here... */
113299448a8cSJohannes Berg 		if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
1133459ab045SJohannes Berg 		    mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED)
113499448a8cSJohannes Berg 			continue;
113599448a8cSJohannes Berg 
113699448a8cSJohannes Berg 		/* Check to see if there are inactive TIDs on this queue */
113799448a8cSJohannes Berg 		for_each_set_bit(tid, &queue_tid_bitmap,
113899448a8cSJohannes Berg 				 IWL_MAX_TID_COUNT + 1) {
113999448a8cSJohannes Berg 			if (time_after(mvm->queue_info[i].last_frame_time[tid] +
114099448a8cSJohannes Berg 				       IWL_MVM_DQA_QUEUE_TIMEOUT, now))
114199448a8cSJohannes Berg 				continue;
114299448a8cSJohannes Berg 
114399448a8cSJohannes Berg 			inactive_tid_bitmap |= BIT(tid);
114499448a8cSJohannes Berg 		}
114599448a8cSJohannes Berg 
114699448a8cSJohannes Berg 		/* If all TIDs are active - finish check on this queue */
114799448a8cSJohannes Berg 		if (!inactive_tid_bitmap)
114899448a8cSJohannes Berg 			continue;
114999448a8cSJohannes Berg 
115099448a8cSJohannes Berg 		/*
115199448a8cSJohannes Berg 		 * If we are here - the queue hadn't been served recently and is
115299448a8cSJohannes Berg 		 * in use
115399448a8cSJohannes Berg 		 */
115499448a8cSJohannes Berg 
115599448a8cSJohannes Berg 		sta_id = mvm->queue_info[i].ra_sta_id;
115699448a8cSJohannes Berg 		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
115799448a8cSJohannes Berg 
115899448a8cSJohannes Berg 		/*
115999448a8cSJohannes Berg 		 * If the STA doesn't exist anymore, it isn't an error. It could
116099448a8cSJohannes Berg 		 * be that it was removed since getting the queues, and in this
116199448a8cSJohannes Berg 		 * case it should've inactivated its queues anyway.
116299448a8cSJohannes Berg 		 */
116399448a8cSJohannes Berg 		if (IS_ERR_OR_NULL(sta))
116499448a8cSJohannes Berg 			continue;
116599448a8cSJohannes Berg 
116699448a8cSJohannes Berg 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
116799448a8cSJohannes Berg 
1168f3f240f9SJohannes Berg 		spin_lock_bh(&mvmsta->lock);
1169724fe771SJohannes Berg 		ret = iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
117090d2d94cSJohannes Berg 						   inactive_tid_bitmap,
1171b3a87f11SJohannes Berg 						   &unshare_queues,
1172b3a87f11SJohannes Berg 						   &changetid_queues);
1173e6d419f9SJohannes Berg 		if (ret && free_queue < 0) {
1174cfbc6c4cSSara Sharon 			queue_owner = sta;
1175e6d419f9SJohannes Berg 			free_queue = i;
1176cfbc6c4cSSara Sharon 		}
1177459ab045SJohannes Berg 		/* only unlock sta lock - we still need the queue info lock */
1178f3f240f9SJohannes Berg 		spin_unlock_bh(&mvmsta->lock);
117999448a8cSJohannes Berg 	}
118099448a8cSJohannes Berg 
1181df2a2245SJohannes Berg 
1182df2a2245SJohannes Berg 	/* Reconfigure queues requiring reconfiguation */
118390d2d94cSJohannes Berg 	for_each_set_bit(i, &unshare_queues, IWL_MAX_HW_QUEUES)
118490d2d94cSJohannes Berg 		iwl_mvm_unshare_queue(mvm, i);
1185b3a87f11SJohannes Berg 	for_each_set_bit(i, &changetid_queues, IWL_MAX_HW_QUEUES)
1186b3a87f11SJohannes Berg 		iwl_mvm_change_queue_tid(mvm, i);
1187724fe771SJohannes Berg 
1188fbb1461aSJohannes Berg 	rcu_read_unlock();
1189fbb1461aSJohannes Berg 
1190724fe771SJohannes Berg 	if (free_queue >= 0 && alloc_for_sta != IWL_MVM_INVALID_STA) {
1191cfbc6c4cSSara Sharon 		ret = iwl_mvm_free_inactive_queue(mvm, free_queue, queue_owner,
1192724fe771SJohannes Berg 						  alloc_for_sta);
1193fbb1461aSJohannes Berg 		if (ret)
1194724fe771SJohannes Berg 			return ret;
1195724fe771SJohannes Berg 	}
1196724fe771SJohannes Berg 
1197724fe771SJohannes Berg 	return free_queue;
119899448a8cSJohannes Berg }
119999448a8cSJohannes Berg 
1200c20e08b0SJohannes Berg static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
1201cfbc6c4cSSara Sharon 				   struct ieee80211_sta *sta, u8 ac, int tid)
1202c20e08b0SJohannes Berg {
1203c20e08b0SJohannes Berg 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1204c20e08b0SJohannes Berg 	struct iwl_trans_txq_scd_cfg cfg = {
1205c20e08b0SJohannes Berg 		.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
1206c20e08b0SJohannes Berg 		.sta_id = mvmsta->sta_id,
1207c20e08b0SJohannes Berg 		.tid = tid,
1208c20e08b0SJohannes Berg 		.frame_limit = IWL_FRAME_LIMIT,
1209c20e08b0SJohannes Berg 	};
1210c20e08b0SJohannes Berg 	unsigned int wdg_timeout =
1211c20e08b0SJohannes Berg 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1212c20e08b0SJohannes Berg 	int queue = -1;
1213c6ce1c74SJohannes Berg 	u16 queue_tmp;
1214c20e08b0SJohannes Berg 	unsigned long disable_agg_tids = 0;
1215c20e08b0SJohannes Berg 	enum iwl_mvm_agg_state queue_state;
1216c20e08b0SJohannes Berg 	bool shared_queue = false, inc_ssn;
1217c20e08b0SJohannes Berg 	int ssn;
1218c20e08b0SJohannes Berg 	unsigned long tfd_queue_mask;
1219c20e08b0SJohannes Berg 	int ret;
1220c20e08b0SJohannes Berg 
1221c20e08b0SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
1222c20e08b0SJohannes Berg 
1223c20e08b0SJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm))
1224c20e08b0SJohannes Berg 		return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
1225c20e08b0SJohannes Berg 
1226c20e08b0SJohannes Berg 	spin_lock_bh(&mvmsta->lock);
1227c20e08b0SJohannes Berg 	tfd_queue_mask = mvmsta->tfd_queue_msk;
122835739348SSara Sharon 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1229c20e08b0SJohannes Berg 	spin_unlock_bh(&mvmsta->lock);
1230c20e08b0SJohannes Berg 
1231cfbc6c4cSSara Sharon 	if (tid == IWL_MAX_TID_COUNT) {
1232c20e08b0SJohannes Berg 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1233c20e08b0SJohannes Berg 						IWL_MVM_DQA_MIN_MGMT_QUEUE,
1234c20e08b0SJohannes Berg 						IWL_MVM_DQA_MAX_MGMT_QUEUE);
1235c20e08b0SJohannes Berg 		if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
1236c20e08b0SJohannes Berg 			IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
1237c20e08b0SJohannes Berg 					    queue);
1238c20e08b0SJohannes Berg 
1239c20e08b0SJohannes Berg 		/* If no such queue is found, we'll use a DATA queue instead */
1240c20e08b0SJohannes Berg 	}
1241c20e08b0SJohannes Berg 
1242c20e08b0SJohannes Berg 	if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
1243c20e08b0SJohannes Berg 	    (mvm->queue_info[mvmsta->reserved_queue].status ==
1244724fe771SJohannes Berg 			IWL_MVM_QUEUE_RESERVED)) {
1245c20e08b0SJohannes Berg 		queue = mvmsta->reserved_queue;
1246c20e08b0SJohannes Berg 		mvm->queue_info[queue].reserved = true;
1247c20e08b0SJohannes Berg 		IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
1248c20e08b0SJohannes Berg 	}
1249c20e08b0SJohannes Berg 
1250c20e08b0SJohannes Berg 	if (queue < 0)
1251c20e08b0SJohannes Berg 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1252c20e08b0SJohannes Berg 						IWL_MVM_DQA_MIN_DATA_QUEUE,
1253c20e08b0SJohannes Berg 						IWL_MVM_DQA_MAX_DATA_QUEUE);
1254724fe771SJohannes Berg 	if (queue < 0) {
1255724fe771SJohannes Berg 		/* try harder - perhaps kill an inactive queue */
1256724fe771SJohannes Berg 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1257c20e08b0SJohannes Berg 	}
1258c20e08b0SJohannes Berg 
1259c20e08b0SJohannes Berg 	/* No free queue - we'll have to share */
1260c20e08b0SJohannes Berg 	if (queue <= 0) {
1261c20e08b0SJohannes Berg 		queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
1262c20e08b0SJohannes Berg 		if (queue > 0) {
1263c20e08b0SJohannes Berg 			shared_queue = true;
1264c20e08b0SJohannes Berg 			mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
1265c20e08b0SJohannes Berg 		}
1266c20e08b0SJohannes Berg 	}
1267c20e08b0SJohannes Berg 
1268c20e08b0SJohannes Berg 	/*
1269c20e08b0SJohannes Berg 	 * Mark TXQ as ready, even though it hasn't been fully configured yet,
1270c20e08b0SJohannes Berg 	 * to make sure no one else takes it.
1271c20e08b0SJohannes Berg 	 * This will allow avoiding re-acquiring the lock at the end of the
1272c20e08b0SJohannes Berg 	 * configuration. On error we'll mark it back as free.
1273c20e08b0SJohannes Berg 	 */
1274c20e08b0SJohannes Berg 	if (queue > 0 && !shared_queue)
1275c20e08b0SJohannes Berg 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1276c20e08b0SJohannes Berg 
1277c20e08b0SJohannes Berg 	/* This shouldn't happen - out of queues */
1278c20e08b0SJohannes Berg 	if (WARN_ON(queue <= 0)) {
1279c20e08b0SJohannes Berg 		IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
1280c20e08b0SJohannes Berg 			tid, cfg.sta_id);
1281c20e08b0SJohannes Berg 		return queue;
1282c20e08b0SJohannes Berg 	}
1283c20e08b0SJohannes Berg 
1284c20e08b0SJohannes Berg 	/*
1285c20e08b0SJohannes Berg 	 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
1286c20e08b0SJohannes Berg 	 * but for configuring the SCD to send A-MPDUs we need to mark the queue
1287c20e08b0SJohannes Berg 	 * as aggregatable.
1288c20e08b0SJohannes Berg 	 * Mark all DATA queues as allowing to be aggregated at some point
1289c20e08b0SJohannes Berg 	 */
1290c20e08b0SJohannes Berg 	cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1291c20e08b0SJohannes Berg 			 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1292c20e08b0SJohannes Berg 
1293c20e08b0SJohannes Berg 	IWL_DEBUG_TX_QUEUES(mvm,
1294c20e08b0SJohannes Berg 			    "Allocating %squeue #%d to sta %d on tid %d\n",
1295c20e08b0SJohannes Berg 			    shared_queue ? "shared " : "", queue,
1296c20e08b0SJohannes Berg 			    mvmsta->sta_id, tid);
1297c20e08b0SJohannes Berg 
1298c20e08b0SJohannes Berg 	if (shared_queue) {
1299c20e08b0SJohannes Berg 		/* Disable any open aggs on this queue */
1300c20e08b0SJohannes Berg 		disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
1301c20e08b0SJohannes Berg 
1302c20e08b0SJohannes Berg 		if (disable_agg_tids) {
1303c20e08b0SJohannes Berg 			IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
1304c20e08b0SJohannes Berg 					    queue);
1305c20e08b0SJohannes Berg 			iwl_mvm_invalidate_sta_queue(mvm, queue,
1306c20e08b0SJohannes Berg 						     disable_agg_tids, false);
1307c20e08b0SJohannes Berg 		}
1308c20e08b0SJohannes Berg 	}
1309c20e08b0SJohannes Berg 
1310cfbc6c4cSSara Sharon 	inc_ssn = iwl_mvm_enable_txq(mvm, sta, queue, ssn, &cfg, wdg_timeout);
1311c20e08b0SJohannes Berg 
1312c20e08b0SJohannes Berg 	/*
1313c20e08b0SJohannes Berg 	 * Mark queue as shared in transport if shared
1314c20e08b0SJohannes Berg 	 * Note this has to be done after queue enablement because enablement
1315c20e08b0SJohannes Berg 	 * can also set this value, and there is no indication there to shared
1316c20e08b0SJohannes Berg 	 * queues
1317c20e08b0SJohannes Berg 	 */
1318c20e08b0SJohannes Berg 	if (shared_queue)
1319c20e08b0SJohannes Berg 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
1320c20e08b0SJohannes Berg 
1321c20e08b0SJohannes Berg 	spin_lock_bh(&mvmsta->lock);
1322c20e08b0SJohannes Berg 	/*
1323c20e08b0SJohannes Berg 	 * This looks racy, but it is not. We have only one packet for
1324c20e08b0SJohannes Berg 	 * this ra/tid in our Tx path since we stop the Qdisc when we
1325c20e08b0SJohannes Berg 	 * need to allocate a new TFD queue.
1326c20e08b0SJohannes Berg 	 */
132735739348SSara Sharon 	if (inc_ssn) {
1328c20e08b0SJohannes Berg 		mvmsta->tid_data[tid].seq_number += 0x10;
132935739348SSara Sharon 		ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
133035739348SSara Sharon 	}
1331c20e08b0SJohannes Berg 	mvmsta->tid_data[tid].txq_id = queue;
1332c20e08b0SJohannes Berg 	mvmsta->tfd_queue_msk |= BIT(queue);
1333c20e08b0SJohannes Berg 	queue_state = mvmsta->tid_data[tid].state;
1334c20e08b0SJohannes Berg 
1335c20e08b0SJohannes Berg 	if (mvmsta->reserved_queue == queue)
1336c20e08b0SJohannes Berg 		mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
1337c20e08b0SJohannes Berg 	spin_unlock_bh(&mvmsta->lock);
1338c20e08b0SJohannes Berg 
1339c20e08b0SJohannes Berg 	if (!shared_queue) {
1340c20e08b0SJohannes Berg 		ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
1341c20e08b0SJohannes Berg 		if (ret)
1342c20e08b0SJohannes Berg 			goto out_err;
1343c20e08b0SJohannes Berg 
1344c20e08b0SJohannes Berg 		/* If we need to re-enable aggregations... */
1345c20e08b0SJohannes Berg 		if (queue_state == IWL_AGG_ON) {
1346c20e08b0SJohannes Berg 			ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1347c20e08b0SJohannes Berg 			if (ret)
1348c20e08b0SJohannes Berg 				goto out_err;
1349c20e08b0SJohannes Berg 		}
1350c20e08b0SJohannes Berg 	} else {
1351c20e08b0SJohannes Berg 		/* Redirect queue, if needed */
1352cfbc6c4cSSara Sharon 		ret = iwl_mvm_redirect_queue(mvm, queue, tid, ac, ssn,
1353cfbc6c4cSSara Sharon 					     wdg_timeout, false,
1354cfbc6c4cSSara Sharon 					     iwl_mvm_txq_from_tid(sta, tid));
1355c20e08b0SJohannes Berg 		if (ret)
1356c20e08b0SJohannes Berg 			goto out_err;
1357c20e08b0SJohannes Berg 	}
1358c20e08b0SJohannes Berg 
1359c20e08b0SJohannes Berg 	return 0;
1360c20e08b0SJohannes Berg 
1361c20e08b0SJohannes Berg out_err:
1362c6ce1c74SJohannes Berg 	queue_tmp = queue;
1363c6ce1c74SJohannes Berg 	iwl_mvm_disable_txq(mvm, sta, &queue_tmp, tid, 0);
1364c20e08b0SJohannes Berg 
1365c20e08b0SJohannes Berg 	return ret;
1366c20e08b0SJohannes Berg }
1367c20e08b0SJohannes Berg 
136824afba76SLiad Kaufman void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
136924afba76SLiad Kaufman {
137024afba76SLiad Kaufman 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
137124afba76SLiad Kaufman 					   add_stream_wk);
137224afba76SLiad Kaufman 
137324afba76SLiad Kaufman 	mutex_lock(&mvm->mutex);
137424afba76SLiad Kaufman 
1375724fe771SJohannes Berg 	iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
13769f9af3d7SLiad Kaufman 
1377cfbc6c4cSSara Sharon 	while (!list_empty(&mvm->add_stream_txqs)) {
1378cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq;
1379cfbc6c4cSSara Sharon 		struct ieee80211_txq *txq;
1380cfbc6c4cSSara Sharon 		u8 tid;
138124afba76SLiad Kaufman 
1382cfbc6c4cSSara Sharon 		mvmtxq = list_first_entry(&mvm->add_stream_txqs,
1383cfbc6c4cSSara Sharon 					  struct iwl_mvm_txq, list);
138424afba76SLiad Kaufman 
1385cfbc6c4cSSara Sharon 		txq = container_of((void *)mvmtxq, struct ieee80211_txq,
1386cfbc6c4cSSara Sharon 				   drv_priv);
1387cfbc6c4cSSara Sharon 		tid = txq->tid;
1388cfbc6c4cSSara Sharon 		if (tid == IEEE80211_NUM_TIDS)
1389cfbc6c4cSSara Sharon 			tid = IWL_MAX_TID_COUNT;
1390cfbc6c4cSSara Sharon 
13913d1d87abSJohannes Berg 		/*
13923d1d87abSJohannes Berg 		 * We can't really do much here, but if this fails we can't
13933d1d87abSJohannes Berg 		 * transmit anyway - so just don't transmit the frame etc.
13943d1d87abSJohannes Berg 		 * and let them back up ... we've tried our best to allocate
13953d1d87abSJohannes Berg 		 * a queue in the function itself.
13963d1d87abSJohannes Berg 		 */
13973d1d87abSJohannes Berg 		if (iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, tid)) {
13983d1d87abSJohannes Berg 			list_del_init(&mvmtxq->list);
13993d1d87abSJohannes Berg 			continue;
14003d1d87abSJohannes Berg 		}
14013d1d87abSJohannes Berg 
1402cfbc6c4cSSara Sharon 		list_del_init(&mvmtxq->list);
1403f5ae2f93SJohannes Berg 		local_bh_disable();
1404cfbc6c4cSSara Sharon 		iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1405f5ae2f93SJohannes Berg 		local_bh_enable();
140624afba76SLiad Kaufman 	}
140724afba76SLiad Kaufman 
140824afba76SLiad Kaufman 	mutex_unlock(&mvm->mutex);
140924afba76SLiad Kaufman }
141024afba76SLiad Kaufman 
141124afba76SLiad Kaufman static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1412d5216a28SLiad Kaufman 				      struct ieee80211_sta *sta,
1413d5216a28SLiad Kaufman 				      enum nl80211_iftype vif_type)
141424afba76SLiad Kaufman {
141524afba76SLiad Kaufman 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
141624afba76SLiad Kaufman 	int queue;
141724afba76SLiad Kaufman 
1418396952eeSSara Sharon 	/* queue reserving is disabled on new TX path */
1419396952eeSSara Sharon 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1420396952eeSSara Sharon 		return 0;
1421396952eeSSara Sharon 
1422724fe771SJohannes Berg 	/* run the general cleanup/unsharing of queues */
1423724fe771SJohannes Berg 	iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
14249794c64fSLiad Kaufman 
142524afba76SLiad Kaufman 	/* Make sure we have free resources for this STA */
1426d5216a28SLiad Kaufman 	if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
14271c14089eSJohannes Berg 	    !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].tid_bitmap &&
1428cf961e16SLiad Kaufman 	    (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1429cf961e16SLiad Kaufman 	     IWL_MVM_QUEUE_FREE))
1430d5216a28SLiad Kaufman 		queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1431d5216a28SLiad Kaufman 	else
14329794c64fSLiad Kaufman 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
14339794c64fSLiad Kaufman 						IWL_MVM_DQA_MIN_DATA_QUEUE,
143424afba76SLiad Kaufman 						IWL_MVM_DQA_MAX_DATA_QUEUE);
143524afba76SLiad Kaufman 	if (queue < 0) {
1436724fe771SJohannes Berg 		/* try again - this time kick out a queue if needed */
1437724fe771SJohannes Berg 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1438724fe771SJohannes Berg 		if (queue < 0) {
143924afba76SLiad Kaufman 			IWL_ERR(mvm, "No available queues for new station\n");
144024afba76SLiad Kaufman 			return -ENOSPC;
1441724fe771SJohannes Berg 		}
144224afba76SLiad Kaufman 	}
1443cf961e16SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
144424afba76SLiad Kaufman 
144524afba76SLiad Kaufman 	mvmsta->reserved_queue = queue;
144624afba76SLiad Kaufman 
144724afba76SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
144824afba76SLiad Kaufman 			    queue, mvmsta->sta_id);
144924afba76SLiad Kaufman 
145024afba76SLiad Kaufman 	return 0;
145124afba76SLiad Kaufman }
145224afba76SLiad Kaufman 
14538d98ae6eSLiad Kaufman /*
14548d98ae6eSLiad Kaufman  * In DQA mode, after a HW restart the queues should be allocated as before, in
14558d98ae6eSLiad Kaufman  * order to avoid race conditions when there are shared queues. This function
14568d98ae6eSLiad Kaufman  * does the re-mapping and queue allocation.
14578d98ae6eSLiad Kaufman  *
14588d98ae6eSLiad Kaufman  * Note that re-enabling aggregations isn't done in this function.
14598d98ae6eSLiad Kaufman  */
14608d98ae6eSLiad Kaufman static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1461cfbc6c4cSSara Sharon 						 struct ieee80211_sta *sta)
14628d98ae6eSLiad Kaufman {
1463cfbc6c4cSSara Sharon 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1464cfbc6c4cSSara Sharon 	unsigned int wdg =
14658d98ae6eSLiad Kaufman 		iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
14668d98ae6eSLiad Kaufman 	int i;
14678d98ae6eSLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
14688d98ae6eSLiad Kaufman 		.sta_id = mvm_sta->sta_id,
14698d98ae6eSLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
14708d98ae6eSLiad Kaufman 	};
14718d98ae6eSLiad Kaufman 
147203c902bfSJohannes Berg 	/* Make sure reserved queue is still marked as such (if allocated) */
147303c902bfSJohannes Berg 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
14748d98ae6eSLiad Kaufman 		mvm->queue_info[mvm_sta->reserved_queue].status =
14758d98ae6eSLiad Kaufman 			IWL_MVM_QUEUE_RESERVED;
14768d98ae6eSLiad Kaufman 
14778d98ae6eSLiad Kaufman 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
14788d98ae6eSLiad Kaufman 		struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
14798d98ae6eSLiad Kaufman 		int txq_id = tid_data->txq_id;
14808d98ae6eSLiad Kaufman 		int ac;
14818d98ae6eSLiad Kaufman 
14826862fceeSSara Sharon 		if (txq_id == IWL_MVM_INVALID_QUEUE)
14838d98ae6eSLiad Kaufman 			continue;
14848d98ae6eSLiad Kaufman 
14858d98ae6eSLiad Kaufman 		ac = tid_to_mac80211_ac[i];
14868d98ae6eSLiad Kaufman 
1487310181ecSSara Sharon 		if (iwl_mvm_has_new_tx_api(mvm)) {
1488310181ecSSara Sharon 			IWL_DEBUG_TX_QUEUES(mvm,
1489310181ecSSara Sharon 					    "Re-mapping sta %d tid %d\n",
1490310181ecSSara Sharon 					    mvm_sta->sta_id, i);
1491cfbc6c4cSSara Sharon 			txq_id = iwl_mvm_tvqm_enable_txq(mvm, mvm_sta->sta_id,
1492cfbc6c4cSSara Sharon 							 i, wdg);
149391cf5dedSJohannes Berg 			/*
149491cf5dedSJohannes Berg 			 * on failures, just set it to IWL_MVM_INVALID_QUEUE
149591cf5dedSJohannes Berg 			 * to try again later, we have no other good way of
149691cf5dedSJohannes Berg 			 * failing here
149791cf5dedSJohannes Berg 			 */
149891cf5dedSJohannes Berg 			if (txq_id < 0)
149991cf5dedSJohannes Berg 				txq_id = IWL_MVM_INVALID_QUEUE;
1500310181ecSSara Sharon 			tid_data->txq_id = txq_id;
15015d39051aSLiad Kaufman 
15025d39051aSLiad Kaufman 			/*
15035d39051aSLiad Kaufman 			 * Since we don't set the seq number after reset, and HW
15045d39051aSLiad Kaufman 			 * sets it now, FW reset will cause the seq num to start
15055d39051aSLiad Kaufman 			 * at 0 again, so driver will need to update it
15065d39051aSLiad Kaufman 			 * internally as well, so it keeps in sync with real val
15075d39051aSLiad Kaufman 			 */
15085d39051aSLiad Kaufman 			tid_data->seq_number = 0;
1509310181ecSSara Sharon 		} else {
1510310181ecSSara Sharon 			u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1511310181ecSSara Sharon 
15128d98ae6eSLiad Kaufman 			cfg.tid = i;
1513cf6c6ea3SEmmanuel Grumbach 			cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
15148d98ae6eSLiad Kaufman 			cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1515310181ecSSara Sharon 					 txq_id ==
1516310181ecSSara Sharon 					 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
15178d98ae6eSLiad Kaufman 
15188d98ae6eSLiad Kaufman 			IWL_DEBUG_TX_QUEUES(mvm,
15198d98ae6eSLiad Kaufman 					    "Re-mapping sta %d tid %d to queue %d\n",
15208d98ae6eSLiad Kaufman 					    mvm_sta->sta_id, i, txq_id);
15218d98ae6eSLiad Kaufman 
1522cfbc6c4cSSara Sharon 			iwl_mvm_enable_txq(mvm, sta, txq_id, seq, &cfg, wdg);
15238d98ae6eSLiad Kaufman 			mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
15248d98ae6eSLiad Kaufman 		}
152534e10860SSara Sharon 	}
15268d98ae6eSLiad Kaufman }
15278d98ae6eSLiad Kaufman 
1528732d06e9SShaul Triebitz static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1529732d06e9SShaul Triebitz 				      struct iwl_mvm_int_sta *sta,
1530732d06e9SShaul Triebitz 				      const u8 *addr,
1531732d06e9SShaul Triebitz 				      u16 mac_id, u16 color)
1532732d06e9SShaul Triebitz {
1533732d06e9SShaul Triebitz 	struct iwl_mvm_add_sta_cmd cmd;
1534732d06e9SShaul Triebitz 	int ret;
15353f497de9SLuca Coelho 	u32 status = ADD_STA_SUCCESS;
1536732d06e9SShaul Triebitz 
1537732d06e9SShaul Triebitz 	lockdep_assert_held(&mvm->mutex);
1538732d06e9SShaul Triebitz 
1539732d06e9SShaul Triebitz 	memset(&cmd, 0, sizeof(cmd));
1540732d06e9SShaul Triebitz 	cmd.sta_id = sta->sta_id;
15412c2c3647SNathan Errera 
1542971cbe50SJohannes Berg 	if (iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA, 0) >= 12 &&
15432c2c3647SNathan Errera 	    sta->type == IWL_STA_AUX_ACTIVITY)
15442c2c3647SNathan Errera 		cmd.mac_id_n_color = cpu_to_le32(mac_id);
15452c2c3647SNathan Errera 	else
1546732d06e9SShaul Triebitz 		cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1547732d06e9SShaul Triebitz 								     color));
15482c2c3647SNathan Errera 
1549732d06e9SShaul Triebitz 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1550732d06e9SShaul Triebitz 		cmd.station_type = sta->type;
1551732d06e9SShaul Triebitz 
1552732d06e9SShaul Triebitz 	if (!iwl_mvm_has_new_tx_api(mvm))
1553732d06e9SShaul Triebitz 		cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1554732d06e9SShaul Triebitz 	cmd.tid_disable_tx = cpu_to_le16(0xffff);
1555732d06e9SShaul Triebitz 
1556732d06e9SShaul Triebitz 	if (addr)
1557732d06e9SShaul Triebitz 		memcpy(cmd.addr, addr, ETH_ALEN);
1558732d06e9SShaul Triebitz 
1559732d06e9SShaul Triebitz 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1560732d06e9SShaul Triebitz 					  iwl_mvm_add_sta_cmd_size(mvm),
1561732d06e9SShaul Triebitz 					  &cmd, &status);
1562732d06e9SShaul Triebitz 	if (ret)
1563732d06e9SShaul Triebitz 		return ret;
1564732d06e9SShaul Triebitz 
1565732d06e9SShaul Triebitz 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1566732d06e9SShaul Triebitz 	case ADD_STA_SUCCESS:
1567732d06e9SShaul Triebitz 		IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1568732d06e9SShaul Triebitz 		return 0;
1569732d06e9SShaul Triebitz 	default:
1570732d06e9SShaul Triebitz 		ret = -EIO;
1571732d06e9SShaul Triebitz 		IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1572732d06e9SShaul Triebitz 			status);
1573732d06e9SShaul Triebitz 		break;
1574732d06e9SShaul Triebitz 	}
1575732d06e9SShaul Triebitz 	return ret;
1576732d06e9SShaul Triebitz }
1577732d06e9SShaul Triebitz 
1578e705c121SKalle Valo int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1579e705c121SKalle Valo 		    struct ieee80211_vif *vif,
1580e705c121SKalle Valo 		    struct ieee80211_sta *sta)
1581e705c121SKalle Valo {
1582e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1583e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1584a571f5f6SSara Sharon 	struct iwl_mvm_rxq_dup_data *dup_data;
1585e705c121SKalle Valo 	int i, ret, sta_id;
1586732d06e9SShaul Triebitz 	bool sta_update = false;
1587732d06e9SShaul Triebitz 	unsigned int sta_flags = 0;
1588e705c121SKalle Valo 
1589e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1590e705c121SKalle Valo 
1591e705c121SKalle Valo 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1592e705c121SKalle Valo 		sta_id = iwl_mvm_find_free_sta_id(mvm,
1593e705c121SKalle Valo 						  ieee80211_vif_type_p2p(vif));
1594e705c121SKalle Valo 	else
1595e705c121SKalle Valo 		sta_id = mvm_sta->sta_id;
1596e705c121SKalle Valo 
15970ae98812SSara Sharon 	if (sta_id == IWL_MVM_INVALID_STA)
1598e705c121SKalle Valo 		return -ENOSPC;
1599e705c121SKalle Valo 
1600e705c121SKalle Valo 	spin_lock_init(&mvm_sta->lock);
1601e705c121SKalle Valo 
1602c8f54701SJohannes Berg 	/* if this is a HW restart re-alloc existing queues */
1603c8f54701SJohannes Berg 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1604732d06e9SShaul Triebitz 		struct iwl_mvm_int_sta tmp_sta = {
1605732d06e9SShaul Triebitz 			.sta_id = sta_id,
1606732d06e9SShaul Triebitz 			.type = mvm_sta->sta_type,
1607732d06e9SShaul Triebitz 		};
1608732d06e9SShaul Triebitz 
1609732d06e9SShaul Triebitz 		/*
1610732d06e9SShaul Triebitz 		 * First add an empty station since allocating
1611732d06e9SShaul Triebitz 		 * a queue requires a valid station
1612732d06e9SShaul Triebitz 		 */
1613732d06e9SShaul Triebitz 		ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1614732d06e9SShaul Triebitz 						 mvmvif->id, mvmvif->color);
1615732d06e9SShaul Triebitz 		if (ret)
1616732d06e9SShaul Triebitz 			goto err;
1617732d06e9SShaul Triebitz 
1618cfbc6c4cSSara Sharon 		iwl_mvm_realloc_queues_after_restart(mvm, sta);
1619732d06e9SShaul Triebitz 		sta_update = true;
1620732d06e9SShaul Triebitz 		sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
16218d98ae6eSLiad Kaufman 		goto update_fw;
16228d98ae6eSLiad Kaufman 	}
16238d98ae6eSLiad Kaufman 
1624e705c121SKalle Valo 	mvm_sta->sta_id = sta_id;
1625e705c121SKalle Valo 	mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1626e705c121SKalle Valo 						      mvmvif->color);
1627e705c121SKalle Valo 	mvm_sta->vif = vif;
1628286ca8ebSLuca Coelho 	if (!mvm->trans->trans_cfg->gen2)
1629e705c121SKalle Valo 		mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1630a58bb468SLiad Kaufman 	else
1631a58bb468SLiad Kaufman 		mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
1632e705c121SKalle Valo 	mvm_sta->tx_protection = 0;
1633e705c121SKalle Valo 	mvm_sta->tt_tx_protection = false;
1634ced19f26SSara Sharon 	mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
1635e705c121SKalle Valo 
1636e705c121SKalle Valo 	/* HW restart, don't assume the memory has been zeroed */
1637e705c121SKalle Valo 	mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1638e705c121SKalle Valo 	mvm_sta->tfd_queue_msk = 0;
1639e705c121SKalle Valo 
1640e705c121SKalle Valo 	/* for HW restart - reset everything but the sequence number */
164124afba76SLiad Kaufman 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1642e705c121SKalle Valo 		u16 seq = mvm_sta->tid_data[i].seq_number;
1643e705c121SKalle Valo 		memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1644e705c121SKalle Valo 		mvm_sta->tid_data[i].seq_number = seq;
164524afba76SLiad Kaufman 
164624afba76SLiad Kaufman 		/*
164724afba76SLiad Kaufman 		 * Mark all queues for this STA as unallocated and defer TX
164824afba76SLiad Kaufman 		 * frames until the queue is allocated
164924afba76SLiad Kaufman 		 */
16506862fceeSSara Sharon 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1651e705c121SKalle Valo 	}
1652cfbc6c4cSSara Sharon 
1653cfbc6c4cSSara Sharon 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1654cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
1655cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1656cfbc6c4cSSara Sharon 
1657cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1658cfbc6c4cSSara Sharon 		INIT_LIST_HEAD(&mvmtxq->list);
1659fba8248eSSara Sharon 		atomic_set(&mvmtxq->tx_request, 0);
1660cfbc6c4cSSara Sharon 	}
1661cfbc6c4cSSara Sharon 
1662e705c121SKalle Valo 	mvm_sta->agg_tids = 0;
1663e705c121SKalle Valo 
1664a571f5f6SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) &&
1665a571f5f6SSara Sharon 	    !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
166692c4dca6SJohannes Berg 		int q;
166792c4dca6SJohannes Berg 
1668a571f5f6SSara Sharon 		dup_data = kcalloc(mvm->trans->num_rx_queues,
166992c4dca6SJohannes Berg 				   sizeof(*dup_data), GFP_KERNEL);
1670a571f5f6SSara Sharon 		if (!dup_data)
1671a571f5f6SSara Sharon 			return -ENOMEM;
167292c4dca6SJohannes Berg 		/*
167392c4dca6SJohannes Berg 		 * Initialize all the last_seq values to 0xffff which can never
167492c4dca6SJohannes Berg 		 * compare equal to the frame's seq_ctrl in the check in
167592c4dca6SJohannes Berg 		 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
167692c4dca6SJohannes Berg 		 * number and fragmented packets don't reach that function.
167792c4dca6SJohannes Berg 		 *
167892c4dca6SJohannes Berg 		 * This thus allows receiving a packet with seqno 0 and the
167992c4dca6SJohannes Berg 		 * retry bit set as the very first packet on a new TID.
168092c4dca6SJohannes Berg 		 */
168192c4dca6SJohannes Berg 		for (q = 0; q < mvm->trans->num_rx_queues; q++)
168292c4dca6SJohannes Berg 			memset(dup_data[q].last_seq, 0xff,
168392c4dca6SJohannes Berg 			       sizeof(dup_data[q].last_seq));
1684a571f5f6SSara Sharon 		mvm_sta->dup_data = dup_data;
1685a571f5f6SSara Sharon 	}
1686a571f5f6SSara Sharon 
1687c8f54701SJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm)) {
1688d5216a28SLiad Kaufman 		ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1689d5216a28SLiad Kaufman 						 ieee80211_vif_type_p2p(vif));
169024afba76SLiad Kaufman 		if (ret)
169124afba76SLiad Kaufman 			goto err;
169224afba76SLiad Kaufman 	}
169324afba76SLiad Kaufman 
16949f66a397SGregory Greenman 	/*
16959f66a397SGregory Greenman 	 * if rs is registered with mac80211, then "add station" will be handled
16969f66a397SGregory Greenman 	 * via the corresponding ops, otherwise need to notify rate scaling here
16979f66a397SGregory Greenman 	 */
16984243edb4SEmmanuel Grumbach 	if (iwl_mvm_has_tlc_offload(mvm))
16999f66a397SGregory Greenman 		iwl_mvm_rs_add_sta(mvm, mvm_sta);
17000f8084cdSMordechay Goodstein 	else
1701f5d88fa3SGregory Greenman 		spin_lock_init(&mvm_sta->lq_sta.rs_drv.pers.lock);
17029f66a397SGregory Greenman 
17030dde2440SAvraham Stern 	iwl_mvm_toggle_tx_ant(mvm, &mvm_sta->tx_ant);
17040dde2440SAvraham Stern 
17058d98ae6eSLiad Kaufman update_fw:
1706732d06e9SShaul Triebitz 	ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
1707e705c121SKalle Valo 	if (ret)
1708e705c121SKalle Valo 		goto err;
1709e705c121SKalle Valo 
1710e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION) {
1711e705c121SKalle Valo 		if (!sta->tdls) {
17120ae98812SSara Sharon 			WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
1713e705c121SKalle Valo 			mvmvif->ap_sta_id = sta_id;
1714e705c121SKalle Valo 		} else {
17150ae98812SSara Sharon 			WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
1716e705c121SKalle Valo 		}
1717e705c121SKalle Valo 	}
1718e705c121SKalle Valo 
1719e705c121SKalle Valo 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1720e705c121SKalle Valo 
1721e705c121SKalle Valo 	return 0;
1722e705c121SKalle Valo 
1723e705c121SKalle Valo err:
1724e705c121SKalle Valo 	return ret;
1725e705c121SKalle Valo }
1726e705c121SKalle Valo 
1727e705c121SKalle Valo int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1728e705c121SKalle Valo 		      bool drain)
1729e705c121SKalle Valo {
1730e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
1731e705c121SKalle Valo 	int ret;
1732e705c121SKalle Valo 	u32 status;
1733e705c121SKalle Valo 
1734e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1735e705c121SKalle Valo 
1736e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1737e705c121SKalle Valo 	cmd.sta_id = mvmsta->sta_id;
1738e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
1739e705c121SKalle Valo 	cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1740e705c121SKalle Valo 	cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1741e705c121SKalle Valo 
1742e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
1743854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1744854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
1745e705c121SKalle Valo 					  &cmd, &status);
1746e705c121SKalle Valo 	if (ret)
1747e705c121SKalle Valo 		return ret;
1748e705c121SKalle Valo 
1749837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1750e705c121SKalle Valo 	case ADD_STA_SUCCESS:
1751e705c121SKalle Valo 		IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1752e705c121SKalle Valo 			       mvmsta->sta_id);
1753e705c121SKalle Valo 		break;
1754e705c121SKalle Valo 	default:
1755e705c121SKalle Valo 		ret = -EIO;
1756e705c121SKalle Valo 		IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1757e705c121SKalle Valo 			mvmsta->sta_id);
1758e705c121SKalle Valo 		break;
1759e705c121SKalle Valo 	}
1760e705c121SKalle Valo 
1761e705c121SKalle Valo 	return ret;
1762e705c121SKalle Valo }
1763e705c121SKalle Valo 
1764e705c121SKalle Valo /*
1765e705c121SKalle Valo  * Remove a station from the FW table. Before sending the command to remove
1766e705c121SKalle Valo  * the station validate that the station is indeed known to the driver (sanity
1767e705c121SKalle Valo  * only).
1768e705c121SKalle Valo  */
1769e705c121SKalle Valo static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1770e705c121SKalle Valo {
1771e705c121SKalle Valo 	struct ieee80211_sta *sta;
1772e705c121SKalle Valo 	struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1773e705c121SKalle Valo 		.sta_id = sta_id,
1774e705c121SKalle Valo 	};
1775e705c121SKalle Valo 	int ret;
1776e705c121SKalle Valo 
1777e705c121SKalle Valo 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1778e705c121SKalle Valo 					lockdep_is_held(&mvm->mutex));
1779e705c121SKalle Valo 
1780e705c121SKalle Valo 	/* Note: internal stations are marked as error values */
1781e705c121SKalle Valo 	if (!sta) {
1782e705c121SKalle Valo 		IWL_ERR(mvm, "Invalid station id\n");
1783e705c121SKalle Valo 		return -EINVAL;
1784e705c121SKalle Valo 	}
1785e705c121SKalle Valo 
1786e705c121SKalle Valo 	ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1787e705c121SKalle Valo 				   sizeof(rm_sta_cmd), &rm_sta_cmd);
1788e705c121SKalle Valo 	if (ret) {
1789e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1790e705c121SKalle Valo 		return ret;
1791e705c121SKalle Valo 	}
1792e705c121SKalle Valo 
1793e705c121SKalle Valo 	return 0;
1794e705c121SKalle Valo }
1795e705c121SKalle Valo 
179624afba76SLiad Kaufman static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
179724afba76SLiad Kaufman 				       struct ieee80211_vif *vif,
1798cfbc6c4cSSara Sharon 				       struct ieee80211_sta *sta)
179924afba76SLiad Kaufman {
1800cfbc6c4cSSara Sharon 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
180124afba76SLiad Kaufman 	int i;
180224afba76SLiad Kaufman 
180324afba76SLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
180424afba76SLiad Kaufman 
180524afba76SLiad Kaufman 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
18066862fceeSSara Sharon 		if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
180724afba76SLiad Kaufman 			continue;
180824afba76SLiad Kaufman 
1809c6ce1c74SJohannes Berg 		iwl_mvm_disable_txq(mvm, sta, &mvm_sta->tid_data[i].txq_id, i,
1810cfbc6c4cSSara Sharon 				    0);
18116862fceeSSara Sharon 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
181224afba76SLiad Kaufman 	}
1813cfbc6c4cSSara Sharon 
1814cfbc6c4cSSara Sharon 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1815cfbc6c4cSSara Sharon 		struct iwl_mvm_txq *mvmtxq =
1816cfbc6c4cSSara Sharon 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1817cfbc6c4cSSara Sharon 
1818cfbc6c4cSSara Sharon 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1819cfbc6c4cSSara Sharon 	}
182024afba76SLiad Kaufman }
182124afba76SLiad Kaufman 
1822d6d517b7SSara Sharon int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1823d6d517b7SSara Sharon 				  struct iwl_mvm_sta *mvm_sta)
1824d6d517b7SSara Sharon {
1825bec9522aSSharon Dvir 	int i;
1826d6d517b7SSara Sharon 
1827d6d517b7SSara Sharon 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1828d6d517b7SSara Sharon 		u16 txq_id;
1829bec9522aSSharon Dvir 		int ret;
1830d6d517b7SSara Sharon 
1831d6d517b7SSara Sharon 		spin_lock_bh(&mvm_sta->lock);
1832d6d517b7SSara Sharon 		txq_id = mvm_sta->tid_data[i].txq_id;
1833d6d517b7SSara Sharon 		spin_unlock_bh(&mvm_sta->lock);
1834d6d517b7SSara Sharon 
1835d6d517b7SSara Sharon 		if (txq_id == IWL_MVM_INVALID_QUEUE)
1836d6d517b7SSara Sharon 			continue;
1837d6d517b7SSara Sharon 
1838d6d517b7SSara Sharon 		ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1839d6d517b7SSara Sharon 		if (ret)
1840bec9522aSSharon Dvir 			return ret;
1841d6d517b7SSara Sharon 	}
1842d6d517b7SSara Sharon 
1843bec9522aSSharon Dvir 	return 0;
1844d6d517b7SSara Sharon }
1845d6d517b7SSara Sharon 
1846e705c121SKalle Valo int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1847e705c121SKalle Valo 		   struct ieee80211_vif *vif,
1848e705c121SKalle Valo 		   struct ieee80211_sta *sta)
1849e705c121SKalle Valo {
1850e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1851e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
185294c3e614SSara Sharon 	u8 sta_id = mvm_sta->sta_id;
1853e705c121SKalle Valo 	int ret;
1854e705c121SKalle Valo 
1855e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1856e705c121SKalle Valo 
1857a571f5f6SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm))
1858a571f5f6SSara Sharon 		kfree(mvm_sta->dup_data);
1859a571f5f6SSara Sharon 
1860e705c121SKalle Valo 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1861e705c121SKalle Valo 	if (ret)
1862e705c121SKalle Valo 		return ret;
1863c8f54701SJohannes Berg 
1864e705c121SKalle Valo 	/* flush its queues here since we are freeing mvm_sta */
1865f9084775SNathan Errera 	ret = iwl_mvm_flush_sta(mvm, mvm_sta, false);
1866e705c121SKalle Valo 	if (ret)
1867e705c121SKalle Valo 		return ret;
1868d6d517b7SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
1869d6d517b7SSara Sharon 		ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1870d6d517b7SSara Sharon 	} else {
1871d6d517b7SSara Sharon 		u32 q_mask = mvm_sta->tfd_queue_msk;
1872d6d517b7SSara Sharon 
1873a1a57877SSara Sharon 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1874d6d517b7SSara Sharon 						     q_mask);
1875d6d517b7SSara Sharon 	}
1876e705c121SKalle Valo 	if (ret)
1877e705c121SKalle Valo 		return ret;
1878c8f54701SJohannes Berg 
1879e705c121SKalle Valo 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1880e705c121SKalle Valo 
1881cfbc6c4cSSara Sharon 	iwl_mvm_disable_sta_queues(mvm, vif, sta);
188256214749SLiad Kaufman 
188356214749SLiad Kaufman 	/* If there is a TXQ still marked as reserved - free it */
1884c8f54701SJohannes Berg 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1885a0315deaSLiad Kaufman 		u8 reserved_txq = mvm_sta->reserved_queue;
1886a0315deaSLiad Kaufman 		enum iwl_mvm_queue_status *status;
1887a0315deaSLiad Kaufman 
1888a0315deaSLiad Kaufman 		/*
1889a0315deaSLiad Kaufman 		 * If no traffic has gone through the reserved TXQ - it
1890a0315deaSLiad Kaufman 		 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1891a0315deaSLiad Kaufman 		 * should be manually marked as free again
1892a0315deaSLiad Kaufman 		 */
1893a0315deaSLiad Kaufman 		status = &mvm->queue_info[reserved_txq].status;
1894a0315deaSLiad Kaufman 		if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1895a0315deaSLiad Kaufman 			 (*status != IWL_MVM_QUEUE_FREE),
1896a0315deaSLiad Kaufman 			 "sta_id %d reserved txq %d status %d",
1897f3f240f9SJohannes Berg 			 sta_id, reserved_txq, *status))
1898a0315deaSLiad Kaufman 			return -EINVAL;
1899a0315deaSLiad Kaufman 
1900a0315deaSLiad Kaufman 		*status = IWL_MVM_QUEUE_FREE;
1901a0315deaSLiad Kaufman 	}
1902a0315deaSLiad Kaufman 
1903e3118ad7SLiad Kaufman 	if (vif->type == NL80211_IFTYPE_STATION &&
190494c3e614SSara Sharon 	    mvmvif->ap_sta_id == sta_id) {
1905e3118ad7SLiad Kaufman 		/* if associated - we can't remove the AP STA now */
1906e705c121SKalle Valo 		if (vif->bss_conf.assoc)
1907e705c121SKalle Valo 			return ret;
1908e705c121SKalle Valo 
1909e705c121SKalle Valo 		/* unassoc - go ahead - remove the AP STA now */
19100ae98812SSara Sharon 		mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1911e705c121SKalle Valo 	}
1912e705c121SKalle Valo 
1913e705c121SKalle Valo 	/*
1914e705c121SKalle Valo 	 * This shouldn't happen - the TDLS channel switch should be canceled
1915e705c121SKalle Valo 	 * before the STA is removed.
1916e705c121SKalle Valo 	 */
191794c3e614SSara Sharon 	if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
19180ae98812SSara Sharon 		mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1919e705c121SKalle Valo 		cancel_delayed_work(&mvm->tdls_cs.dwork);
1920e705c121SKalle Valo 	}
1921e705c121SKalle Valo 
1922e705c121SKalle Valo 	/*
1923e705c121SKalle Valo 	 * Make sure that the tx response code sees the station as -EBUSY and
1924e705c121SKalle Valo 	 * calls the drain worker.
1925e705c121SKalle Valo 	 */
1926e705c121SKalle Valo 	spin_lock_bh(&mvm_sta->lock);
1927e705c121SKalle Valo 	spin_unlock_bh(&mvm_sta->lock);
1928e705c121SKalle Valo 
1929e705c121SKalle Valo 	ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1930e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1931e705c121SKalle Valo 
1932e705c121SKalle Valo 	return ret;
1933e705c121SKalle Valo }
1934e705c121SKalle Valo 
1935e705c121SKalle Valo int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1936e705c121SKalle Valo 		      struct ieee80211_vif *vif,
1937e705c121SKalle Valo 		      u8 sta_id)
1938e705c121SKalle Valo {
1939e705c121SKalle Valo 	int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1940e705c121SKalle Valo 
1941e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
1942e705c121SKalle Valo 
1943e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1944e705c121SKalle Valo 	return ret;
1945e705c121SKalle Valo }
1946e705c121SKalle Valo 
19470e39eb03SChaya Rachel Ivgi int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1948e705c121SKalle Valo 			     struct iwl_mvm_int_sta *sta,
1949ced19f26SSara Sharon 			     u32 qmask, enum nl80211_iftype iftype,
1950ced19f26SSara Sharon 			     enum iwl_sta_type type)
1951e705c121SKalle Valo {
1952df65c8d1SAvraham Stern 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1953df65c8d1SAvraham Stern 	    sta->sta_id == IWL_MVM_INVALID_STA) {
1954e705c121SKalle Valo 		sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
19550ae98812SSara Sharon 		if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
1956e705c121SKalle Valo 			return -ENOSPC;
1957e705c121SKalle Valo 	}
1958e705c121SKalle Valo 
1959e705c121SKalle Valo 	sta->tfd_queue_msk = qmask;
1960ced19f26SSara Sharon 	sta->type = type;
1961e705c121SKalle Valo 
1962e705c121SKalle Valo 	/* put a non-NULL value so iterating over the stations won't stop */
1963e705c121SKalle Valo 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1964e705c121SKalle Valo 	return 0;
1965e705c121SKalle Valo }
1966e705c121SKalle Valo 
196726d6c16bSSara Sharon void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
1968e705c121SKalle Valo {
1969e705c121SKalle Valo 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
1970e705c121SKalle Valo 	memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
19710ae98812SSara Sharon 	sta->sta_id = IWL_MVM_INVALID_STA;
1972e705c121SKalle Valo }
1973e705c121SKalle Valo 
197491cf5dedSJohannes Berg static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 queue,
1975b13f43a4SEmmanuel Grumbach 					  u8 sta_id, u8 fifo)
1976e705c121SKalle Valo {
19779617040eSEmmanuel Grumbach 	unsigned int wdg_timeout =
19789617040eSEmmanuel Grumbach 		mvm->trans->trans_cfg->base_params->wd_timeout;
197928d0793eSLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
1980b13f43a4SEmmanuel Grumbach 		.fifo = fifo,
1981b13f43a4SEmmanuel Grumbach 		.sta_id = sta_id,
198228d0793eSLiad Kaufman 		.tid = IWL_MAX_TID_COUNT,
198328d0793eSLiad Kaufman 		.aggregate = false,
198428d0793eSLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
198528d0793eSLiad Kaufman 	};
198628d0793eSLiad Kaufman 
198791cf5dedSJohannes Berg 	WARN_ON(iwl_mvm_has_new_tx_api(mvm));
198891cf5dedSJohannes Berg 
198991cf5dedSJohannes Berg 	iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
199028d0793eSLiad Kaufman }
199191cf5dedSJohannes Berg 
199291cf5dedSJohannes Berg static int iwl_mvm_enable_aux_snif_queue_tvqm(struct iwl_mvm *mvm, u8 sta_id)
199391cf5dedSJohannes Berg {
19949617040eSEmmanuel Grumbach 	unsigned int wdg_timeout =
19959617040eSEmmanuel Grumbach 		mvm->trans->trans_cfg->base_params->wd_timeout;
199691cf5dedSJohannes Berg 
199791cf5dedSJohannes Berg 	WARN_ON(!iwl_mvm_has_new_tx_api(mvm));
199891cf5dedSJohannes Berg 
199991cf5dedSJohannes Berg 	return iwl_mvm_tvqm_enable_txq(mvm, sta_id, IWL_MAX_TID_COUNT,
200091cf5dedSJohannes Berg 				       wdg_timeout);
200191cf5dedSJohannes Berg }
200291cf5dedSJohannes Berg 
200391cf5dedSJohannes Berg static int iwl_mvm_add_int_sta_with_queue(struct iwl_mvm *mvm, int macidx,
2004be82ecd3SAvraham Stern 					  int maccolor, u8 *addr,
200591cf5dedSJohannes Berg 					  struct iwl_mvm_int_sta *sta,
200691cf5dedSJohannes Berg 					  u16 *queue, int fifo)
200791cf5dedSJohannes Berg {
200891cf5dedSJohannes Berg 	int ret;
200991cf5dedSJohannes Berg 
201091cf5dedSJohannes Berg 	/* Map queue to fifo - needs to happen before adding station */
201191cf5dedSJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm))
201291cf5dedSJohannes Berg 		iwl_mvm_enable_aux_snif_queue(mvm, *queue, sta->sta_id, fifo);
201391cf5dedSJohannes Berg 
2014be82ecd3SAvraham Stern 	ret = iwl_mvm_add_int_sta_common(mvm, sta, addr, macidx, maccolor);
201591cf5dedSJohannes Berg 	if (ret) {
201691cf5dedSJohannes Berg 		if (!iwl_mvm_has_new_tx_api(mvm))
2017c6ce1c74SJohannes Berg 			iwl_mvm_disable_txq(mvm, NULL, queue,
201891cf5dedSJohannes Berg 					    IWL_MAX_TID_COUNT, 0);
201991cf5dedSJohannes Berg 		return ret;
202091cf5dedSJohannes Berg 	}
202191cf5dedSJohannes Berg 
202291cf5dedSJohannes Berg 	/*
202391cf5dedSJohannes Berg 	 * For 22000 firmware and on we cannot add queue to a station unknown
202491cf5dedSJohannes Berg 	 * to firmware so enable queue here - after the station was added
202591cf5dedSJohannes Berg 	 */
202691cf5dedSJohannes Berg 	if (iwl_mvm_has_new_tx_api(mvm)) {
202791cf5dedSJohannes Berg 		int txq;
202891cf5dedSJohannes Berg 
202991cf5dedSJohannes Berg 		txq = iwl_mvm_enable_aux_snif_queue_tvqm(mvm, sta->sta_id);
203091cf5dedSJohannes Berg 		if (txq < 0) {
203191cf5dedSJohannes Berg 			iwl_mvm_rm_sta_common(mvm, sta->sta_id);
203291cf5dedSJohannes Berg 			return txq;
203391cf5dedSJohannes Berg 		}
203491cf5dedSJohannes Berg 
203591cf5dedSJohannes Berg 		*queue = txq;
203691cf5dedSJohannes Berg 	}
203791cf5dedSJohannes Berg 
203891cf5dedSJohannes Berg 	return 0;
2039c5a719eeSSara Sharon }
2040c5a719eeSSara Sharon 
20412c2c3647SNathan Errera int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm, u32 lmac_id)
2042c5a719eeSSara Sharon {
2043c5a719eeSSara Sharon 	int ret;
2044c5a719eeSSara Sharon 
2045c5a719eeSSara Sharon 	lockdep_assert_held(&mvm->mutex);
2046c5a719eeSSara Sharon 
2047c5a719eeSSara Sharon 	/* Allocate aux station and assign to it the aux queue */
2048c5a719eeSSara Sharon 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
2049ced19f26SSara Sharon 				       NL80211_IFTYPE_UNSPECIFIED,
2050ced19f26SSara Sharon 				       IWL_STA_AUX_ACTIVITY);
2051c5a719eeSSara Sharon 	if (ret)
2052c5a719eeSSara Sharon 		return ret;
2053c5a719eeSSara Sharon 
20542c2c3647SNathan Errera 	/*
20552c2c3647SNathan Errera 	 * In CDB NICs we need to specify which lmac to use for aux activity
20562c2c3647SNathan Errera 	 * using the mac_id argument place to send lmac_id to the function
20572c2c3647SNathan Errera 	 */
20582c2c3647SNathan Errera 	ret = iwl_mvm_add_int_sta_with_queue(mvm, lmac_id, 0, NULL,
205991cf5dedSJohannes Berg 					     &mvm->aux_sta, &mvm->aux_queue,
2060b13f43a4SEmmanuel Grumbach 					     IWL_MVM_TX_FIFO_MCAST);
2061c5a719eeSSara Sharon 	if (ret) {
2062e705c121SKalle Valo 		iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2063e705c121SKalle Valo 		return ret;
2064e705c121SKalle Valo 	}
2065e705c121SKalle Valo 
2066c5a719eeSSara Sharon 	return 0;
2067c5a719eeSSara Sharon }
2068c5a719eeSSara Sharon 
20690e39eb03SChaya Rachel Ivgi int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
20700e39eb03SChaya Rachel Ivgi {
20710e39eb03SChaya Rachel Ivgi 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
20720e39eb03SChaya Rachel Ivgi 
20730e39eb03SChaya Rachel Ivgi 	lockdep_assert_held(&mvm->mutex);
2074b13f43a4SEmmanuel Grumbach 
207591cf5dedSJohannes Berg 	return iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
2076be82ecd3SAvraham Stern 					      NULL, &mvm->snif_sta,
2077be82ecd3SAvraham Stern 					      &mvm->snif_queue,
2078b13f43a4SEmmanuel Grumbach 					      IWL_MVM_TX_FIFO_BE);
20790e39eb03SChaya Rachel Ivgi }
20800e39eb03SChaya Rachel Ivgi 
20810e39eb03SChaya Rachel Ivgi int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
20820e39eb03SChaya Rachel Ivgi {
20830e39eb03SChaya Rachel Ivgi 	int ret;
20840e39eb03SChaya Rachel Ivgi 
20850e39eb03SChaya Rachel Ivgi 	lockdep_assert_held(&mvm->mutex);
20860e39eb03SChaya Rachel Ivgi 
2087e223e42aSGregory Greenman 	if (WARN_ON_ONCE(mvm->snif_sta.sta_id == IWL_MVM_INVALID_STA))
2088e223e42aSGregory Greenman 		return -EINVAL;
2089e223e42aSGregory Greenman 
2090c6ce1c74SJohannes Berg 	iwl_mvm_disable_txq(mvm, NULL, &mvm->snif_queue, IWL_MAX_TID_COUNT, 0);
20910e39eb03SChaya Rachel Ivgi 	ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
20920e39eb03SChaya Rachel Ivgi 	if (ret)
20930e39eb03SChaya Rachel Ivgi 		IWL_WARN(mvm, "Failed sending remove station\n");
20940e39eb03SChaya Rachel Ivgi 
20950e39eb03SChaya Rachel Ivgi 	return ret;
20960e39eb03SChaya Rachel Ivgi }
20970e39eb03SChaya Rachel Ivgi 
2098f327236dSSharon int iwl_mvm_rm_aux_sta(struct iwl_mvm *mvm)
2099f327236dSSharon {
2100f327236dSSharon 	int ret;
2101f327236dSSharon 
2102f327236dSSharon 	lockdep_assert_held(&mvm->mutex);
2103f327236dSSharon 
2104e223e42aSGregory Greenman 	if (WARN_ON_ONCE(mvm->aux_sta.sta_id == IWL_MVM_INVALID_STA))
2105e223e42aSGregory Greenman 		return -EINVAL;
2106e223e42aSGregory Greenman 
2107c6ce1c74SJohannes Berg 	iwl_mvm_disable_txq(mvm, NULL, &mvm->aux_queue, IWL_MAX_TID_COUNT, 0);
2108f327236dSSharon 	ret = iwl_mvm_rm_sta_common(mvm, mvm->aux_sta.sta_id);
2109f327236dSSharon 	if (ret)
2110f327236dSSharon 		IWL_WARN(mvm, "Failed sending remove station\n");
2111f327236dSSharon 	iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2112f327236dSSharon 
2113f327236dSSharon 	return ret;
2114f327236dSSharon }
2115f327236dSSharon 
21160e39eb03SChaya Rachel Ivgi void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
21170e39eb03SChaya Rachel Ivgi {
21180e39eb03SChaya Rachel Ivgi 	iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
21190e39eb03SChaya Rachel Ivgi }
21200e39eb03SChaya Rachel Ivgi 
2121e705c121SKalle Valo /*
2122e705c121SKalle Valo  * Send the add station command for the vif's broadcast station.
2123e705c121SKalle Valo  * Assumes that the station was already allocated.
2124e705c121SKalle Valo  *
2125e705c121SKalle Valo  * @mvm: the mvm component
2126e705c121SKalle Valo  * @vif: the interface to which the broadcast station is added
2127e705c121SKalle Valo  * @bsta: the broadcast station to add.
2128e705c121SKalle Valo  */
2129e705c121SKalle Valo int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2130e705c121SKalle Valo {
2131e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2132e705c121SKalle Valo 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2133e705c121SKalle Valo 	static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2134e705c121SKalle Valo 	const u8 *baddr = _baddr;
21357daa7624SJohannes Berg 	int queue;
2136df88c08dSLiad Kaufman 	int ret;
2137c5a719eeSSara Sharon 	unsigned int wdg_timeout =
2138c5a719eeSSara Sharon 		iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2139de24f638SLiad Kaufman 	struct iwl_trans_txq_scd_cfg cfg = {
2140de24f638SLiad Kaufman 		.fifo = IWL_MVM_TX_FIFO_VO,
2141de24f638SLiad Kaufman 		.sta_id = mvmvif->bcast_sta.sta_id,
2142de24f638SLiad Kaufman 		.tid = IWL_MAX_TID_COUNT,
2143de24f638SLiad Kaufman 		.aggregate = false,
2144de24f638SLiad Kaufman 		.frame_limit = IWL_FRAME_LIMIT,
2145de24f638SLiad Kaufman 	};
2146de24f638SLiad Kaufman 
2147c5a719eeSSara Sharon 	lockdep_assert_held(&mvm->mutex);
2148c5a719eeSSara Sharon 
2149c8f54701SJohannes Berg 	if (!iwl_mvm_has_new_tx_api(mvm)) {
21504d339989SLiad Kaufman 		if (vif->type == NL80211_IFTYPE_AP ||
2151f8510d67SNathan Chancellor 		    vif->type == NL80211_IFTYPE_ADHOC) {
215249f71713SSara Sharon 			queue = mvm->probe_queue;
2153f8510d67SNathan Chancellor 		} else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
215449f71713SSara Sharon 			queue = mvm->p2p_dev_queue;
2155f8510d67SNathan Chancellor 		} else {
2156f8510d67SNathan Chancellor 			WARN(1, "Missing required TXQ for adding bcast STA\n");
2157de24f638SLiad Kaufman 			return -EINVAL;
2158f8510d67SNathan Chancellor 		}
2159de24f638SLiad Kaufman 
2160df88c08dSLiad Kaufman 		bsta->tfd_queue_msk |= BIT(queue);
2161c5a719eeSSara Sharon 
2162cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2163de24f638SLiad Kaufman 	}
2164de24f638SLiad Kaufman 
2165e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_ADHOC)
2166e705c121SKalle Valo 		baddr = vif->bss_conf.bssid;
2167e705c121SKalle Valo 
21680ae98812SSara Sharon 	if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
2169e705c121SKalle Valo 		return -ENOSPC;
2170e705c121SKalle Valo 
2171df88c08dSLiad Kaufman 	ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2172e705c121SKalle Valo 					 mvmvif->id, mvmvif->color);
2173df88c08dSLiad Kaufman 	if (ret)
2174df88c08dSLiad Kaufman 		return ret;
2175df88c08dSLiad Kaufman 
2176df88c08dSLiad Kaufman 	/*
21772f7a3863SLuca Coelho 	 * For 22000 firmware and on we cannot add queue to a station unknown
2178c5a719eeSSara Sharon 	 * to firmware so enable queue here - after the station was added
2179df88c08dSLiad Kaufman 	 */
2180310181ecSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
2181cfbc6c4cSSara Sharon 		queue = iwl_mvm_tvqm_enable_txq(mvm, bsta->sta_id,
2182310181ecSSara Sharon 						IWL_MAX_TID_COUNT,
2183c5a719eeSSara Sharon 						wdg_timeout);
218491cf5dedSJohannes Berg 		if (queue < 0) {
218591cf5dedSJohannes Berg 			iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
218691cf5dedSJohannes Berg 			return queue;
218791cf5dedSJohannes Berg 		}
21887daa7624SJohannes Berg 
21897b758a11SLuca Coelho 		if (vif->type == NL80211_IFTYPE_AP ||
21907b758a11SLuca Coelho 		    vif->type == NL80211_IFTYPE_ADHOC)
2191310181ecSSara Sharon 			mvm->probe_queue = queue;
2192310181ecSSara Sharon 		else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
2193310181ecSSara Sharon 			mvm->p2p_dev_queue = queue;
2194310181ecSSara Sharon 	}
2195df88c08dSLiad Kaufman 
2196df88c08dSLiad Kaufman 	return 0;
2197df88c08dSLiad Kaufman }
2198df88c08dSLiad Kaufman 
2199df88c08dSLiad Kaufman static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2200df88c08dSLiad Kaufman 					  struct ieee80211_vif *vif)
2201df88c08dSLiad Kaufman {
2202df88c08dSLiad Kaufman 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2203c6ce1c74SJohannes Berg 	u16 *queueptr, queue;
2204df88c08dSLiad Kaufman 
2205df88c08dSLiad Kaufman 	lockdep_assert_held(&mvm->mutex);
2206df88c08dSLiad Kaufman 
2207f9084775SNathan Errera 	iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true);
2208d49394a1SSara Sharon 
2209d167e81aSMordechai Goodstein 	switch (vif->type) {
2210d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_AP:
2211d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_ADHOC:
2212c6ce1c74SJohannes Berg 		queueptr = &mvm->probe_queue;
2213d167e81aSMordechai Goodstein 		break;
2214d167e81aSMordechai Goodstein 	case NL80211_IFTYPE_P2P_DEVICE:
2215c6ce1c74SJohannes Berg 		queueptr = &mvm->p2p_dev_queue;
2216d167e81aSMordechai Goodstein 		break;
2217d167e81aSMordechai Goodstein 	default:
2218d167e81aSMordechai Goodstein 		WARN(1, "Can't free bcast queue on vif type %d\n",
2219d167e81aSMordechai Goodstein 		     vif->type);
2220d167e81aSMordechai Goodstein 		return;
2221df88c08dSLiad Kaufman 	}
2222df88c08dSLiad Kaufman 
2223c6ce1c74SJohannes Berg 	queue = *queueptr;
2224c6ce1c74SJohannes Berg 	iwl_mvm_disable_txq(mvm, NULL, queueptr, IWL_MAX_TID_COUNT, 0);
2225d167e81aSMordechai Goodstein 	if (iwl_mvm_has_new_tx_api(mvm))
2226d167e81aSMordechai Goodstein 		return;
2227d167e81aSMordechai Goodstein 
2228d167e81aSMordechai Goodstein 	WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
2229d167e81aSMordechai Goodstein 	mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
2230e705c121SKalle Valo }
2231e705c121SKalle Valo 
2232e705c121SKalle Valo /* Send the FW a request to remove the station from it's internal data
2233e705c121SKalle Valo  * structures, but DO NOT remove the entry from the local data structures. */
2234e705c121SKalle Valo int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2235e705c121SKalle Valo {
2236e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2237e705c121SKalle Valo 	int ret;
2238e705c121SKalle Valo 
2239e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2240e705c121SKalle Valo 
2241df88c08dSLiad Kaufman 	iwl_mvm_free_bcast_sta_queues(mvm, vif);
2242df88c08dSLiad Kaufman 
2243e705c121SKalle Valo 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
2244e705c121SKalle Valo 	if (ret)
2245e705c121SKalle Valo 		IWL_WARN(mvm, "Failed sending remove station\n");
2246e705c121SKalle Valo 	return ret;
2247e705c121SKalle Valo }
2248e705c121SKalle Valo 
2249e705c121SKalle Valo int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2250e705c121SKalle Valo {
2251e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2252e705c121SKalle Valo 
2253e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2254e705c121SKalle Valo 
2255c8f54701SJohannes Berg 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, 0,
2256ced19f26SSara Sharon 					ieee80211_vif_type_p2p(vif),
2257ced19f26SSara Sharon 					IWL_STA_GENERAL_PURPOSE);
2258e705c121SKalle Valo }
2259e705c121SKalle Valo 
2260e705c121SKalle Valo /* Allocate a new station entry for the broadcast station to the given vif,
2261e705c121SKalle Valo  * and send it to the FW.
2262e705c121SKalle Valo  * Note that each P2P mac should have its own broadcast station.
2263e705c121SKalle Valo  *
2264e705c121SKalle Valo  * @mvm: the mvm component
2265e705c121SKalle Valo  * @vif: the interface to which the broadcast station is added
2266e705c121SKalle Valo  * @bsta: the broadcast station to add. */
2267d197358bSLuca Coelho int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2268e705c121SKalle Valo {
2269e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2270e705c121SKalle Valo 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2271e705c121SKalle Valo 	int ret;
2272e705c121SKalle Valo 
2273e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2274e705c121SKalle Valo 
2275e705c121SKalle Valo 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2276e705c121SKalle Valo 	if (ret)
2277e705c121SKalle Valo 		return ret;
2278e705c121SKalle Valo 
2279e705c121SKalle Valo 	ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2280e705c121SKalle Valo 
2281e705c121SKalle Valo 	if (ret)
2282e705c121SKalle Valo 		iwl_mvm_dealloc_int_sta(mvm, bsta);
2283e705c121SKalle Valo 
2284e705c121SKalle Valo 	return ret;
2285e705c121SKalle Valo }
2286e705c121SKalle Valo 
2287e705c121SKalle Valo void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2288e705c121SKalle Valo {
2289e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2290e705c121SKalle Valo 
2291e705c121SKalle Valo 	iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2292e705c121SKalle Valo }
2293e705c121SKalle Valo 
2294e705c121SKalle Valo /*
2295e705c121SKalle Valo  * Send the FW a request to remove the station from it's internal data
2296e705c121SKalle Valo  * structures, and in addition remove it from the local data structure.
2297e705c121SKalle Valo  */
2298d197358bSLuca Coelho int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2299e705c121SKalle Valo {
2300e705c121SKalle Valo 	int ret;
2301e705c121SKalle Valo 
2302e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2303e705c121SKalle Valo 
2304e705c121SKalle Valo 	ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2305e705c121SKalle Valo 
2306e705c121SKalle Valo 	iwl_mvm_dealloc_bcast_sta(mvm, vif);
2307e705c121SKalle Valo 
2308e705c121SKalle Valo 	return ret;
2309e705c121SKalle Valo }
2310e705c121SKalle Valo 
231126d6c16bSSara Sharon /*
231226d6c16bSSara Sharon  * Allocate a new station entry for the multicast station to the given vif,
231326d6c16bSSara Sharon  * and send it to the FW.
231426d6c16bSSara Sharon  * Note that each AP/GO mac should have its own multicast station.
231526d6c16bSSara Sharon  *
231626d6c16bSSara Sharon  * @mvm: the mvm component
231726d6c16bSSara Sharon  * @vif: the interface to which the multicast station is added
231826d6c16bSSara Sharon  */
231926d6c16bSSara Sharon int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
232026d6c16bSSara Sharon {
232126d6c16bSSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
232226d6c16bSSara Sharon 	struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
232326d6c16bSSara Sharon 	static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
232426d6c16bSSara Sharon 	const u8 *maddr = _maddr;
232526d6c16bSSara Sharon 	struct iwl_trans_txq_scd_cfg cfg = {
2326192a7e1fSJohannes Berg 		.fifo = vif->type == NL80211_IFTYPE_AP ?
2327192a7e1fSJohannes Berg 			IWL_MVM_TX_FIFO_MCAST : IWL_MVM_TX_FIFO_BE,
232826d6c16bSSara Sharon 		.sta_id = msta->sta_id,
23296508de03SIlan Peer 		.tid = 0,
233026d6c16bSSara Sharon 		.aggregate = false,
233126d6c16bSSara Sharon 		.frame_limit = IWL_FRAME_LIMIT,
233226d6c16bSSara Sharon 	};
233326d6c16bSSara Sharon 	unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
233426d6c16bSSara Sharon 	int ret;
233526d6c16bSSara Sharon 
233626d6c16bSSara Sharon 	lockdep_assert_held(&mvm->mutex);
233726d6c16bSSara Sharon 
2338ee48b722SLiad Kaufman 	if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2339ee48b722SLiad Kaufman 		    vif->type != NL80211_IFTYPE_ADHOC))
234026d6c16bSSara Sharon 		return -ENOTSUPP;
234126d6c16bSSara Sharon 
2342ced19f26SSara Sharon 	/*
2343fc07bd8cSSara Sharon 	 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2344fc07bd8cSSara Sharon 	 * invalid, so make sure we use the queue we want.
2345fc07bd8cSSara Sharon 	 * Note that this is done here as we want to avoid making DQA
2346fc07bd8cSSara Sharon 	 * changes in mac80211 layer.
2347fc07bd8cSSara Sharon 	 */
2348cfbc6c4cSSara Sharon 	if (vif->type == NL80211_IFTYPE_ADHOC)
2349cfbc6c4cSSara Sharon 		mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2350fc07bd8cSSara Sharon 
2351fc07bd8cSSara Sharon 	/*
2352ced19f26SSara Sharon 	 * While in previous FWs we had to exclude cab queue from TFD queue
2353ced19f26SSara Sharon 	 * mask, now it is needed as any other queue.
2354ced19f26SSara Sharon 	 */
2355ced19f26SSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm) &&
2356ced19f26SSara Sharon 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2357cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2358cfbc6c4cSSara Sharon 				   timeout);
2359cfbc6c4cSSara Sharon 		msta->tfd_queue_msk |= BIT(mvmvif->cab_queue);
2360ced19f26SSara Sharon 	}
236126d6c16bSSara Sharon 	ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
236226d6c16bSSara Sharon 					 mvmvif->id, mvmvif->color);
236391cf5dedSJohannes Berg 	if (ret)
236491cf5dedSJohannes Berg 		goto err;
236526d6c16bSSara Sharon 
236626d6c16bSSara Sharon 	/*
236726d6c16bSSara Sharon 	 * Enable cab queue after the ADD_STA command is sent.
23682f7a3863SLuca Coelho 	 * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
2369ced19f26SSara Sharon 	 * command with unknown station id, and for FW that doesn't support
2370ced19f26SSara Sharon 	 * station API since the cab queue is not included in the
2371ced19f26SSara Sharon 	 * tfd_queue_mask.
237226d6c16bSSara Sharon 	 */
2373310181ecSSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
2374cfbc6c4cSSara Sharon 		int queue = iwl_mvm_tvqm_enable_txq(mvm, msta->sta_id,
23756508de03SIlan Peer 						    0,
237626d6c16bSSara Sharon 						    timeout);
237791cf5dedSJohannes Berg 		if (queue < 0) {
237891cf5dedSJohannes Berg 			ret = queue;
237991cf5dedSJohannes Berg 			goto err;
238091cf5dedSJohannes Berg 		}
2381e2af3fabSSara Sharon 		mvmvif->cab_queue = queue;
2382ced19f26SSara Sharon 	} else if (!fw_has_api(&mvm->fw->ucode_capa,
2383fc07bd8cSSara Sharon 			       IWL_UCODE_TLV_API_STA_TYPE))
2384cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2385cfbc6c4cSSara Sharon 				   timeout);
238626d6c16bSSara Sharon 
238726d6c16bSSara Sharon 	return 0;
238891cf5dedSJohannes Berg err:
238991cf5dedSJohannes Berg 	iwl_mvm_dealloc_int_sta(mvm, msta);
239091cf5dedSJohannes Berg 	return ret;
239126d6c16bSSara Sharon }
239226d6c16bSSara Sharon 
239328916a16SEmmanuel Grumbach static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
239428916a16SEmmanuel Grumbach 				    struct ieee80211_key_conf *keyconf,
239528916a16SEmmanuel Grumbach 				    bool mcast)
239628916a16SEmmanuel Grumbach {
239728916a16SEmmanuel Grumbach 	union {
239828916a16SEmmanuel Grumbach 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
239928916a16SEmmanuel Grumbach 		struct iwl_mvm_add_sta_key_cmd cmd;
240028916a16SEmmanuel Grumbach 	} u = {};
240128916a16SEmmanuel Grumbach 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
240228916a16SEmmanuel Grumbach 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
240328916a16SEmmanuel Grumbach 	__le16 key_flags;
240428916a16SEmmanuel Grumbach 	int ret, size;
240528916a16SEmmanuel Grumbach 	u32 status;
240628916a16SEmmanuel Grumbach 
240728916a16SEmmanuel Grumbach 	/* This is a valid situation for GTK removal */
240828916a16SEmmanuel Grumbach 	if (sta_id == IWL_MVM_INVALID_STA)
240928916a16SEmmanuel Grumbach 		return 0;
241028916a16SEmmanuel Grumbach 
241128916a16SEmmanuel Grumbach 	key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
241228916a16SEmmanuel Grumbach 				 STA_KEY_FLG_KEYID_MSK);
241328916a16SEmmanuel Grumbach 	key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
241428916a16SEmmanuel Grumbach 	key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
241528916a16SEmmanuel Grumbach 
241628916a16SEmmanuel Grumbach 	if (mcast)
241728916a16SEmmanuel Grumbach 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
241828916a16SEmmanuel Grumbach 
241928916a16SEmmanuel Grumbach 	/*
242028916a16SEmmanuel Grumbach 	 * The fields assigned here are in the same location at the start
242128916a16SEmmanuel Grumbach 	 * of the command, so we can do this union trick.
242228916a16SEmmanuel Grumbach 	 */
242328916a16SEmmanuel Grumbach 	u.cmd.common.key_flags = key_flags;
242428916a16SEmmanuel Grumbach 	u.cmd.common.key_offset = keyconf->hw_key_idx;
242528916a16SEmmanuel Grumbach 	u.cmd.common.sta_id = sta_id;
242628916a16SEmmanuel Grumbach 
242728916a16SEmmanuel Grumbach 	size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
242828916a16SEmmanuel Grumbach 
242928916a16SEmmanuel Grumbach 	status = ADD_STA_SUCCESS;
243028916a16SEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
243128916a16SEmmanuel Grumbach 					  &status);
243228916a16SEmmanuel Grumbach 
243328916a16SEmmanuel Grumbach 	switch (status) {
243428916a16SEmmanuel Grumbach 	case ADD_STA_SUCCESS:
243528916a16SEmmanuel Grumbach 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
243628916a16SEmmanuel Grumbach 		break;
243728916a16SEmmanuel Grumbach 	default:
243828916a16SEmmanuel Grumbach 		ret = -EIO;
243928916a16SEmmanuel Grumbach 		IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
244028916a16SEmmanuel Grumbach 		break;
244128916a16SEmmanuel Grumbach 	}
244228916a16SEmmanuel Grumbach 
244328916a16SEmmanuel Grumbach 	return ret;
244428916a16SEmmanuel Grumbach }
244528916a16SEmmanuel Grumbach 
244626d6c16bSSara Sharon /*
244726d6c16bSSara Sharon  * Send the FW a request to remove the station from it's internal data
244826d6c16bSSara Sharon  * structures, and in addition remove it from the local data structure.
244926d6c16bSSara Sharon  */
245026d6c16bSSara Sharon int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
245126d6c16bSSara Sharon {
245226d6c16bSSara Sharon 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
245326d6c16bSSara Sharon 	int ret;
245426d6c16bSSara Sharon 
245526d6c16bSSara Sharon 	lockdep_assert_held(&mvm->mutex);
245626d6c16bSSara Sharon 
2457f9084775SNathan Errera 	iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true);
2458d49394a1SSara Sharon 
2459c6ce1c74SJohannes Berg 	iwl_mvm_disable_txq(mvm, NULL, &mvmvif->cab_queue, 0, 0);
246026d6c16bSSara Sharon 
246126d6c16bSSara Sharon 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
246226d6c16bSSara Sharon 	if (ret)
246326d6c16bSSara Sharon 		IWL_WARN(mvm, "Failed sending remove station\n");
246426d6c16bSSara Sharon 
246526d6c16bSSara Sharon 	return ret;
246626d6c16bSSara Sharon }
246726d6c16bSSara Sharon 
2468b915c101SSara Sharon static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
246910b2b201SSara Sharon {
24705e1688ceSJohannes Berg 	struct iwl_mvm_delba_data notif = {
24715e1688ceSJohannes Berg 		.baid = baid,
2472b915c101SSara Sharon 	};
24735e1688ceSJohannes Berg 
24745e1688ceSJohannes Berg 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_NOTIF_DEL_BA, true,
24755e1688ceSJohannes Berg 					&notif, sizeof(notif));
247610b2b201SSara Sharon };
247710b2b201SSara Sharon 
2478b915c101SSara Sharon static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2479b915c101SSara Sharon 				 struct iwl_mvm_baid_data *data)
2480b915c101SSara Sharon {
2481b915c101SSara Sharon 	int i;
2482b915c101SSara Sharon 
2483b915c101SSara Sharon 	iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2484b915c101SSara Sharon 
2485b915c101SSara Sharon 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2486b915c101SSara Sharon 		int j;
2487b915c101SSara Sharon 		struct iwl_mvm_reorder_buffer *reorder_buf =
2488b915c101SSara Sharon 			&data->reorder_buf[i];
2489dfdddd92SJohannes Berg 		struct iwl_mvm_reorder_buf_entry *entries =
2490dfdddd92SJohannes Berg 			&data->entries[i * data->entries_per_queue];
2491b915c101SSara Sharon 
24920690405fSSara Sharon 		spin_lock_bh(&reorder_buf->lock);
24930690405fSSara Sharon 		if (likely(!reorder_buf->num_stored)) {
24940690405fSSara Sharon 			spin_unlock_bh(&reorder_buf->lock);
2495b915c101SSara Sharon 			continue;
24960690405fSSara Sharon 		}
2497b915c101SSara Sharon 
2498b915c101SSara Sharon 		/*
2499b915c101SSara Sharon 		 * This shouldn't happen in regular DELBA since the internal
2500b915c101SSara Sharon 		 * delBA notification should trigger a release of all frames in
2501b915c101SSara Sharon 		 * the reorder buffer.
2502b915c101SSara Sharon 		 */
2503b915c101SSara Sharon 		WARN_ON(1);
2504b915c101SSara Sharon 
2505b915c101SSara Sharon 		for (j = 0; j < reorder_buf->buf_size; j++)
2506dfdddd92SJohannes Berg 			__skb_queue_purge(&entries[j].e.frames);
25070690405fSSara Sharon 		/*
25080690405fSSara Sharon 		 * Prevent timer re-arm. This prevents a very far fetched case
25090690405fSSara Sharon 		 * where we timed out on the notification. There may be prior
25100690405fSSara Sharon 		 * RX frames pending in the RX queue before the notification
25110690405fSSara Sharon 		 * that might get processed between now and the actual deletion
25120690405fSSara Sharon 		 * and we would re-arm the timer although we are deleting the
25130690405fSSara Sharon 		 * reorder buffer.
25140690405fSSara Sharon 		 */
25150690405fSSara Sharon 		reorder_buf->removed = true;
25160690405fSSara Sharon 		spin_unlock_bh(&reorder_buf->lock);
25170690405fSSara Sharon 		del_timer_sync(&reorder_buf->reorder_timer);
2518b915c101SSara Sharon 	}
2519b915c101SSara Sharon }
2520b915c101SSara Sharon 
2521b915c101SSara Sharon static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2522b915c101SSara Sharon 					struct iwl_mvm_baid_data *data,
2523514c3069SLuca Coelho 					u16 ssn, u16 buf_size)
2524b915c101SSara Sharon {
2525b915c101SSara Sharon 	int i;
2526b915c101SSara Sharon 
2527b915c101SSara Sharon 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2528b915c101SSara Sharon 		struct iwl_mvm_reorder_buffer *reorder_buf =
2529b915c101SSara Sharon 			&data->reorder_buf[i];
2530dfdddd92SJohannes Berg 		struct iwl_mvm_reorder_buf_entry *entries =
2531dfdddd92SJohannes Berg 			&data->entries[i * data->entries_per_queue];
2532b915c101SSara Sharon 		int j;
2533b915c101SSara Sharon 
2534b915c101SSara Sharon 		reorder_buf->num_stored = 0;
2535b915c101SSara Sharon 		reorder_buf->head_sn = ssn;
2536b915c101SSara Sharon 		reorder_buf->buf_size = buf_size;
25370690405fSSara Sharon 		/* rx reorder timer */
25388cef5344SKees Cook 		timer_setup(&reorder_buf->reorder_timer,
25398cef5344SKees Cook 			    iwl_mvm_reorder_timer_expired, 0);
25400690405fSSara Sharon 		spin_lock_init(&reorder_buf->lock);
25410690405fSSara Sharon 		reorder_buf->mvm = mvm;
2542b915c101SSara Sharon 		reorder_buf->queue = i;
25435d43eab6SSara Sharon 		reorder_buf->valid = false;
2544b915c101SSara Sharon 		for (j = 0; j < reorder_buf->buf_size; j++)
2545dfdddd92SJohannes Berg 			__skb_queue_head_init(&entries[j].e.frames);
2546b915c101SSara Sharon 	}
254710b2b201SSara Sharon }
254810b2b201SSara Sharon 
254997f70c56SJohannes Berg static int iwl_mvm_fw_baid_op_sta(struct iwl_mvm *mvm,
255097f70c56SJohannes Berg 				  struct iwl_mvm_sta *mvm_sta,
255197f70c56SJohannes Berg 				  bool start, int tid, u16 ssn,
255297f70c56SJohannes Berg 				  u16 buf_size)
25530f3a4e48SJohannes Berg {
25540f3a4e48SJohannes Berg 	struct iwl_mvm_add_sta_cmd cmd = {
25550f3a4e48SJohannes Berg 		.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
25560f3a4e48SJohannes Berg 		.sta_id = mvm_sta->sta_id,
25570f3a4e48SJohannes Berg 		.add_modify = STA_MODE_MODIFY,
25580f3a4e48SJohannes Berg 	};
25590f3a4e48SJohannes Berg 	u32 status;
25600f3a4e48SJohannes Berg 	int ret;
25610f3a4e48SJohannes Berg 
25620f3a4e48SJohannes Berg 	if (start) {
25630f3a4e48SJohannes Berg 		cmd.add_immediate_ba_tid = tid;
25640f3a4e48SJohannes Berg 		cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
25650f3a4e48SJohannes Berg 		cmd.rx_ba_window = cpu_to_le16(buf_size);
25660f3a4e48SJohannes Berg 		cmd.modify_mask = STA_MODIFY_ADD_BA_TID;
25670f3a4e48SJohannes Berg 	} else {
25680f3a4e48SJohannes Berg 		cmd.remove_immediate_ba_tid = tid;
25690f3a4e48SJohannes Berg 		cmd.modify_mask = STA_MODIFY_REMOVE_BA_TID;
25700f3a4e48SJohannes Berg 	}
25710f3a4e48SJohannes Berg 
25720f3a4e48SJohannes Berg 	status = ADD_STA_SUCCESS;
25730f3a4e48SJohannes Berg 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
25740f3a4e48SJohannes Berg 					  iwl_mvm_add_sta_cmd_size(mvm),
25750f3a4e48SJohannes Berg 					  &cmd, &status);
25760f3a4e48SJohannes Berg 	if (ret)
25770f3a4e48SJohannes Berg 		return ret;
25780f3a4e48SJohannes Berg 
25790f3a4e48SJohannes Berg 	switch (status & IWL_ADD_STA_STATUS_MASK) {
25800f3a4e48SJohannes Berg 	case ADD_STA_SUCCESS:
25810f3a4e48SJohannes Berg 		IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
25820f3a4e48SJohannes Berg 			     start ? "start" : "stopp");
25830f3a4e48SJohannes Berg 		if (WARN_ON(start && iwl_mvm_has_new_rx_api(mvm) &&
25840f3a4e48SJohannes Berg 			    !(status & IWL_ADD_STA_BAID_VALID_MASK)))
25850f3a4e48SJohannes Berg 			return -EINVAL;
25860f3a4e48SJohannes Berg 		return u32_get_bits(status, IWL_ADD_STA_BAID_MASK);
25870f3a4e48SJohannes Berg 	case ADD_STA_IMMEDIATE_BA_FAILURE:
25880f3a4e48SJohannes Berg 		IWL_WARN(mvm, "RX BA Session refused by fw\n");
25890f3a4e48SJohannes Berg 		return -ENOSPC;
25900f3a4e48SJohannes Berg 	default:
25910f3a4e48SJohannes Berg 		IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
25920f3a4e48SJohannes Berg 			start ? "start" : "stopp", status);
25930f3a4e48SJohannes Berg 		return -EIO;
25940f3a4e48SJohannes Berg 	}
25950f3a4e48SJohannes Berg }
25960f3a4e48SJohannes Berg 
259797f70c56SJohannes Berg static int iwl_mvm_fw_baid_op_cmd(struct iwl_mvm *mvm,
259897f70c56SJohannes Berg 				  struct iwl_mvm_sta *mvm_sta,
259997f70c56SJohannes Berg 				  bool start, int tid, u16 ssn,
260097f70c56SJohannes Berg 				  u16 buf_size, int baid)
260197f70c56SJohannes Berg {
260297f70c56SJohannes Berg 	struct iwl_rx_baid_cfg_cmd cmd = {
260397f70c56SJohannes Berg 		.action = start ? cpu_to_le32(IWL_RX_BAID_ACTION_ADD) :
260497f70c56SJohannes Berg 				  cpu_to_le32(IWL_RX_BAID_ACTION_REMOVE),
260597f70c56SJohannes Berg 	};
260697f70c56SJohannes Berg 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, RX_BAID_ALLOCATION_CONFIG_CMD);
260797f70c56SJohannes Berg 	int ret;
260897f70c56SJohannes Berg 
260997f70c56SJohannes Berg 	BUILD_BUG_ON(sizeof(struct iwl_rx_baid_cfg_resp) != sizeof(baid));
261097f70c56SJohannes Berg 
261197f70c56SJohannes Berg 	if (start) {
261297f70c56SJohannes Berg 		cmd.alloc.sta_id_mask = cpu_to_le32(BIT(mvm_sta->sta_id));
261397f70c56SJohannes Berg 		cmd.alloc.tid = tid;
261497f70c56SJohannes Berg 		cmd.alloc.ssn = cpu_to_le16(ssn);
261597f70c56SJohannes Berg 		cmd.alloc.win_size = cpu_to_le16(buf_size);
261697f70c56SJohannes Berg 		baid = -EIO;
261797f70c56SJohannes Berg 	} else {
261897f70c56SJohannes Berg 		cmd.remove.baid = cpu_to_le32(baid);
261997f70c56SJohannes Berg 	}
262097f70c56SJohannes Berg 
262197f70c56SJohannes Berg 	ret = iwl_mvm_send_cmd_pdu_status(mvm, cmd_id, sizeof(cmd),
262297f70c56SJohannes Berg 					  &cmd, &baid);
262397f70c56SJohannes Berg 	if (ret)
262497f70c56SJohannes Berg 		return ret;
262597f70c56SJohannes Berg 
262697f70c56SJohannes Berg 	if (!start) {
262797f70c56SJohannes Berg 		/* ignore firmware baid on remove */
262897f70c56SJohannes Berg 		baid = 0;
262997f70c56SJohannes Berg 	}
263097f70c56SJohannes Berg 
263197f70c56SJohannes Berg 	IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
263297f70c56SJohannes Berg 		     start ? "start" : "stopp");
263397f70c56SJohannes Berg 
263497f70c56SJohannes Berg 	if (baid < 0 || baid >= ARRAY_SIZE(mvm->baid_map))
263597f70c56SJohannes Berg 		return -EINVAL;
263697f70c56SJohannes Berg 
263797f70c56SJohannes Berg 	return baid;
263897f70c56SJohannes Berg }
263997f70c56SJohannes Berg 
264097f70c56SJohannes Berg static int iwl_mvm_fw_baid_op(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvm_sta,
264197f70c56SJohannes Berg 			      bool start, int tid, u16 ssn, u16 buf_size,
264297f70c56SJohannes Berg 			      int baid)
264397f70c56SJohannes Berg {
264497f70c56SJohannes Berg 	if (fw_has_capa(&mvm->fw->ucode_capa,
264597f70c56SJohannes Berg 			IWL_UCODE_TLV_CAPA_BAID_ML_SUPPORT))
264697f70c56SJohannes Berg 		return iwl_mvm_fw_baid_op_cmd(mvm, mvm_sta, start,
264797f70c56SJohannes Berg 					      tid, ssn, buf_size, baid);
264897f70c56SJohannes Berg 
264997f70c56SJohannes Berg 	return iwl_mvm_fw_baid_op_sta(mvm, mvm_sta, start,
265097f70c56SJohannes Berg 				      tid, ssn, buf_size);
265197f70c56SJohannes Berg }
265297f70c56SJohannes Berg 
2653e705c121SKalle Valo int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2654514c3069SLuca Coelho 		       int tid, u16 ssn, bool start, u16 buf_size, u16 timeout)
2655e705c121SKalle Valo {
2656e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
265710b2b201SSara Sharon 	struct iwl_mvm_baid_data *baid_data = NULL;
26580f3a4e48SJohannes Berg 	int ret, baid;
2659ded5ded3SEmmanuel Grumbach 	u32 max_ba_id_sessions = iwl_mvm_has_new_tx_api(mvm) ? IWL_MAX_BAID :
2660ded5ded3SEmmanuel Grumbach 							       IWL_MAX_BAID_OLD;
2661e705c121SKalle Valo 
2662e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2663e705c121SKalle Valo 
2664ded5ded3SEmmanuel Grumbach 	if (start && mvm->rx_ba_sessions >= max_ba_id_sessions) {
2665e705c121SKalle Valo 		IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2666e705c121SKalle Valo 		return -ENOSPC;
2667e705c121SKalle Valo 	}
2668e705c121SKalle Valo 
266910b2b201SSara Sharon 	if (iwl_mvm_has_new_rx_api(mvm) && start) {
2670dfdddd92SJohannes Berg 		u16 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2671dfdddd92SJohannes Berg 
2672dfdddd92SJohannes Berg 		/* sparse doesn't like the __align() so don't check */
2673dfdddd92SJohannes Berg #ifndef __CHECKER__
2674dfdddd92SJohannes Berg 		/*
2675dfdddd92SJohannes Berg 		 * The division below will be OK if either the cache line size
2676dfdddd92SJohannes Berg 		 * can be divided by the entry size (ALIGN will round up) or if
2677dfdddd92SJohannes Berg 		 * if the entry size can be divided by the cache line size, in
2678dfdddd92SJohannes Berg 		 * which case the ALIGN() will do nothing.
2679dfdddd92SJohannes Berg 		 */
2680dfdddd92SJohannes Berg 		BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2681dfdddd92SJohannes Berg 			     sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2682dfdddd92SJohannes Berg #endif
2683dfdddd92SJohannes Berg 
2684dfdddd92SJohannes Berg 		/*
2685dfdddd92SJohannes Berg 		 * Upward align the reorder buffer size to fill an entire cache
2686dfdddd92SJohannes Berg 		 * line for each queue, to avoid sharing cache lines between
2687dfdddd92SJohannes Berg 		 * different queues.
2688dfdddd92SJohannes Berg 		 */
2689dfdddd92SJohannes Berg 		reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2690dfdddd92SJohannes Berg 
269110b2b201SSara Sharon 		/*
269210b2b201SSara Sharon 		 * Allocate here so if allocation fails we can bail out early
269310b2b201SSara Sharon 		 * before starting the BA session in the firmware
269410b2b201SSara Sharon 		 */
2695b915c101SSara Sharon 		baid_data = kzalloc(sizeof(*baid_data) +
2696b915c101SSara Sharon 				    mvm->trans->num_rx_queues *
2697dfdddd92SJohannes Berg 				    reorder_buf_size,
2698b915c101SSara Sharon 				    GFP_KERNEL);
269910b2b201SSara Sharon 		if (!baid_data)
270010b2b201SSara Sharon 			return -ENOMEM;
2701dfdddd92SJohannes Berg 
2702dfdddd92SJohannes Berg 		/*
2703dfdddd92SJohannes Berg 		 * This division is why we need the above BUILD_BUG_ON(),
2704dfdddd92SJohannes Berg 		 * if that doesn't hold then this will not be right.
2705dfdddd92SJohannes Berg 		 */
2706dfdddd92SJohannes Berg 		baid_data->entries_per_queue =
2707dfdddd92SJohannes Berg 			reorder_buf_size / sizeof(baid_data->entries[0]);
270810b2b201SSara Sharon 	}
270910b2b201SSara Sharon 
271097f70c56SJohannes Berg 	if (iwl_mvm_has_new_rx_api(mvm) && !start) {
271197f70c56SJohannes Berg 		baid = mvm_sta->tid_to_baid[tid];
271297f70c56SJohannes Berg 	} else {
271397f70c56SJohannes Berg 		/* we don't really need it in this case */
271497f70c56SJohannes Berg 		baid = -1;
271597f70c56SJohannes Berg 	}
271697f70c56SJohannes Berg 
2717*3538c809SLuca Coelho 	/* Don't send command to remove (start=0) BAID during restart */
2718*3538c809SLuca Coelho 	if (start || !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
271997f70c56SJohannes Berg 		baid = iwl_mvm_fw_baid_op(mvm, mvm_sta, start, tid, ssn, buf_size,
272097f70c56SJohannes Berg 					  baid);
2721*3538c809SLuca Coelho 
27220f3a4e48SJohannes Berg 	if (baid < 0) {
27230f3a4e48SJohannes Berg 		ret = baid;
272410b2b201SSara Sharon 		goto out_free;
2725e705c121SKalle Valo 	}
2726e705c121SKalle Valo 
272710b2b201SSara Sharon 	if (start) {
2728e705c121SKalle Valo 		mvm->rx_ba_sessions++;
272910b2b201SSara Sharon 
273010b2b201SSara Sharon 		if (!iwl_mvm_has_new_rx_api(mvm))
273110b2b201SSara Sharon 			return 0;
273210b2b201SSara Sharon 
273310b2b201SSara Sharon 		baid_data->baid = baid;
273410b2b201SSara Sharon 		baid_data->timeout = timeout;
273510b2b201SSara Sharon 		baid_data->last_rx = jiffies;
27368cef5344SKees Cook 		baid_data->rcu_ptr = &mvm->baid_map[baid];
27378cef5344SKees Cook 		timer_setup(&baid_data->session_timer,
27388cef5344SKees Cook 			    iwl_mvm_rx_agg_session_expired, 0);
273910b2b201SSara Sharon 		baid_data->mvm = mvm;
274010b2b201SSara Sharon 		baid_data->tid = tid;
274110b2b201SSara Sharon 		baid_data->sta_id = mvm_sta->sta_id;
274210b2b201SSara Sharon 
274310b2b201SSara Sharon 		mvm_sta->tid_to_baid[tid] = baid;
274410b2b201SSara Sharon 		if (timeout)
274510b2b201SSara Sharon 			mod_timer(&baid_data->session_timer,
274610b2b201SSara Sharon 				  TU_TO_EXP_TIME(timeout * 2));
274710b2b201SSara Sharon 
27483f1c4c58SSara Sharon 		iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size);
274910b2b201SSara Sharon 		/*
275010b2b201SSara Sharon 		 * protect the BA data with RCU to cover a case where our
275110b2b201SSara Sharon 		 * internal RX sync mechanism will timeout (not that it's
275210b2b201SSara Sharon 		 * supposed to happen) and we will free the session data while
275310b2b201SSara Sharon 		 * RX is being processed in parallel
275410b2b201SSara Sharon 		 */
275535263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
275635263a03SSara Sharon 			     mvm_sta->sta_id, tid, baid);
275710b2b201SSara Sharon 		WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
275810b2b201SSara Sharon 		rcu_assign_pointer(mvm->baid_map[baid], baid_data);
275960dec523SSara Sharon 	} else  {
27600f3a4e48SJohannes Berg 		baid = mvm_sta->tid_to_baid[tid];
276110b2b201SSara Sharon 
276260dec523SSara Sharon 		if (mvm->rx_ba_sessions > 0)
2763e705c121SKalle Valo 			/* check that restart flow didn't zero the counter */
2764e705c121SKalle Valo 			mvm->rx_ba_sessions--;
276510b2b201SSara Sharon 		if (!iwl_mvm_has_new_rx_api(mvm))
276610b2b201SSara Sharon 			return 0;
2767e705c121SKalle Valo 
276810b2b201SSara Sharon 		if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
276910b2b201SSara Sharon 			return -EINVAL;
277010b2b201SSara Sharon 
277110b2b201SSara Sharon 		baid_data = rcu_access_pointer(mvm->baid_map[baid]);
277210b2b201SSara Sharon 		if (WARN_ON(!baid_data))
277310b2b201SSara Sharon 			return -EINVAL;
277410b2b201SSara Sharon 
277510b2b201SSara Sharon 		/* synchronize all rx queues so we can safely delete */
2776b915c101SSara Sharon 		iwl_mvm_free_reorder(mvm, baid_data);
277710b2b201SSara Sharon 		del_timer_sync(&baid_data->session_timer);
277810b2b201SSara Sharon 		RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
277910b2b201SSara Sharon 		kfree_rcu(baid_data, rcu_head);
278035263a03SSara Sharon 		IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
27812438d430SJohannes Berg 
27822438d430SJohannes Berg 		/*
27832438d430SJohannes Berg 		 * After we've deleted it, do another queue sync
27842438d430SJohannes Berg 		 * so if an IWL_MVM_RXQ_NSSN_SYNC was concurrently
27852438d430SJohannes Berg 		 * running it won't find a new session in the old
27862438d430SJohannes Berg 		 * BAID. It can find the NULL pointer for the BAID,
27872438d430SJohannes Berg 		 * but we must not have it find a different session.
27882438d430SJohannes Berg 		 */
27892438d430SJohannes Berg 		iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY,
27902438d430SJohannes Berg 						true, NULL, 0);
279110b2b201SSara Sharon 	}
279210b2b201SSara Sharon 	return 0;
279310b2b201SSara Sharon 
279410b2b201SSara Sharon out_free:
279510b2b201SSara Sharon 	kfree(baid_data);
2796e705c121SKalle Valo 	return ret;
2797e705c121SKalle Valo }
2798e705c121SKalle Valo 
27999794c64fSLiad Kaufman int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2800e705c121SKalle Valo 		       int tid, u8 queue, bool start)
2801e705c121SKalle Valo {
2802e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2803e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {};
2804e705c121SKalle Valo 	int ret;
2805e705c121SKalle Valo 	u32 status;
2806e705c121SKalle Valo 
2807e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2808e705c121SKalle Valo 
2809e705c121SKalle Valo 	if (start) {
2810e705c121SKalle Valo 		mvm_sta->tfd_queue_msk |= BIT(queue);
2811e705c121SKalle Valo 		mvm_sta->tid_disable_agg &= ~BIT(tid);
2812e705c121SKalle Valo 	} else {
2813cf961e16SLiad Kaufman 		/* In DQA-mode the queue isn't removed on agg termination */
2814e705c121SKalle Valo 		mvm_sta->tid_disable_agg |= BIT(tid);
2815e705c121SKalle Valo 	}
2816e705c121SKalle Valo 
2817e705c121SKalle Valo 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2818e705c121SKalle Valo 	cmd.sta_id = mvm_sta->sta_id;
2819e705c121SKalle Valo 	cmd.add_modify = STA_MODE_MODIFY;
2820bb49701bSSara Sharon 	if (!iwl_mvm_has_new_tx_api(mvm))
2821bb49701bSSara Sharon 		cmd.modify_mask = STA_MODIFY_QUEUES;
2822bb49701bSSara Sharon 	cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
2823e705c121SKalle Valo 	cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2824e705c121SKalle Valo 	cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2825e705c121SKalle Valo 
2826e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
2827854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2828854c5705SSara Sharon 					  iwl_mvm_add_sta_cmd_size(mvm),
2829e705c121SKalle Valo 					  &cmd, &status);
2830e705c121SKalle Valo 	if (ret)
2831e705c121SKalle Valo 		return ret;
2832e705c121SKalle Valo 
2833837c4da9SSara Sharon 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2834e705c121SKalle Valo 	case ADD_STA_SUCCESS:
2835e705c121SKalle Valo 		break;
2836e705c121SKalle Valo 	default:
2837e705c121SKalle Valo 		ret = -EIO;
2838e705c121SKalle Valo 		IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2839e705c121SKalle Valo 			start ? "start" : "stopp", status);
2840e705c121SKalle Valo 		break;
2841e705c121SKalle Valo 	}
2842e705c121SKalle Valo 
2843e705c121SKalle Valo 	return ret;
2844e705c121SKalle Valo }
2845e705c121SKalle Valo 
2846e705c121SKalle Valo const u8 tid_to_mac80211_ac[] = {
2847e705c121SKalle Valo 	IEEE80211_AC_BE,
2848e705c121SKalle Valo 	IEEE80211_AC_BK,
2849e705c121SKalle Valo 	IEEE80211_AC_BK,
2850e705c121SKalle Valo 	IEEE80211_AC_BE,
2851e705c121SKalle Valo 	IEEE80211_AC_VI,
2852e705c121SKalle Valo 	IEEE80211_AC_VI,
2853e705c121SKalle Valo 	IEEE80211_AC_VO,
2854e705c121SKalle Valo 	IEEE80211_AC_VO,
28559794c64fSLiad Kaufman 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2856e705c121SKalle Valo };
2857e705c121SKalle Valo 
2858e705c121SKalle Valo static const u8 tid_to_ucode_ac[] = {
2859e705c121SKalle Valo 	AC_BE,
2860e705c121SKalle Valo 	AC_BK,
2861e705c121SKalle Valo 	AC_BK,
2862e705c121SKalle Valo 	AC_BE,
2863e705c121SKalle Valo 	AC_VI,
2864e705c121SKalle Valo 	AC_VI,
2865e705c121SKalle Valo 	AC_VO,
2866e705c121SKalle Valo 	AC_VO,
2867e705c121SKalle Valo };
2868e705c121SKalle Valo 
2869e705c121SKalle Valo int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2870e705c121SKalle Valo 			     struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2871e705c121SKalle Valo {
2872e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2873e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data;
2874dd32162dSLiad Kaufman 	u16 normalized_ssn;
2875b0d795a9SMordechay Goodstein 	u16 txq_id;
2876e705c121SKalle Valo 	int ret;
2877e705c121SKalle Valo 
2878e705c121SKalle Valo 	if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2879e705c121SKalle Valo 		return -EINVAL;
2880e705c121SKalle Valo 
2881bd800e41SNaftali Goldstein 	if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
2882bd800e41SNaftali Goldstein 	    mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2883bd800e41SNaftali Goldstein 		IWL_ERR(mvm,
2884bd800e41SNaftali Goldstein 			"Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
2885e705c121SKalle Valo 			mvmsta->tid_data[tid].state);
2886e705c121SKalle Valo 		return -ENXIO;
2887e705c121SKalle Valo 	}
2888e705c121SKalle Valo 
2889e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
2890e705c121SKalle Valo 
2891bd8f3fc6SLiad Kaufman 	if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE &&
2892bd8f3fc6SLiad Kaufman 	    iwl_mvm_has_new_tx_api(mvm)) {
2893bd8f3fc6SLiad Kaufman 		u8 ac = tid_to_mac80211_ac[tid];
2894bd8f3fc6SLiad Kaufman 
2895bd8f3fc6SLiad Kaufman 		ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
2896bd8f3fc6SLiad Kaufman 		if (ret)
2897bd8f3fc6SLiad Kaufman 			return ret;
2898bd8f3fc6SLiad Kaufman 	}
2899bd8f3fc6SLiad Kaufman 
2900e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
2901e705c121SKalle Valo 
2902cf961e16SLiad Kaufman 	/*
2903cf961e16SLiad Kaufman 	 * Note the possible cases:
29044a6d2e52SAvraham Stern 	 *  1. An enabled TXQ - TXQ needs to become agg'ed
29054a6d2e52SAvraham Stern 	 *  2. The TXQ hasn't yet been enabled, so find a free one and mark
29064a6d2e52SAvraham Stern 	 *	it as reserved
2907cf961e16SLiad Kaufman 	 */
2908cf961e16SLiad Kaufman 	txq_id = mvmsta->tid_data[tid].txq_id;
290934e10860SSara Sharon 	if (txq_id == IWL_MVM_INVALID_QUEUE) {
2910b0d795a9SMordechay Goodstein 		ret = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2911c8f54701SJohannes Berg 					      IWL_MVM_DQA_MIN_DATA_QUEUE,
2912c8f54701SJohannes Berg 					      IWL_MVM_DQA_MAX_DATA_QUEUE);
2913b0d795a9SMordechay Goodstein 		if (ret < 0) {
2914e705c121SKalle Valo 			IWL_ERR(mvm, "Failed to allocate agg queue\n");
2915f3f240f9SJohannes Berg 			goto out;
2916e705c121SKalle Valo 		}
2917cf961e16SLiad Kaufman 
2918b0d795a9SMordechay Goodstein 		txq_id = ret;
2919b0d795a9SMordechay Goodstein 
2920cf961e16SLiad Kaufman 		/* TXQ hasn't yet been enabled, so mark it only as reserved */
2921cf961e16SLiad Kaufman 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2922b0d795a9SMordechay Goodstein 	} else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) {
2923b0d795a9SMordechay Goodstein 		ret = -ENXIO;
2924b0d795a9SMordechay Goodstein 		IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n",
2925b0d795a9SMordechay Goodstein 			tid, IWL_MAX_HW_QUEUES - 1);
2926b0d795a9SMordechay Goodstein 		goto out;
2927b0d795a9SMordechay Goodstein 
29284a6d2e52SAvraham Stern 	} else if (unlikely(mvm->queue_info[txq_id].status ==
29294a6d2e52SAvraham Stern 			    IWL_MVM_QUEUE_SHARED)) {
29304a6d2e52SAvraham Stern 		ret = -ENXIO;
29314a6d2e52SAvraham Stern 		IWL_DEBUG_TX_QUEUES(mvm,
29324a6d2e52SAvraham Stern 				    "Can't start tid %d agg on shared queue!\n",
29334a6d2e52SAvraham Stern 				    tid);
2934f3f240f9SJohannes Berg 		goto out;
2935cf961e16SLiad Kaufman 	}
29369f9af3d7SLiad Kaufman 
2937cf961e16SLiad Kaufman 	IWL_DEBUG_TX_QUEUES(mvm,
2938cf961e16SLiad Kaufman 			    "AGG for tid %d will be on queue #%d\n",
2939cf961e16SLiad Kaufman 			    tid, txq_id);
2940cf961e16SLiad Kaufman 
2941e705c121SKalle Valo 	tid_data = &mvmsta->tid_data[tid];
2942e705c121SKalle Valo 	tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2943e705c121SKalle Valo 	tid_data->txq_id = txq_id;
2944e705c121SKalle Valo 	*ssn = tid_data->ssn;
2945e705c121SKalle Valo 
2946e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm,
2947e705c121SKalle Valo 			    "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2948e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2949e705c121SKalle Valo 			    tid_data->next_reclaimed);
2950e705c121SKalle Valo 
2951dd32162dSLiad Kaufman 	/*
29522f7a3863SLuca Coelho 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
2953dd32162dSLiad Kaufman 	 * to align the wrap around of ssn so we compare relevant values.
2954dd32162dSLiad Kaufman 	 */
2955dd32162dSLiad Kaufman 	normalized_ssn = tid_data->ssn;
2956286ca8ebSLuca Coelho 	if (mvm->trans->trans_cfg->gen2)
2957dd32162dSLiad Kaufman 		normalized_ssn &= 0xff;
2958dd32162dSLiad Kaufman 
2959dd32162dSLiad Kaufman 	if (normalized_ssn == tid_data->next_reclaimed) {
2960e705c121SKalle Valo 		tid_data->state = IWL_AGG_STARTING;
29612ce113deSJohannes Berg 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
2962e705c121SKalle Valo 	} else {
2963e705c121SKalle Valo 		tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
29647e0ca723SMordechay Goodstein 		ret = IEEE80211_AMPDU_TX_START_DELAY_ADDBA;
29652ce113deSJohannes Berg 	}
2966e705c121SKalle Valo 
29679f9af3d7SLiad Kaufman out:
2968e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
2969e705c121SKalle Valo 
2970e705c121SKalle Valo 	return ret;
2971e705c121SKalle Valo }
2972e705c121SKalle Valo 
2973e705c121SKalle Valo int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2974514c3069SLuca Coelho 			    struct ieee80211_sta *sta, u16 tid, u16 buf_size,
2975bb81bb68SEmmanuel Grumbach 			    bool amsdu)
2976e705c121SKalle Valo {
2977e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2978e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2979e705c121SKalle Valo 	unsigned int wdg_timeout =
2980e705c121SKalle Valo 		iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
2981eea76c36SEmmanuel Grumbach 	int queue, ret;
2982cf961e16SLiad Kaufman 	bool alloc_queue = true;
29839f9af3d7SLiad Kaufman 	enum iwl_mvm_queue_status queue_status;
2984e705c121SKalle Valo 	u16 ssn;
2985e705c121SKalle Valo 
2986eea76c36SEmmanuel Grumbach 	struct iwl_trans_txq_scd_cfg cfg = {
2987eea76c36SEmmanuel Grumbach 		.sta_id = mvmsta->sta_id,
2988eea76c36SEmmanuel Grumbach 		.tid = tid,
2989eea76c36SEmmanuel Grumbach 		.frame_limit = buf_size,
2990eea76c36SEmmanuel Grumbach 		.aggregate = true,
2991eea76c36SEmmanuel Grumbach 	};
2992eea76c36SEmmanuel Grumbach 
2993ecaf71deSGregory Greenman 	/*
2994ecaf71deSGregory Greenman 	 * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
2995ecaf71deSGregory Greenman 	 * manager, so this function should never be called in this case.
2996ecaf71deSGregory Greenman 	 */
29974243edb4SEmmanuel Grumbach 	if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
2998ecaf71deSGregory Greenman 		return -EINVAL;
2999ecaf71deSGregory Greenman 
3000e705c121SKalle Valo 	BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
3001e705c121SKalle Valo 		     != IWL_MAX_TID_COUNT);
3002e705c121SKalle Valo 
3003e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3004e705c121SKalle Valo 	ssn = tid_data->ssn;
3005e705c121SKalle Valo 	queue = tid_data->txq_id;
3006e705c121SKalle Valo 	tid_data->state = IWL_AGG_ON;
3007e705c121SKalle Valo 	mvmsta->agg_tids |= BIT(tid);
3008e705c121SKalle Valo 	tid_data->ssn = 0xffff;
3009bb81bb68SEmmanuel Grumbach 	tid_data->amsdu_in_ampdu_allowed = amsdu;
3010e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3011e705c121SKalle Valo 
301234e10860SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm)) {
301334e10860SSara Sharon 		/*
30140ec9257bSSara Sharon 		 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
30150ec9257bSSara Sharon 		 * would have failed, so if we are here there is no need to
30160ec9257bSSara Sharon 		 * allocate a queue.
30170ec9257bSSara Sharon 		 * However, if aggregation size is different than the default
30180ec9257bSSara Sharon 		 * size, the scheduler should be reconfigured.
30190ec9257bSSara Sharon 		 * We cannot do this with the new TX API, so return unsupported
30200ec9257bSSara Sharon 		 * for now, until it will be offloaded to firmware..
30210ec9257bSSara Sharon 		 * Note that if SCD default value changes - this condition
30220ec9257bSSara Sharon 		 * should be updated as well.
302334e10860SSara Sharon 		 */
30240ec9257bSSara Sharon 		if (buf_size < IWL_FRAME_LIMIT)
302534e10860SSara Sharon 			return -ENOTSUPP;
302634e10860SSara Sharon 
302734e10860SSara Sharon 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
302834e10860SSara Sharon 		if (ret)
302934e10860SSara Sharon 			return -EIO;
303034e10860SSara Sharon 		goto out;
303134e10860SSara Sharon 	}
303234e10860SSara Sharon 
3033eea76c36SEmmanuel Grumbach 	cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
3034e705c121SKalle Valo 
30359f9af3d7SLiad Kaufman 	queue_status = mvm->queue_info[queue].status;
30369f9af3d7SLiad Kaufman 
3037cf961e16SLiad Kaufman 	/* Maybe there is no need to even alloc a queue... */
3038cf961e16SLiad Kaufman 	if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
3039cf961e16SLiad Kaufman 		alloc_queue = false;
3040cf961e16SLiad Kaufman 
3041cf961e16SLiad Kaufman 	/*
3042cf961e16SLiad Kaufman 	 * Only reconfig the SCD for the queue if the window size has
3043cf961e16SLiad Kaufman 	 * changed from current (become smaller)
3044cf961e16SLiad Kaufman 	 */
30450ec9257bSSara Sharon 	if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
3046cf961e16SLiad Kaufman 		/*
3047cf961e16SLiad Kaufman 		 * If reconfiguring an existing queue, it first must be
3048cf961e16SLiad Kaufman 		 * drained
3049cf961e16SLiad Kaufman 		 */
3050a1a57877SSara Sharon 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
3051cf961e16SLiad Kaufman 						     BIT(queue));
3052cf961e16SLiad Kaufman 		if (ret) {
3053cf961e16SLiad Kaufman 			IWL_ERR(mvm,
3054cf961e16SLiad Kaufman 				"Error draining queue before reconfig\n");
3055cf961e16SLiad Kaufman 			return ret;
3056cf961e16SLiad Kaufman 		}
3057cf961e16SLiad Kaufman 
3058cf961e16SLiad Kaufman 		ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
3059cf961e16SLiad Kaufman 					   mvmsta->sta_id, tid,
3060cf961e16SLiad Kaufman 					   buf_size, ssn);
3061cf961e16SLiad Kaufman 		if (ret) {
3062cf961e16SLiad Kaufman 			IWL_ERR(mvm,
3063cf961e16SLiad Kaufman 				"Error reconfiguring TXQ #%d\n", queue);
3064cf961e16SLiad Kaufman 			return ret;
3065cf961e16SLiad Kaufman 		}
3066cf961e16SLiad Kaufman 	}
3067cf961e16SLiad Kaufman 
3068cf961e16SLiad Kaufman 	if (alloc_queue)
3069cfbc6c4cSSara Sharon 		iwl_mvm_enable_txq(mvm, sta, queue, ssn,
3070cf961e16SLiad Kaufman 				   &cfg, wdg_timeout);
3071e705c121SKalle Valo 
30729f9af3d7SLiad Kaufman 	/* Send ADD_STA command to enable aggs only if the queue isn't shared */
30739f9af3d7SLiad Kaufman 	if (queue_status != IWL_MVM_QUEUE_SHARED) {
3074e705c121SKalle Valo 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
3075e705c121SKalle Valo 		if (ret)
3076e705c121SKalle Valo 			return -EIO;
30779f9af3d7SLiad Kaufman 	}
3078e705c121SKalle Valo 
3079e705c121SKalle Valo 	/* No need to mark as reserved */
3080cf961e16SLiad Kaufman 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
3081e705c121SKalle Valo 
308234e10860SSara Sharon out:
3083e705c121SKalle Valo 	/*
3084e705c121SKalle Valo 	 * Even though in theory the peer could have different
3085e705c121SKalle Valo 	 * aggregation reorder buffer sizes for different sessions,
3086e705c121SKalle Valo 	 * our ucode doesn't allow for that and has a global limit
3087e705c121SKalle Valo 	 * for each station. Therefore, use the minimum of all the
3088e705c121SKalle Valo 	 * aggregation sessions and our default value.
3089e705c121SKalle Valo 	 */
3090e705c121SKalle Valo 	mvmsta->max_agg_bufsize =
3091e705c121SKalle Valo 		min(mvmsta->max_agg_bufsize, buf_size);
3092ecaf71deSGregory Greenman 	mvmsta->lq_sta.rs_drv.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
3093e705c121SKalle Valo 
3094e705c121SKalle Valo 	IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
3095e705c121SKalle Valo 		     sta->addr, tid);
3096e705c121SKalle Valo 
3097cd4d6b0bSGregory Greenman 	return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.rs_drv.lq);
3098e705c121SKalle Valo }
3099e705c121SKalle Valo 
310034e10860SSara Sharon static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
310134e10860SSara Sharon 					struct iwl_mvm_sta *mvmsta,
31024b387906SAvraham Stern 					struct iwl_mvm_tid_data *tid_data)
310334e10860SSara Sharon {
31044b387906SAvraham Stern 	u16 txq_id = tid_data->txq_id;
31054b387906SAvraham Stern 
3106f3f240f9SJohannes Berg 	lockdep_assert_held(&mvm->mutex);
3107f3f240f9SJohannes Berg 
310834e10860SSara Sharon 	if (iwl_mvm_has_new_tx_api(mvm))
310934e10860SSara Sharon 		return;
311034e10860SSara Sharon 
311134e10860SSara Sharon 	/*
311234e10860SSara Sharon 	 * The TXQ is marked as reserved only if no traffic came through yet
311334e10860SSara Sharon 	 * This means no traffic has been sent on this TID (agg'd or not), so
311434e10860SSara Sharon 	 * we no longer have use for the queue. Since it hasn't even been
311534e10860SSara Sharon 	 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
311634e10860SSara Sharon 	 * free.
311734e10860SSara Sharon 	 */
31184b387906SAvraham Stern 	if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
311934e10860SSara Sharon 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
31204b387906SAvraham Stern 		tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
31214b387906SAvraham Stern 	}
312234e10860SSara Sharon }
312334e10860SSara Sharon 
3124e705c121SKalle Valo int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3125e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid)
3126e705c121SKalle Valo {
3127e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3128e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3129e705c121SKalle Valo 	u16 txq_id;
3130e705c121SKalle Valo 	int err;
3131e705c121SKalle Valo 
3132e705c121SKalle Valo 	/*
3133e705c121SKalle Valo 	 * If mac80211 is cleaning its state, then say that we finished since
3134e705c121SKalle Valo 	 * our state has been cleared anyway.
3135e705c121SKalle Valo 	 */
3136e705c121SKalle Valo 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3137e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3138e705c121SKalle Valo 		return 0;
3139e705c121SKalle Valo 	}
3140e705c121SKalle Valo 
3141e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3142e705c121SKalle Valo 
3143e705c121SKalle Valo 	txq_id = tid_data->txq_id;
3144e705c121SKalle Valo 
3145e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
3146e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
3147e705c121SKalle Valo 
3148e705c121SKalle Valo 	mvmsta->agg_tids &= ~BIT(tid);
3149e705c121SKalle Valo 
31504b387906SAvraham Stern 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3151e705c121SKalle Valo 
3152e705c121SKalle Valo 	switch (tid_data->state) {
3153e705c121SKalle Valo 	case IWL_AGG_ON:
3154e705c121SKalle Valo 		tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3155e705c121SKalle Valo 
3156e705c121SKalle Valo 		IWL_DEBUG_TX_QUEUES(mvm,
3157e705c121SKalle Valo 				    "ssn = %d, next_recl = %d\n",
3158e705c121SKalle Valo 				    tid_data->ssn, tid_data->next_reclaimed);
3159e705c121SKalle Valo 
3160e705c121SKalle Valo 		tid_data->ssn = 0xffff;
3161e705c121SKalle Valo 		tid_data->state = IWL_AGG_OFF;
3162e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
3163e705c121SKalle Valo 
3164e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3165e705c121SKalle Valo 
3166e705c121SKalle Valo 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3167e705c121SKalle Valo 		return 0;
3168e705c121SKalle Valo 	case IWL_AGG_STARTING:
3169e705c121SKalle Valo 	case IWL_EMPTYING_HW_QUEUE_ADDBA:
3170e705c121SKalle Valo 		/*
3171e705c121SKalle Valo 		 * The agg session has been stopped before it was set up. This
3172e705c121SKalle Valo 		 * can happen when the AddBA timer times out for example.
3173e705c121SKalle Valo 		 */
3174e705c121SKalle Valo 
3175e705c121SKalle Valo 		/* No barriers since we are under mutex */
3176e705c121SKalle Valo 		lockdep_assert_held(&mvm->mutex);
3177e705c121SKalle Valo 
3178e705c121SKalle Valo 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3179e705c121SKalle Valo 		tid_data->state = IWL_AGG_OFF;
3180e705c121SKalle Valo 		err = 0;
3181e705c121SKalle Valo 		break;
3182e705c121SKalle Valo 	default:
3183e705c121SKalle Valo 		IWL_ERR(mvm,
3184e705c121SKalle Valo 			"Stopping AGG while state not ON or starting for %d on %d (%d)\n",
3185e705c121SKalle Valo 			mvmsta->sta_id, tid, tid_data->state);
3186e705c121SKalle Valo 		IWL_ERR(mvm,
3187e705c121SKalle Valo 			"\ttid_data->txq_id = %d\n", tid_data->txq_id);
3188e705c121SKalle Valo 		err = -EINVAL;
3189e705c121SKalle Valo 	}
3190e705c121SKalle Valo 
3191e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3192e705c121SKalle Valo 
3193e705c121SKalle Valo 	return err;
3194e705c121SKalle Valo }
3195e705c121SKalle Valo 
3196e705c121SKalle Valo int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3197e705c121SKalle Valo 			    struct ieee80211_sta *sta, u16 tid)
3198e705c121SKalle Valo {
3199e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3200e705c121SKalle Valo 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3201e705c121SKalle Valo 	u16 txq_id;
3202e705c121SKalle Valo 	enum iwl_mvm_agg_state old_state;
3203e705c121SKalle Valo 
3204e705c121SKalle Valo 	/*
3205e705c121SKalle Valo 	 * First set the agg state to OFF to avoid calling
3206e705c121SKalle Valo 	 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
3207e705c121SKalle Valo 	 */
3208e705c121SKalle Valo 	spin_lock_bh(&mvmsta->lock);
3209e705c121SKalle Valo 	txq_id = tid_data->txq_id;
3210e705c121SKalle Valo 	IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
3211e705c121SKalle Valo 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
3212e705c121SKalle Valo 	old_state = tid_data->state;
3213e705c121SKalle Valo 	tid_data->state = IWL_AGG_OFF;
3214e705c121SKalle Valo 	mvmsta->agg_tids &= ~BIT(tid);
3215e705c121SKalle Valo 	spin_unlock_bh(&mvmsta->lock);
3216e705c121SKalle Valo 
32174b387906SAvraham Stern 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3218e705c121SKalle Valo 
3219e705c121SKalle Valo 	if (old_state >= IWL_AGG_ON) {
3220e705c121SKalle Valo 		iwl_mvm_drain_sta(mvm, mvmsta, true);
3221d167e81aSMordechai Goodstein 
3222d167e81aSMordechai Goodstein 		if (iwl_mvm_has_new_tx_api(mvm)) {
3223d167e81aSMordechai Goodstein 			if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
3224d4e3a341SMordechay Goodstein 						   BIT(tid)))
3225d167e81aSMordechai Goodstein 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3226d167e81aSMordechai Goodstein 			iwl_trans_wait_txq_empty(mvm->trans, txq_id);
3227d167e81aSMordechai Goodstein 		} else {
3228d4e3a341SMordechay Goodstein 			if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id)))
3229e705c121SKalle Valo 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
32300b90964aSSara Sharon 			iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
3231d167e81aSMordechai Goodstein 		}
3232d6d517b7SSara Sharon 
3233e705c121SKalle Valo 		iwl_mvm_drain_sta(mvm, mvmsta, false);
3234e705c121SKalle Valo 
3235e705c121SKalle Valo 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3236e705c121SKalle Valo 	}
3237e705c121SKalle Valo 
3238e705c121SKalle Valo 	return 0;
3239e705c121SKalle Valo }
3240e705c121SKalle Valo 
3241e705c121SKalle Valo static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
3242e705c121SKalle Valo {
3243e705c121SKalle Valo 	int i, max = -1, max_offs = -1;
3244e705c121SKalle Valo 
3245e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3246e705c121SKalle Valo 
3247e705c121SKalle Valo 	/* Pick the unused key offset with the highest 'deleted'
3248e705c121SKalle Valo 	 * counter. Every time a key is deleted, all the counters
3249e705c121SKalle Valo 	 * are incremented and the one that was just deleted is
3250e705c121SKalle Valo 	 * reset to zero. Thus, the highest counter is the one
3251e705c121SKalle Valo 	 * that was deleted longest ago. Pick that one.
3252e705c121SKalle Valo 	 */
3253e705c121SKalle Valo 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3254e705c121SKalle Valo 		if (test_bit(i, mvm->fw_key_table))
3255e705c121SKalle Valo 			continue;
3256e705c121SKalle Valo 		if (mvm->fw_key_deleted[i] > max) {
3257e705c121SKalle Valo 			max = mvm->fw_key_deleted[i];
3258e705c121SKalle Valo 			max_offs = i;
3259e705c121SKalle Valo 		}
3260e705c121SKalle Valo 	}
3261e705c121SKalle Valo 
3262e705c121SKalle Valo 	if (max_offs < 0)
3263e705c121SKalle Valo 		return STA_KEY_IDX_INVALID;
3264e705c121SKalle Valo 
3265e705c121SKalle Valo 	return max_offs;
3266e705c121SKalle Valo }
3267e705c121SKalle Valo 
32685f7a1847SJohannes Berg static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
32694615fd15SEmmanuel Grumbach 					       struct ieee80211_vif *vif,
3270e705c121SKalle Valo 					       struct ieee80211_sta *sta)
3271e705c121SKalle Valo {
3272e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3273e705c121SKalle Valo 
32745f7a1847SJohannes Berg 	if (sta)
32755f7a1847SJohannes Berg 		return iwl_mvm_sta_from_mac80211(sta);
3276e705c121SKalle Valo 
3277e705c121SKalle Valo 	/*
3278e705c121SKalle Valo 	 * The device expects GTKs for station interfaces to be
3279e705c121SKalle Valo 	 * installed as GTKs for the AP station. If we have no
3280e705c121SKalle Valo 	 * station ID, then use AP's station ID.
3281e705c121SKalle Valo 	 */
3282e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
32830ae98812SSara Sharon 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
32844615fd15SEmmanuel Grumbach 		u8 sta_id = mvmvif->ap_sta_id;
32854615fd15SEmmanuel Grumbach 
32867d6a1ab6SEmmanuel Grumbach 		sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
32877d6a1ab6SEmmanuel Grumbach 					    lockdep_is_held(&mvm->mutex));
32887d6a1ab6SEmmanuel Grumbach 
32894615fd15SEmmanuel Grumbach 		/*
32904615fd15SEmmanuel Grumbach 		 * It is possible that the 'sta' parameter is NULL,
32914615fd15SEmmanuel Grumbach 		 * for example when a GTK is removed - the sta_id will then
32924615fd15SEmmanuel Grumbach 		 * be the AP ID, and no station was passed by mac80211.
32934615fd15SEmmanuel Grumbach 		 */
32947d6a1ab6SEmmanuel Grumbach 		if (IS_ERR_OR_NULL(sta))
32957d6a1ab6SEmmanuel Grumbach 			return NULL;
32967d6a1ab6SEmmanuel Grumbach 
32977d6a1ab6SEmmanuel Grumbach 		return iwl_mvm_sta_from_mac80211(sta);
32984615fd15SEmmanuel Grumbach 	}
3299e705c121SKalle Valo 
33005f7a1847SJohannes Berg 	return NULL;
3301e705c121SKalle Valo }
3302e705c121SKalle Valo 
3303a6dfbd04SJohannes Berg static int iwl_mvm_pn_cmp(const u8 *pn1, const u8 *pn2, int len)
3304a6dfbd04SJohannes Berg {
3305a6dfbd04SJohannes Berg 	int i;
3306a6dfbd04SJohannes Berg 
3307a6dfbd04SJohannes Berg 	for (i = len - 1; i >= 0; i--) {
3308a6dfbd04SJohannes Berg 		if (pn1[i] > pn2[i])
3309a6dfbd04SJohannes Berg 			return 1;
3310a6dfbd04SJohannes Berg 		if (pn1[i] < pn2[i])
3311a6dfbd04SJohannes Berg 			return -1;
3312a6dfbd04SJohannes Berg 	}
3313a6dfbd04SJohannes Berg 
3314a6dfbd04SJohannes Berg 	return 0;
3315a6dfbd04SJohannes Berg }
3316a6dfbd04SJohannes Berg 
3317e705c121SKalle Valo static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
331885aeb58cSDavid Spinadel 				u32 sta_id,
331945c458b4SSara Sharon 				struct ieee80211_key_conf *key, bool mcast,
33204615fd15SEmmanuel Grumbach 				u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
33214883145aSEmmanuel Grumbach 				u8 key_offset, bool mfp)
3322e705c121SKalle Valo {
332345c458b4SSara Sharon 	union {
332445c458b4SSara Sharon 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
332545c458b4SSara Sharon 		struct iwl_mvm_add_sta_key_cmd cmd;
332645c458b4SSara Sharon 	} u = {};
3327e705c121SKalle Valo 	__le16 key_flags;
3328e705c121SKalle Valo 	int ret;
3329e705c121SKalle Valo 	u32 status;
3330e705c121SKalle Valo 	u16 keyidx;
333145c458b4SSara Sharon 	u64 pn = 0;
333245c458b4SSara Sharon 	int i, size;
333345c458b4SSara Sharon 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
333445c458b4SSara Sharon 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3335971cbe50SJohannes Berg 	int api_ver = iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA_KEY,
3336199d895fSJohannes Berg 					    new_api ? 2 : 1);
3337e705c121SKalle Valo 
333885aeb58cSDavid Spinadel 	if (sta_id == IWL_MVM_INVALID_STA)
333985aeb58cSDavid Spinadel 		return -EINVAL;
334085aeb58cSDavid Spinadel 
334145c458b4SSara Sharon 	keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
3342e705c121SKalle Valo 		 STA_KEY_FLG_KEYID_MSK;
3343e705c121SKalle Valo 	key_flags = cpu_to_le16(keyidx);
3344e705c121SKalle Valo 	key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3345e705c121SKalle Valo 
334645c458b4SSara Sharon 	switch (key->cipher) {
3347e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
3348e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
3349199d895fSJohannes Berg 		if (api_ver >= 2) {
335045c458b4SSara Sharon 			memcpy((void *)&u.cmd.tx_mic_key,
335145c458b4SSara Sharon 			       &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
335245c458b4SSara Sharon 			       IWL_MIC_KEY_SIZE);
335345c458b4SSara Sharon 
335445c458b4SSara Sharon 			memcpy((void *)&u.cmd.rx_mic_key,
335545c458b4SSara Sharon 			       &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
335645c458b4SSara Sharon 			       IWL_MIC_KEY_SIZE);
335745c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
335845c458b4SSara Sharon 
335945c458b4SSara Sharon 		} else {
336045c458b4SSara Sharon 			u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3361e705c121SKalle Valo 			for (i = 0; i < 5; i++)
336245c458b4SSara Sharon 				u.cmd_v1.tkip_rx_ttak[i] =
336345c458b4SSara Sharon 					cpu_to_le16(tkip_p1k[i]);
336445c458b4SSara Sharon 		}
336545c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3366e705c121SKalle Valo 		break;
3367e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
3368e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
336945c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3370199d895fSJohannes Berg 		if (api_ver >= 2)
337145c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
3372e705c121SKalle Valo 		break;
3373e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
3374e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
33755a2abdcaSGustavo A. R. Silva 		fallthrough;
3376e705c121SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
3377e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
337845c458b4SSara Sharon 		memcpy(u.cmd.common.key + 3, key->key, key->keylen);
3379e705c121SKalle Valo 		break;
33802a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP_256:
33812a53d166SAyala Beker 		key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
33825a2abdcaSGustavo A. R. Silva 		fallthrough;
33832a53d166SAyala Beker 	case WLAN_CIPHER_SUITE_GCMP:
33842a53d166SAyala Beker 		key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
338545c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3386199d895fSJohannes Berg 		if (api_ver >= 2)
338745c458b4SSara Sharon 			pn = atomic64_read(&key->tx_pn);
33882a53d166SAyala Beker 		break;
3389e705c121SKalle Valo 	default:
3390e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
339145c458b4SSara Sharon 		memcpy(u.cmd.common.key, key->key, key->keylen);
3392e705c121SKalle Valo 	}
3393e705c121SKalle Valo 
3394e705c121SKalle Valo 	if (mcast)
3395e705c121SKalle Valo 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
33964883145aSEmmanuel Grumbach 	if (mfp)
33974883145aSEmmanuel Grumbach 		key_flags |= cpu_to_le16(STA_KEY_MFP);
3398e705c121SKalle Valo 
339945c458b4SSara Sharon 	u.cmd.common.key_offset = key_offset;
340045c458b4SSara Sharon 	u.cmd.common.key_flags = key_flags;
340185aeb58cSDavid Spinadel 	u.cmd.common.sta_id = sta_id;
340245c458b4SSara Sharon 
3403a6dfbd04SJohannes Berg 	if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
3404a6dfbd04SJohannes Berg 		i = 0;
3405a6dfbd04SJohannes Berg 	else
3406a6dfbd04SJohannes Berg 		i = -1;
3407a6dfbd04SJohannes Berg 
3408a6dfbd04SJohannes Berg 	for (; i < IEEE80211_NUM_TIDS; i++) {
3409a6dfbd04SJohannes Berg 		struct ieee80211_key_seq seq = {};
3410a6dfbd04SJohannes Berg 		u8 _rx_pn[IEEE80211_MAX_PN_LEN] = {}, *rx_pn = _rx_pn;
3411a6dfbd04SJohannes Berg 		int rx_pn_len = 8;
3412199d895fSJohannes Berg 		/* there's a hole at 2/3 in FW format depending on version */
3413199d895fSJohannes Berg 		int hole = api_ver >= 3 ? 0 : 2;
3414a6dfbd04SJohannes Berg 
3415a6dfbd04SJohannes Berg 		ieee80211_get_key_rx_seq(key, i, &seq);
3416a6dfbd04SJohannes Berg 
3417a6dfbd04SJohannes Berg 		if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
3418a6dfbd04SJohannes Berg 			rx_pn[0] = seq.tkip.iv16;
3419a6dfbd04SJohannes Berg 			rx_pn[1] = seq.tkip.iv16 >> 8;
3420199d895fSJohannes Berg 			rx_pn[2 + hole] = seq.tkip.iv32;
3421199d895fSJohannes Berg 			rx_pn[3 + hole] = seq.tkip.iv32 >> 8;
3422199d895fSJohannes Berg 			rx_pn[4 + hole] = seq.tkip.iv32 >> 16;
3423199d895fSJohannes Berg 			rx_pn[5 + hole] = seq.tkip.iv32 >> 24;
3424a6dfbd04SJohannes Berg 		} else if (key_flags & cpu_to_le16(STA_KEY_FLG_EXT)) {
3425a6dfbd04SJohannes Berg 			rx_pn = seq.hw.seq;
3426a6dfbd04SJohannes Berg 			rx_pn_len = seq.hw.seq_len;
3427a6dfbd04SJohannes Berg 		} else {
3428a6dfbd04SJohannes Berg 			rx_pn[0] = seq.ccmp.pn[0];
3429a6dfbd04SJohannes Berg 			rx_pn[1] = seq.ccmp.pn[1];
3430199d895fSJohannes Berg 			rx_pn[2 + hole] = seq.ccmp.pn[2];
3431199d895fSJohannes Berg 			rx_pn[3 + hole] = seq.ccmp.pn[3];
3432199d895fSJohannes Berg 			rx_pn[4 + hole] = seq.ccmp.pn[4];
3433199d895fSJohannes Berg 			rx_pn[5 + hole] = seq.ccmp.pn[5];
3434a6dfbd04SJohannes Berg 		}
3435a6dfbd04SJohannes Berg 
3436a6dfbd04SJohannes Berg 		if (iwl_mvm_pn_cmp(rx_pn, (u8 *)&u.cmd.common.rx_secur_seq_cnt,
3437a6dfbd04SJohannes Berg 				   rx_pn_len) > 0)
3438a6dfbd04SJohannes Berg 			memcpy(&u.cmd.common.rx_secur_seq_cnt, rx_pn,
3439a6dfbd04SJohannes Berg 			       rx_pn_len);
3440a6dfbd04SJohannes Berg 	}
3441a6dfbd04SJohannes Berg 
3442199d895fSJohannes Berg 	if (api_ver >= 2) {
344345c458b4SSara Sharon 		u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
344445c458b4SSara Sharon 		size = sizeof(u.cmd);
344545c458b4SSara Sharon 	} else {
344645c458b4SSara Sharon 		size = sizeof(u.cmd_v1);
344745c458b4SSara Sharon 	}
3448e705c121SKalle Valo 
3449e705c121SKalle Valo 	status = ADD_STA_SUCCESS;
3450e705c121SKalle Valo 	if (cmd_flags & CMD_ASYNC)
345145c458b4SSara Sharon 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
345245c458b4SSara Sharon 					   &u.cmd);
3453e705c121SKalle Valo 	else
345445c458b4SSara Sharon 		ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
345545c458b4SSara Sharon 						  &u.cmd, &status);
3456e705c121SKalle Valo 
3457e705c121SKalle Valo 	switch (status) {
3458e705c121SKalle Valo 	case ADD_STA_SUCCESS:
3459e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3460e705c121SKalle Valo 		break;
3461e705c121SKalle Valo 	default:
3462e705c121SKalle Valo 		ret = -EIO;
3463e705c121SKalle Valo 		IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3464e705c121SKalle Valo 		break;
3465e705c121SKalle Valo 	}
3466e705c121SKalle Valo 
3467e705c121SKalle Valo 	return ret;
3468e705c121SKalle Valo }
3469e705c121SKalle Valo 
3470e705c121SKalle Valo static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3471e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
3472e705c121SKalle Valo 				 u8 sta_id, bool remove_key)
3473e705c121SKalle Valo {
3474e705c121SKalle Valo 	struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3475e705c121SKalle Valo 
3476e705c121SKalle Valo 	/* verify the key details match the required command's expectations */
34778e160ab8SAyala Beker 	if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3478b1fdc250SJohannes Berg 		    (keyconf->keyidx != 4 && keyconf->keyidx != 5 &&
3479b1fdc250SJohannes Berg 		     keyconf->keyidx != 6 && keyconf->keyidx != 7) ||
34808e160ab8SAyala Beker 		    (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
34818e160ab8SAyala Beker 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
34828e160ab8SAyala Beker 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
34838e160ab8SAyala Beker 		return -EINVAL;
34848e160ab8SAyala Beker 
34858e160ab8SAyala Beker 	if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
34868e160ab8SAyala Beker 		    keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3487e705c121SKalle Valo 		return -EINVAL;
3488e705c121SKalle Valo 
3489e705c121SKalle Valo 	igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3490e705c121SKalle Valo 	igtk_cmd.sta_id = cpu_to_le32(sta_id);
3491e705c121SKalle Valo 
3492e705c121SKalle Valo 	if (remove_key) {
3493197288d5SLuca Coelho 		/* This is a valid situation for IGTK */
3494197288d5SLuca Coelho 		if (sta_id == IWL_MVM_INVALID_STA)
3495197288d5SLuca Coelho 			return 0;
3496197288d5SLuca Coelho 
3497e705c121SKalle Valo 		igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3498e705c121SKalle Valo 	} else {
3499e705c121SKalle Valo 		struct ieee80211_key_seq seq;
3500e705c121SKalle Valo 		const u8 *pn;
3501e705c121SKalle Valo 
3502aa950524SAyala Beker 		switch (keyconf->cipher) {
3503aa950524SAyala Beker 		case WLAN_CIPHER_SUITE_AES_CMAC:
3504aa950524SAyala Beker 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3505aa950524SAyala Beker 			break;
35068e160ab8SAyala Beker 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
35078e160ab8SAyala Beker 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
35088e160ab8SAyala Beker 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
35098e160ab8SAyala Beker 			break;
3510aa950524SAyala Beker 		default:
3511aa950524SAyala Beker 			return -EINVAL;
3512aa950524SAyala Beker 		}
3513aa950524SAyala Beker 
35148e160ab8SAyala Beker 		memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
35158e160ab8SAyala Beker 		if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
35168e160ab8SAyala Beker 			igtk_cmd.ctrl_flags |=
35178e160ab8SAyala Beker 				cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3518e705c121SKalle Valo 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3519e705c121SKalle Valo 		pn = seq.aes_cmac.pn;
3520e705c121SKalle Valo 		igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3521e705c121SKalle Valo 						       ((u64) pn[4] << 8) |
3522e705c121SKalle Valo 						       ((u64) pn[3] << 16) |
3523e705c121SKalle Valo 						       ((u64) pn[2] << 24) |
3524e705c121SKalle Valo 						       ((u64) pn[1] << 32) |
3525e705c121SKalle Valo 						       ((u64) pn[0] << 40));
3526e705c121SKalle Valo 	}
3527e705c121SKalle Valo 
3528b1fdc250SJohannes Berg 	IWL_DEBUG_INFO(mvm, "%s %sIGTK (%d) for sta %u\n",
3529e705c121SKalle Valo 		       remove_key ? "removing" : "installing",
3530b1fdc250SJohannes Berg 		       keyconf->keyidx >= 6 ? "B" : "",
3531b1fdc250SJohannes Berg 		       keyconf->keyidx, igtk_cmd.sta_id);
3532e705c121SKalle Valo 
35338e160ab8SAyala Beker 	if (!iwl_mvm_has_new_rx_api(mvm)) {
35348e160ab8SAyala Beker 		struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
35358e160ab8SAyala Beker 			.ctrl_flags = igtk_cmd.ctrl_flags,
35368e160ab8SAyala Beker 			.key_id = igtk_cmd.key_id,
35378e160ab8SAyala Beker 			.sta_id = igtk_cmd.sta_id,
35388e160ab8SAyala Beker 			.receive_seq_cnt = igtk_cmd.receive_seq_cnt
35398e160ab8SAyala Beker 		};
35408e160ab8SAyala Beker 
35418e160ab8SAyala Beker 		memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
35428e160ab8SAyala Beker 		       ARRAY_SIZE(igtk_cmd_v1.igtk));
35438e160ab8SAyala Beker 		return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
35448e160ab8SAyala Beker 					    sizeof(igtk_cmd_v1), &igtk_cmd_v1);
35458e160ab8SAyala Beker 	}
3546e705c121SKalle Valo 	return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3547e705c121SKalle Valo 				    sizeof(igtk_cmd), &igtk_cmd);
3548e705c121SKalle Valo }
3549e705c121SKalle Valo 
3550e705c121SKalle Valo 
3551e705c121SKalle Valo static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3552e705c121SKalle Valo 				       struct ieee80211_vif *vif,
3553e705c121SKalle Valo 				       struct ieee80211_sta *sta)
3554e705c121SKalle Valo {
3555e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3556e705c121SKalle Valo 
3557e705c121SKalle Valo 	if (sta)
3558e705c121SKalle Valo 		return sta->addr;
3559e705c121SKalle Valo 
3560e705c121SKalle Valo 	if (vif->type == NL80211_IFTYPE_STATION &&
35610ae98812SSara Sharon 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3562e705c121SKalle Valo 		u8 sta_id = mvmvif->ap_sta_id;
3563e705c121SKalle Valo 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3564e705c121SKalle Valo 						lockdep_is_held(&mvm->mutex));
3565e705c121SKalle Valo 		return sta->addr;
3566e705c121SKalle Valo 	}
3567e705c121SKalle Valo 
3568e705c121SKalle Valo 
3569e705c121SKalle Valo 	return NULL;
3570e705c121SKalle Valo }
3571e705c121SKalle Valo 
3572e705c121SKalle Valo static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3573e705c121SKalle Valo 				 struct ieee80211_vif *vif,
3574e705c121SKalle Valo 				 struct ieee80211_sta *sta,
3575e705c121SKalle Valo 				 struct ieee80211_key_conf *keyconf,
35764615fd15SEmmanuel Grumbach 				 u8 key_offset,
3577e705c121SKalle Valo 				 bool mcast)
3578e705c121SKalle Valo {
3579e705c121SKalle Valo 	const u8 *addr;
3580e705c121SKalle Valo 	struct ieee80211_key_seq seq;
3581e705c121SKalle Valo 	u16 p1k[5];
358285aeb58cSDavid Spinadel 	u32 sta_id;
35834883145aSEmmanuel Grumbach 	bool mfp = false;
358485aeb58cSDavid Spinadel 
358585aeb58cSDavid Spinadel 	if (sta) {
358685aeb58cSDavid Spinadel 		struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
358785aeb58cSDavid Spinadel 
358885aeb58cSDavid Spinadel 		sta_id = mvm_sta->sta_id;
35894883145aSEmmanuel Grumbach 		mfp = sta->mfp;
359085aeb58cSDavid Spinadel 	} else if (vif->type == NL80211_IFTYPE_AP &&
359185aeb58cSDavid Spinadel 		   !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
359285aeb58cSDavid Spinadel 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
359385aeb58cSDavid Spinadel 
359485aeb58cSDavid Spinadel 		sta_id = mvmvif->mcast_sta.sta_id;
359585aeb58cSDavid Spinadel 	} else {
359685aeb58cSDavid Spinadel 		IWL_ERR(mvm, "Failed to find station id\n");
359785aeb58cSDavid Spinadel 		return -EINVAL;
359885aeb58cSDavid Spinadel 	}
3599e705c121SKalle Valo 
360095a62c33SJohannes Berg 	if (keyconf->cipher == WLAN_CIPHER_SUITE_TKIP) {
3601e705c121SKalle Valo 		addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3602e705c121SKalle Valo 		/* get phase 1 key from mac80211 */
3603e705c121SKalle Valo 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3604e705c121SKalle Valo 		ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
360595a62c33SJohannes Berg 
360695a62c33SJohannes Berg 		return iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
36074883145aSEmmanuel Grumbach 					    seq.tkip.iv32, p1k, 0, key_offset,
36084883145aSEmmanuel Grumbach 					    mfp);
3609e705c121SKalle Valo 	}
3610e705c121SKalle Valo 
361195a62c33SJohannes Berg 	return iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
361295a62c33SJohannes Berg 				    0, NULL, 0, key_offset, mfp);
3613e705c121SKalle Valo }
3614e705c121SKalle Valo 
3615e705c121SKalle Valo int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3616e705c121SKalle Valo 			struct ieee80211_vif *vif,
3617e705c121SKalle Valo 			struct ieee80211_sta *sta,
3618e705c121SKalle Valo 			struct ieee80211_key_conf *keyconf,
36194615fd15SEmmanuel Grumbach 			u8 key_offset)
3620e705c121SKalle Valo {
3621e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
36225f7a1847SJohannes Berg 	struct iwl_mvm_sta *mvm_sta;
362385aeb58cSDavid Spinadel 	u8 sta_id = IWL_MVM_INVALID_STA;
3624e705c121SKalle Valo 	int ret;
3625e705c121SKalle Valo 	static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3626e705c121SKalle Valo 
3627e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3628e705c121SKalle Valo 
362985aeb58cSDavid Spinadel 	if (vif->type != NL80211_IFTYPE_AP ||
363085aeb58cSDavid Spinadel 	    keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3631e705c121SKalle Valo 		/* Get the station id from the mvm local station table */
36325f7a1847SJohannes Berg 		mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
36335f7a1847SJohannes Berg 		if (!mvm_sta) {
36345f7a1847SJohannes Berg 			IWL_ERR(mvm, "Failed to find station\n");
3635e705c121SKalle Valo 			return -EINVAL;
3636e705c121SKalle Valo 		}
36375f7a1847SJohannes Berg 		sta_id = mvm_sta->sta_id;
3638e705c121SKalle Valo 
3639e705c121SKalle Valo 		/*
3640e705c121SKalle Valo 		 * It is possible that the 'sta' parameter is NULL, and thus
364185aeb58cSDavid Spinadel 		 * there is a need to retrieve the sta from the local station
364285aeb58cSDavid Spinadel 		 * table.
3643e705c121SKalle Valo 		 */
3644e705c121SKalle Valo 		if (!sta) {
364585aeb58cSDavid Spinadel 			sta = rcu_dereference_protected(
364685aeb58cSDavid Spinadel 				mvm->fw_id_to_mac_id[sta_id],
3647e705c121SKalle Valo 				lockdep_is_held(&mvm->mutex));
3648e705c121SKalle Valo 			if (IS_ERR_OR_NULL(sta)) {
3649e705c121SKalle Valo 				IWL_ERR(mvm, "Invalid station id\n");
3650e705c121SKalle Valo 				return -EINVAL;
3651e705c121SKalle Valo 			}
3652e705c121SKalle Valo 		}
3653e705c121SKalle Valo 
3654e705c121SKalle Valo 		if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3655e705c121SKalle Valo 			return -EINVAL;
3656e829b17cSBeni Lev 	} else {
3657e829b17cSBeni Lev 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3658e829b17cSBeni Lev 
3659e829b17cSBeni Lev 		sta_id = mvmvif->mcast_sta.sta_id;
3660e829b17cSBeni Lev 	}
3661e829b17cSBeni Lev 
3662e829b17cSBeni Lev 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3663e829b17cSBeni Lev 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3664e829b17cSBeni Lev 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3665e829b17cSBeni Lev 		ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3666e829b17cSBeni Lev 		goto end;
366785aeb58cSDavid Spinadel 	}
3668e705c121SKalle Valo 
36694615fd15SEmmanuel Grumbach 	/* If the key_offset is not pre-assigned, we need to find a
36704615fd15SEmmanuel Grumbach 	 * new offset to use.  In normal cases, the offset is not
36714615fd15SEmmanuel Grumbach 	 * pre-assigned, but during HW_RESTART we want to reuse the
36724615fd15SEmmanuel Grumbach 	 * same indices, so we pass them when this function is called.
36734615fd15SEmmanuel Grumbach 	 *
36744615fd15SEmmanuel Grumbach 	 * In D3 entry, we need to hardcoded the indices (because the
36754615fd15SEmmanuel Grumbach 	 * firmware hardcodes the PTK offset to 0).  In this case, we
36764615fd15SEmmanuel Grumbach 	 * need to make sure we don't overwrite the hw_key_idx in the
36774615fd15SEmmanuel Grumbach 	 * keyconf structure, because otherwise we cannot configure
36784615fd15SEmmanuel Grumbach 	 * the original ones back when resuming.
3679e705c121SKalle Valo 	 */
36804615fd15SEmmanuel Grumbach 	if (key_offset == STA_KEY_IDX_INVALID) {
36814615fd15SEmmanuel Grumbach 		key_offset  = iwl_mvm_set_fw_key_idx(mvm);
36824615fd15SEmmanuel Grumbach 		if (key_offset == STA_KEY_IDX_INVALID)
3683e705c121SKalle Valo 			return -ENOSPC;
36844615fd15SEmmanuel Grumbach 		keyconf->hw_key_idx = key_offset;
3685e705c121SKalle Valo 	}
3686e705c121SKalle Valo 
36874615fd15SEmmanuel Grumbach 	ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
36889c3deeb5SLuca Coelho 	if (ret)
3689e705c121SKalle Valo 		goto end;
3690e705c121SKalle Valo 
3691e705c121SKalle Valo 	/*
3692e705c121SKalle Valo 	 * For WEP, the same key is used for multicast and unicast. Upload it
3693e705c121SKalle Valo 	 * again, using the same key offset, and now pointing the other one
3694e705c121SKalle Valo 	 * to the same key slot (offset).
3695e705c121SKalle Valo 	 * If this fails, remove the original as well.
3696e705c121SKalle Valo 	 */
369785aeb58cSDavid Spinadel 	if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
369885aeb58cSDavid Spinadel 	     keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
369985aeb58cSDavid Spinadel 	    sta) {
37004615fd15SEmmanuel Grumbach 		ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
37014615fd15SEmmanuel Grumbach 					    key_offset, !mcast);
3702e705c121SKalle Valo 		if (ret) {
3703e705c121SKalle Valo 			__iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
37049c3deeb5SLuca Coelho 			goto end;
3705e705c121SKalle Valo 		}
3706e705c121SKalle Valo 	}
3707e705c121SKalle Valo 
37089c3deeb5SLuca Coelho 	__set_bit(key_offset, mvm->fw_key_table);
37099c3deeb5SLuca Coelho 
3710e705c121SKalle Valo end:
3711e705c121SKalle Valo 	IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3712e705c121SKalle Valo 		      keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3713e705c121SKalle Valo 		      sta ? sta->addr : zero_addr, ret);
3714e705c121SKalle Valo 	return ret;
3715e705c121SKalle Valo }
3716e705c121SKalle Valo 
3717e705c121SKalle Valo int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3718e705c121SKalle Valo 			   struct ieee80211_vif *vif,
3719e705c121SKalle Valo 			   struct ieee80211_sta *sta,
3720e705c121SKalle Valo 			   struct ieee80211_key_conf *keyconf)
3721e705c121SKalle Valo {
3722e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
37235f7a1847SJohannes Berg 	struct iwl_mvm_sta *mvm_sta;
37240ae98812SSara Sharon 	u8 sta_id = IWL_MVM_INVALID_STA;
3725e705c121SKalle Valo 	int ret, i;
3726e705c121SKalle Valo 
3727e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
3728e705c121SKalle Valo 
37295f7a1847SJohannes Berg 	/* Get the station from the mvm local station table */
37305f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
373171793b7dSLuca Coelho 	if (mvm_sta)
3732cd4d23c1SIlan Peer 		sta_id = mvm_sta->sta_id;
373385aeb58cSDavid Spinadel 	else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
373485aeb58cSDavid Spinadel 		sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
373585aeb58cSDavid Spinadel 
3736e705c121SKalle Valo 
3737e705c121SKalle Valo 	IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3738e705c121SKalle Valo 		      keyconf->keyidx, sta_id);
3739e705c121SKalle Valo 
3740197288d5SLuca Coelho 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
37418e160ab8SAyala Beker 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3742197288d5SLuca Coelho 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3743e705c121SKalle Valo 		return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3744e705c121SKalle Valo 
3745e705c121SKalle Valo 	if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3746e705c121SKalle Valo 		IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3747e705c121SKalle Valo 			keyconf->hw_key_idx);
3748e705c121SKalle Valo 		return -ENOENT;
3749e705c121SKalle Valo 	}
3750e705c121SKalle Valo 
3751e705c121SKalle Valo 	/* track which key was deleted last */
3752e705c121SKalle Valo 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3753e705c121SKalle Valo 		if (mvm->fw_key_deleted[i] < U8_MAX)
3754e705c121SKalle Valo 			mvm->fw_key_deleted[i]++;
3755e705c121SKalle Valo 	}
3756e705c121SKalle Valo 	mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3757e705c121SKalle Valo 
375885aeb58cSDavid Spinadel 	if (sta && !mvm_sta) {
3759e705c121SKalle Valo 		IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3760e705c121SKalle Valo 		return 0;
3761e705c121SKalle Valo 	}
3762e705c121SKalle Valo 
3763e705c121SKalle Valo 	ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3764e705c121SKalle Valo 	if (ret)
3765e705c121SKalle Valo 		return ret;
3766e705c121SKalle Valo 
3767e705c121SKalle Valo 	/* delete WEP key twice to get rid of (now useless) offset */
3768e705c121SKalle Valo 	if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3769e705c121SKalle Valo 	    keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3770e705c121SKalle Valo 		ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3771e705c121SKalle Valo 
3772e705c121SKalle Valo 	return ret;
3773e705c121SKalle Valo }
3774e705c121SKalle Valo 
3775e705c121SKalle Valo void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3776e705c121SKalle Valo 			     struct ieee80211_vif *vif,
3777e705c121SKalle Valo 			     struct ieee80211_key_conf *keyconf,
3778e705c121SKalle Valo 			     struct ieee80211_sta *sta, u32 iv32,
3779e705c121SKalle Valo 			     u16 *phase1key)
3780e705c121SKalle Valo {
3781e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta;
3782e705c121SKalle Valo 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
37834883145aSEmmanuel Grumbach 	bool mfp = sta ? sta->mfp : false;
3784e705c121SKalle Valo 
3785e705c121SKalle Valo 	rcu_read_lock();
3786e705c121SKalle Valo 
37875f7a1847SJohannes Berg 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
37885f7a1847SJohannes Berg 	if (WARN_ON_ONCE(!mvm_sta))
378912f17211SEmmanuel Grumbach 		goto unlock;
379085aeb58cSDavid Spinadel 	iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
37914883145aSEmmanuel Grumbach 			     iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx,
37924883145aSEmmanuel Grumbach 			     mfp);
379312f17211SEmmanuel Grumbach 
379412f17211SEmmanuel Grumbach  unlock:
3795e705c121SKalle Valo 	rcu_read_unlock();
3796e705c121SKalle Valo }
3797e705c121SKalle Valo 
3798e705c121SKalle Valo void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3799e705c121SKalle Valo 				struct ieee80211_sta *sta)
3800e705c121SKalle Valo {
3801e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3802e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3803e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3804e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3805e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_PS),
3806e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3807e705c121SKalle Valo 	};
3808e705c121SKalle Valo 	int ret;
3809e705c121SKalle Valo 
3810854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3811854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3812e705c121SKalle Valo 	if (ret)
3813e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3814e705c121SKalle Valo }
3815e705c121SKalle Valo 
3816e705c121SKalle Valo void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3817e705c121SKalle Valo 				       struct ieee80211_sta *sta,
3818e705c121SKalle Valo 				       enum ieee80211_frame_release_type reason,
3819e705c121SKalle Valo 				       u16 cnt, u16 tids, bool more_data,
38209a3fcf91SSara Sharon 				       bool single_sta_queue)
3821e705c121SKalle Valo {
3822e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3823e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3824e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3825e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3826e705c121SKalle Valo 		.modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3827e705c121SKalle Valo 		.sleep_tx_count = cpu_to_le16(cnt),
3828e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3829e705c121SKalle Valo 	};
3830e705c121SKalle Valo 	int tid, ret;
3831e705c121SKalle Valo 	unsigned long _tids = tids;
3832e705c121SKalle Valo 
3833e705c121SKalle Valo 	/* convert TIDs to ACs - we don't support TSPEC so that's OK
3834e705c121SKalle Valo 	 * Note that this field is reserved and unused by firmware not
3835e705c121SKalle Valo 	 * supporting GO uAPSD, so it's safe to always do this.
3836e705c121SKalle Valo 	 */
3837e705c121SKalle Valo 	for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3838e705c121SKalle Valo 		cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3839e705c121SKalle Valo 
38409a3fcf91SSara Sharon 	/* If we're releasing frames from aggregation or dqa queues then check
38419a3fcf91SSara Sharon 	 * if all the queues that we're releasing frames from, combined, have:
3842e705c121SKalle Valo 	 *  - more frames than the service period, in which case more_data
3843e705c121SKalle Valo 	 *    needs to be set
3844e705c121SKalle Valo 	 *  - fewer than 'cnt' frames, in which case we need to adjust the
3845e705c121SKalle Valo 	 *    firmware command (but do that unconditionally)
3846e705c121SKalle Valo 	 */
38479a3fcf91SSara Sharon 	if (single_sta_queue) {
3848e705c121SKalle Valo 		int remaining = cnt;
384936be0eb6SEmmanuel Grumbach 		int sleep_tx_count;
3850e705c121SKalle Valo 
3851e705c121SKalle Valo 		spin_lock_bh(&mvmsta->lock);
3852e705c121SKalle Valo 		for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3853e705c121SKalle Valo 			struct iwl_mvm_tid_data *tid_data;
3854e705c121SKalle Valo 			u16 n_queued;
3855e705c121SKalle Valo 
3856e705c121SKalle Valo 			tid_data = &mvmsta->tid_data[tid];
3857e705c121SKalle Valo 
3858dd32162dSLiad Kaufman 			n_queued = iwl_mvm_tid_queued(mvm, tid_data);
3859e705c121SKalle Valo 			if (n_queued > remaining) {
3860e705c121SKalle Valo 				more_data = true;
3861e705c121SKalle Valo 				remaining = 0;
3862e705c121SKalle Valo 				break;
3863e705c121SKalle Valo 			}
3864e705c121SKalle Valo 			remaining -= n_queued;
3865e705c121SKalle Valo 		}
386636be0eb6SEmmanuel Grumbach 		sleep_tx_count = cnt - remaining;
386736be0eb6SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
386836be0eb6SEmmanuel Grumbach 			mvmsta->sleep_tx_count = sleep_tx_count;
3869e705c121SKalle Valo 		spin_unlock_bh(&mvmsta->lock);
3870e705c121SKalle Valo 
387136be0eb6SEmmanuel Grumbach 		cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3872e705c121SKalle Valo 		if (WARN_ON(cnt - remaining == 0)) {
3873e705c121SKalle Valo 			ieee80211_sta_eosp(sta);
3874e705c121SKalle Valo 			return;
3875e705c121SKalle Valo 		}
3876e705c121SKalle Valo 	}
3877e705c121SKalle Valo 
3878e705c121SKalle Valo 	/* Note: this is ignored by firmware not supporting GO uAPSD */
3879e705c121SKalle Valo 	if (more_data)
3880ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
3881e705c121SKalle Valo 
3882e705c121SKalle Valo 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3883e705c121SKalle Valo 		mvmsta->next_status_eosp = true;
3884ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
3885e705c121SKalle Valo 	} else {
3886ced19f26SSara Sharon 		cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
3887e705c121SKalle Valo 	}
3888e705c121SKalle Valo 
3889156f92f2SEmmanuel Grumbach 	/* block the Tx queues until the FW updated the sleep Tx count */
3890156f92f2SEmmanuel Grumbach 	iwl_trans_block_txq_ptrs(mvm->trans, true);
3891156f92f2SEmmanuel Grumbach 
3892156f92f2SEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3893156f92f2SEmmanuel Grumbach 				   CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3894854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3895e705c121SKalle Valo 	if (ret)
3896e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3897e705c121SKalle Valo }
3898e705c121SKalle Valo 
3899e705c121SKalle Valo void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3900e705c121SKalle Valo 			   struct iwl_rx_cmd_buffer *rxb)
3901e705c121SKalle Valo {
3902e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3903e705c121SKalle Valo 	struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3904e705c121SKalle Valo 	struct ieee80211_sta *sta;
3905e705c121SKalle Valo 	u32 sta_id = le32_to_cpu(notif->sta_id);
3906e705c121SKalle Valo 
3907be9ae34eSNathan Errera 	if (WARN_ON_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations))
3908e705c121SKalle Valo 		return;
3909e705c121SKalle Valo 
3910e705c121SKalle Valo 	rcu_read_lock();
3911e705c121SKalle Valo 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3912e705c121SKalle Valo 	if (!IS_ERR_OR_NULL(sta))
3913e705c121SKalle Valo 		ieee80211_sta_eosp(sta);
3914e705c121SKalle Valo 	rcu_read_unlock();
3915e705c121SKalle Valo }
3916e705c121SKalle Valo 
3917e705c121SKalle Valo void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3918e705c121SKalle Valo 				   struct iwl_mvm_sta *mvmsta, bool disable)
3919e705c121SKalle Valo {
3920e705c121SKalle Valo 	struct iwl_mvm_add_sta_cmd cmd = {
3921e705c121SKalle Valo 		.add_modify = STA_MODE_MODIFY,
3922e705c121SKalle Valo 		.sta_id = mvmsta->sta_id,
3923e705c121SKalle Valo 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3924e705c121SKalle Valo 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3925e705c121SKalle Valo 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3926e705c121SKalle Valo 	};
3927e705c121SKalle Valo 	int ret;
3928e705c121SKalle Valo 
3929854c5705SSara Sharon 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3930854c5705SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3931e705c121SKalle Valo 	if (ret)
3932e705c121SKalle Valo 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3933e705c121SKalle Valo }
3934e705c121SKalle Valo 
3935e705c121SKalle Valo void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3936e705c121SKalle Valo 				      struct ieee80211_sta *sta,
3937e705c121SKalle Valo 				      bool disable)
3938e705c121SKalle Valo {
3939e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3940e705c121SKalle Valo 
3941e705c121SKalle Valo 	spin_lock_bh(&mvm_sta->lock);
3942e705c121SKalle Valo 
3943e705c121SKalle Valo 	if (mvm_sta->disable_tx == disable) {
3944e705c121SKalle Valo 		spin_unlock_bh(&mvm_sta->lock);
3945e705c121SKalle Valo 		return;
3946e705c121SKalle Valo 	}
3947e705c121SKalle Valo 
3948e705c121SKalle Valo 	mvm_sta->disable_tx = disable;
3949e705c121SKalle Valo 
3950b1c6cec0SNaftali Goldstein 	/*
3951b1c6cec0SNaftali Goldstein 	 * If sta PS state is handled by mac80211, tell it to start/stop
3952b1c6cec0SNaftali Goldstein 	 * queuing tx for this station.
3953b1c6cec0SNaftali Goldstein 	 */
3954b1c6cec0SNaftali Goldstein 	if (!ieee80211_hw_check(mvm->hw, AP_LINK_PS))
3955e705c121SKalle Valo 		ieee80211_sta_block_awake(mvm->hw, sta, disable);
3956e705c121SKalle Valo 
3957e705c121SKalle Valo 	iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3958e705c121SKalle Valo 
3959e705c121SKalle Valo 	spin_unlock_bh(&mvm_sta->lock);
3960e705c121SKalle Valo }
3961e705c121SKalle Valo 
3962ced19f26SSara Sharon static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
3963ced19f26SSara Sharon 					      struct iwl_mvm_vif *mvmvif,
3964ced19f26SSara Sharon 					      struct iwl_mvm_int_sta *sta,
3965ced19f26SSara Sharon 					      bool disable)
3966ced19f26SSara Sharon {
3967ced19f26SSara Sharon 	u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
3968ced19f26SSara Sharon 	struct iwl_mvm_add_sta_cmd cmd = {
3969ced19f26SSara Sharon 		.add_modify = STA_MODE_MODIFY,
3970ced19f26SSara Sharon 		.sta_id = sta->sta_id,
3971ced19f26SSara Sharon 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3972ced19f26SSara Sharon 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3973ced19f26SSara Sharon 		.mac_id_n_color = cpu_to_le32(id),
3974ced19f26SSara Sharon 	};
3975ced19f26SSara Sharon 	int ret;
3976ced19f26SSara Sharon 
3977e5d153ecSEmmanuel Grumbach 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3978ced19f26SSara Sharon 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3979ced19f26SSara Sharon 	if (ret)
3980ced19f26SSara Sharon 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3981ced19f26SSara Sharon }
3982ced19f26SSara Sharon 
3983e705c121SKalle Valo void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3984e705c121SKalle Valo 				       struct iwl_mvm_vif *mvmvif,
3985e705c121SKalle Valo 				       bool disable)
3986e705c121SKalle Valo {
3987e705c121SKalle Valo 	struct ieee80211_sta *sta;
3988e705c121SKalle Valo 	struct iwl_mvm_sta *mvm_sta;
3989e705c121SKalle Valo 	int i;
3990e705c121SKalle Valo 
3991e5d153ecSEmmanuel Grumbach 	rcu_read_lock();
3992e705c121SKalle Valo 
3993e705c121SKalle Valo 	/* Block/unblock all the stations of the given mvmvif */
3994be9ae34eSNathan Errera 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
3995e5d153ecSEmmanuel Grumbach 		sta = rcu_dereference(mvm->fw_id_to_mac_id[i]);
3996e705c121SKalle Valo 		if (IS_ERR_OR_NULL(sta))
3997e705c121SKalle Valo 			continue;
3998e705c121SKalle Valo 
3999e705c121SKalle Valo 		mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4000e705c121SKalle Valo 		if (mvm_sta->mac_id_n_color !=
4001e705c121SKalle Valo 		    FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
4002e705c121SKalle Valo 			continue;
4003e705c121SKalle Valo 
4004e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
4005e705c121SKalle Valo 	}
4006ced19f26SSara Sharon 
4007e5d153ecSEmmanuel Grumbach 	rcu_read_unlock();
4008e5d153ecSEmmanuel Grumbach 
4009ced19f26SSara Sharon 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
4010ced19f26SSara Sharon 		return;
4011ced19f26SSara Sharon 
4012ced19f26SSara Sharon 	/* Need to block/unblock also multicast station */
4013ced19f26SSara Sharon 	if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
4014ced19f26SSara Sharon 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
4015ced19f26SSara Sharon 						  &mvmvif->mcast_sta, disable);
4016ced19f26SSara Sharon 
4017ced19f26SSara Sharon 	/*
4018ced19f26SSara Sharon 	 * Only unblock the broadcast station (FW blocks it for immediate
4019ced19f26SSara Sharon 	 * quiet, not the driver)
4020ced19f26SSara Sharon 	 */
4021ced19f26SSara Sharon 	if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
4022ced19f26SSara Sharon 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
4023ced19f26SSara Sharon 						  &mvmvif->bcast_sta, disable);
4024e705c121SKalle Valo }
4025e705c121SKalle Valo 
4026e705c121SKalle Valo void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
4027e705c121SKalle Valo {
4028e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4029e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta;
4030e705c121SKalle Valo 
4031e705c121SKalle Valo 	rcu_read_lock();
4032e705c121SKalle Valo 
4033e705c121SKalle Valo 	mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
4034e705c121SKalle Valo 
4035ad12b231SNathan Errera 	if (mvmsta)
4036e705c121SKalle Valo 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
4037e705c121SKalle Valo 
4038e705c121SKalle Valo 	rcu_read_unlock();
4039e705c121SKalle Valo }
4040dd32162dSLiad Kaufman 
4041dd32162dSLiad Kaufman u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
4042dd32162dSLiad Kaufman {
4043dd32162dSLiad Kaufman 	u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
4044dd32162dSLiad Kaufman 
4045dd32162dSLiad Kaufman 	/*
40462f7a3863SLuca Coelho 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
4047dd32162dSLiad Kaufman 	 * to align the wrap around of ssn so we compare relevant values.
4048dd32162dSLiad Kaufman 	 */
4049286ca8ebSLuca Coelho 	if (mvm->trans->trans_cfg->gen2)
4050dd32162dSLiad Kaufman 		sn &= 0xff;
4051dd32162dSLiad Kaufman 
4052dd32162dSLiad Kaufman 	return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
4053dd32162dSLiad Kaufman }
4054be82ecd3SAvraham Stern 
4055be82ecd3SAvraham Stern int iwl_mvm_add_pasn_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
4056be82ecd3SAvraham Stern 			 struct iwl_mvm_int_sta *sta, u8 *addr, u32 cipher,
4057be82ecd3SAvraham Stern 			 u8 *key, u32 key_len)
4058be82ecd3SAvraham Stern {
4059be82ecd3SAvraham Stern 	int ret;
4060be82ecd3SAvraham Stern 	u16 queue;
4061be82ecd3SAvraham Stern 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4062be82ecd3SAvraham Stern 	struct ieee80211_key_conf *keyconf;
4063be82ecd3SAvraham Stern 
4064be82ecd3SAvraham Stern 	ret = iwl_mvm_allocate_int_sta(mvm, sta, 0,
4065be82ecd3SAvraham Stern 				       NL80211_IFTYPE_UNSPECIFIED,
4066be82ecd3SAvraham Stern 				       IWL_STA_LINK);
4067be82ecd3SAvraham Stern 	if (ret)
4068be82ecd3SAvraham Stern 		return ret;
4069be82ecd3SAvraham Stern 
4070be82ecd3SAvraham Stern 	ret = iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
4071be82ecd3SAvraham Stern 					     addr, sta, &queue,
4072be82ecd3SAvraham Stern 					     IWL_MVM_TX_FIFO_BE);
4073be82ecd3SAvraham Stern 	if (ret)
4074be82ecd3SAvraham Stern 		goto out;
4075be82ecd3SAvraham Stern 
4076be82ecd3SAvraham Stern 	keyconf = kzalloc(sizeof(*keyconf) + key_len, GFP_KERNEL);
4077be82ecd3SAvraham Stern 	if (!keyconf) {
4078be82ecd3SAvraham Stern 		ret = -ENOBUFS;
4079be82ecd3SAvraham Stern 		goto out;
4080be82ecd3SAvraham Stern 	}
4081be82ecd3SAvraham Stern 
4082be82ecd3SAvraham Stern 	keyconf->cipher = cipher;
4083be82ecd3SAvraham Stern 	memcpy(keyconf->key, key, key_len);
4084be82ecd3SAvraham Stern 	keyconf->keylen = key_len;
4085be82ecd3SAvraham Stern 
4086be82ecd3SAvraham Stern 	ret = iwl_mvm_send_sta_key(mvm, sta->sta_id, keyconf, false,
4087be82ecd3SAvraham Stern 				   0, NULL, 0, 0, true);
4088be82ecd3SAvraham Stern 	kfree(keyconf);
4089be82ecd3SAvraham Stern 	return 0;
4090be82ecd3SAvraham Stern out:
4091be82ecd3SAvraham Stern 	iwl_mvm_dealloc_int_sta(mvm, sta);
4092be82ecd3SAvraham Stern 	return ret;
4093be82ecd3SAvraham Stern }
4094ad12b231SNathan Errera 
4095ad12b231SNathan Errera void iwl_mvm_cancel_channel_switch(struct iwl_mvm *mvm,
4096ad12b231SNathan Errera 				   struct ieee80211_vif *vif,
4097ad12b231SNathan Errera 				   u32 mac_id)
4098ad12b231SNathan Errera {
4099ad12b231SNathan Errera 	struct iwl_cancel_channel_switch_cmd cancel_channel_switch_cmd = {
4100ad12b231SNathan Errera 		.mac_id = cpu_to_le32(mac_id),
4101ad12b231SNathan Errera 	};
4102ad12b231SNathan Errera 	int ret;
4103ad12b231SNathan Errera 
4104ad12b231SNathan Errera 	ret = iwl_mvm_send_cmd_pdu(mvm,
4105f0c86427SJohannes Berg 				   WIDE_ID(MAC_CONF_GROUP, CANCEL_CHANNEL_SWITCH_CMD),
4106ad12b231SNathan Errera 				   CMD_ASYNC,
4107ad12b231SNathan Errera 				   sizeof(cancel_channel_switch_cmd),
4108ad12b231SNathan Errera 				   &cancel_channel_switch_cmd);
4109ad12b231SNathan Errera 	if (ret)
4110ad12b231SNathan Errera 		IWL_ERR(mvm, "Failed to cancel the channel switch\n");
4111ad12b231SNathan Errera }
4112