xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/sta.c (revision 5c0c0c189bc74c599cb6ed4a7ad8651c75994ecc)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2015, 2018-2026 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  * Copyright (C) 2016-2017 Intel Deutschland GmbH
6  */
7 #include <net/mac80211.h>
8 
9 #include "mvm.h"
10 #include "sta.h"
11 #include "rs.h"
12 
13 /*
14  * New version of ADD_STA_sta command added new fields at the end of the
15  * structure, so sending the size of the relevant API's structure is enough to
16  * support both API versions.
17  */
18 static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
19 {
20 	if (iwl_mvm_has_new_rx_api(mvm) ||
21 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
22 		return sizeof(struct iwl_mvm_add_sta_cmd);
23 	else
24 		return sizeof(struct iwl_mvm_add_sta_cmd_v7);
25 }
26 
27 int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm, enum nl80211_iftype iftype)
28 {
29 	int sta_id;
30 	u32 reserved_ids = 0;
31 
32 	BUILD_BUG_ON(IWL_STATION_COUNT_MAX > 32);
33 	WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
34 
35 	lockdep_assert_held(&mvm->mutex);
36 
37 	/* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
38 	if (iftype != NL80211_IFTYPE_STATION)
39 		reserved_ids = BIT(0);
40 
41 	/* Don't take rcu_read_lock() since we are protected by mvm->mutex */
42 	for (sta_id = 0; sta_id < mvm->fw->ucode_capa.num_stations; sta_id++) {
43 		if (BIT(sta_id) & reserved_ids)
44 			continue;
45 
46 		if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
47 					       lockdep_is_held(&mvm->mutex)))
48 			return sta_id;
49 	}
50 	return IWL_INVALID_STA;
51 }
52 
53 /* Calculate the ampdu density and max size */
54 u32 iwl_mvm_get_sta_ampdu_dens(struct ieee80211_link_sta *link_sta,
55 			       struct ieee80211_bss_conf *link_conf,
56 			       u32 *_agg_size)
57 {
58 	u32 agg_size = 0, mpdu_dens = 0;
59 
60 	if (WARN_ON(!link_sta))
61 		return 0;
62 
63 	/* Note that we always use only legacy & highest supported PPDUs, so
64 	 * of Draft P802.11be D.30 Table 10-12a--Fields used for calculating
65 	 * the maximum A-MPDU size of various PPDU types in different bands,
66 	 * we only need to worry about the highest supported PPDU type here.
67 	 */
68 
69 	if (link_sta->ht_cap.ht_supported) {
70 		agg_size = link_sta->ht_cap.ampdu_factor;
71 		mpdu_dens = link_sta->ht_cap.ampdu_density;
72 	}
73 
74 	if (link_conf->chanreq.oper.chan->band == NL80211_BAND_6GHZ) {
75 		/* overwrite HT values on 6 GHz */
76 		mpdu_dens = le16_get_bits(link_sta->he_6ghz_capa.capa,
77 					  IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
78 		agg_size = le16_get_bits(link_sta->he_6ghz_capa.capa,
79 					 IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
80 	} else if (link_sta->vht_cap.vht_supported) {
81 		/* if VHT supported overwrite HT value */
82 		agg_size = u32_get_bits(link_sta->vht_cap.cap,
83 					IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
84 	}
85 
86 	/* D6.0 10.12.2 A-MPDU length limit rules
87 	 * A STA indicates the maximum length of the A-MPDU preEOF padding
88 	 * that it can receive in an HE PPDU in the Maximum A-MPDU Length
89 	 * Exponent field in its HT Capabilities, VHT Capabilities,
90 	 * and HE 6 GHz Band Capabilities elements (if present) and the
91 	 * Maximum AMPDU Length Exponent Extension field in its HE
92 	 * Capabilities element
93 	 */
94 	if (link_sta->he_cap.has_he)
95 		agg_size +=
96 			u8_get_bits(link_sta->he_cap.he_cap_elem.mac_cap_info[3],
97 				    IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
98 
99 	if (link_sta->eht_cap.has_eht)
100 		agg_size += u8_get_bits(link_sta->eht_cap.eht_cap_elem.mac_cap_info[1],
101 					IEEE80211_EHT_MAC_CAP1_MAX_AMPDU_LEN_MASK);
102 
103 	/* Limit to max A-MPDU supported by FW */
104 	agg_size = min_t(u32, agg_size,
105 			 STA_FLG_MAX_AGG_SIZE_4M >> STA_FLG_MAX_AGG_SIZE_SHIFT);
106 
107 	*_agg_size = agg_size;
108 	return mpdu_dens;
109 }
110 
111 u8 iwl_mvm_get_sta_uapsd_acs(struct ieee80211_sta *sta)
112 {
113 	u8 uapsd_acs = 0;
114 
115 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
116 		uapsd_acs |= BIT(AC_BK);
117 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
118 		uapsd_acs |= BIT(AC_BE);
119 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
120 		uapsd_acs |= BIT(AC_VI);
121 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
122 		uapsd_acs |= BIT(AC_VO);
123 
124 	return uapsd_acs | uapsd_acs << 4;
125 }
126 
127 /* send station add/update command to firmware */
128 int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
129 			   bool update, unsigned int flags)
130 {
131 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
132 	struct iwl_mvm_add_sta_cmd add_sta_cmd = {
133 		.sta_id = mvm_sta->deflink.sta_id,
134 		.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
135 		.add_modify = update ? 1 : 0,
136 		.station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
137 						 STA_FLG_MIMO_EN_MSK |
138 						 STA_FLG_RTS_MIMO_PROT),
139 		.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
140 	};
141 	int ret;
142 	u32 status;
143 	u32 agg_size = 0, mpdu_dens = 0;
144 
145 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
146 		add_sta_cmd.station_type = mvm_sta->sta_type;
147 
148 	if (!update || (flags & STA_MODIFY_QUEUES)) {
149 		memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
150 
151 		if (!iwl_mvm_has_new_tx_api(mvm)) {
152 			add_sta_cmd.tfd_queue_msk =
153 				cpu_to_le32(mvm_sta->tfd_queue_msk);
154 
155 			if (flags & STA_MODIFY_QUEUES)
156 				add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
157 		} else {
158 			WARN_ON(flags & STA_MODIFY_QUEUES);
159 		}
160 	}
161 
162 	switch (sta->deflink.bandwidth) {
163 	case IEEE80211_STA_RX_BW_320:
164 	case IEEE80211_STA_RX_BW_160:
165 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
166 		fallthrough;
167 	case IEEE80211_STA_RX_BW_80:
168 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
169 		fallthrough;
170 	case IEEE80211_STA_RX_BW_40:
171 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
172 		fallthrough;
173 	case IEEE80211_STA_RX_BW_20:
174 		if (sta->deflink.ht_cap.ht_supported)
175 			add_sta_cmd.station_flags |=
176 				cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
177 		break;
178 	}
179 
180 	switch (sta->deflink.rx_nss) {
181 	case 1:
182 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
183 		break;
184 	case 2:
185 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
186 		break;
187 	case 3 ... 8:
188 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
189 		break;
190 	}
191 
192 	switch (sta->deflink.smps_mode) {
193 	case IEEE80211_SMPS_AUTOMATIC:
194 	case IEEE80211_SMPS_NUM_MODES:
195 		WARN_ON(1);
196 		break;
197 	case IEEE80211_SMPS_STATIC:
198 		/* override NSS */
199 		add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
200 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
201 		break;
202 	case IEEE80211_SMPS_DYNAMIC:
203 		add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
204 		break;
205 	case IEEE80211_SMPS_OFF:
206 		/* nothing */
207 		break;
208 	}
209 
210 	if (sta->deflink.ht_cap.ht_supported ||
211 	    mvm_sta->vif->bss_conf.chanreq.oper.chan->band == NL80211_BAND_6GHZ)
212 		add_sta_cmd.station_flags_msk |=
213 			cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
214 				    STA_FLG_AGG_MPDU_DENS_MSK);
215 
216 	mpdu_dens = iwl_mvm_get_sta_ampdu_dens(&sta->deflink,
217 					       &mvm_sta->vif->bss_conf,
218 					       &agg_size);
219 	add_sta_cmd.station_flags |=
220 		cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
221 	add_sta_cmd.station_flags |=
222 		cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
223 
224 	if (mvm_sta->sta_state >= IEEE80211_STA_ASSOC)
225 		add_sta_cmd.assoc_id = cpu_to_le16(sta->aid);
226 
227 	if (sta->wme) {
228 		add_sta_cmd.modify_mask |= STA_MODIFY_UAPSD_ACS;
229 		add_sta_cmd.uapsd_acs = iwl_mvm_get_sta_uapsd_acs(sta);
230 		add_sta_cmd.sp_length = sta->max_sp ? sta->max_sp * 2 : 128;
231 	}
232 
233 	status = ADD_STA_SUCCESS;
234 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
235 					  iwl_mvm_add_sta_cmd_size(mvm),
236 					  &add_sta_cmd, &status);
237 	if (ret)
238 		return ret;
239 
240 	switch (status & IWL_ADD_STA_STATUS_MASK) {
241 	case ADD_STA_SUCCESS:
242 		IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
243 		break;
244 	default:
245 		ret = -EIO;
246 		IWL_ERR(mvm, "ADD_STA failed\n");
247 		break;
248 	}
249 
250 	return ret;
251 }
252 
253 static void iwl_mvm_rx_agg_session_expired(struct timer_list *t)
254 {
255 	struct iwl_mvm_baid_data *data =
256 		timer_container_of(data, t, session_timer);
257 	struct iwl_mvm_baid_data __rcu **rcu_ptr = data->rcu_ptr;
258 	struct iwl_mvm_baid_data *ba_data;
259 	struct ieee80211_sta *sta;
260 	struct iwl_mvm_sta *mvm_sta;
261 	unsigned long timeout;
262 	unsigned int sta_id;
263 
264 	rcu_read_lock();
265 
266 	ba_data = rcu_dereference(*rcu_ptr);
267 
268 	if (WARN_ON(!ba_data))
269 		goto unlock;
270 
271 	if (!ba_data->timeout)
272 		goto unlock;
273 
274 	timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
275 	if (time_is_after_jiffies(timeout)) {
276 		mod_timer(&ba_data->session_timer, timeout);
277 		goto unlock;
278 	}
279 
280 	/* Timer expired */
281 	sta_id = ffs(ba_data->sta_mask) - 1; /* don't care which one */
282 	sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[sta_id]);
283 
284 	/*
285 	 * sta should be valid unless the following happens:
286 	 * The firmware asserts which triggers a reconfig flow, but
287 	 * the reconfig fails before we set the pointer to sta into
288 	 * the fw_id_to_mac_id pointer table. Mac80211 can't stop
289 	 * A-MDPU and hence the timer continues to run. Then, the
290 	 * timer expires and sta is NULL.
291 	 */
292 	if (IS_ERR_OR_NULL(sta))
293 		goto unlock;
294 
295 	mvm_sta = iwl_mvm_sta_from_mac80211(sta);
296 	ieee80211_rx_ba_timer_expired(mvm_sta->vif,
297 				      sta->addr, ba_data->tid);
298 unlock:
299 	rcu_read_unlock();
300 }
301 
302 /* Disable aggregations for a bitmap of TIDs for a given station */
303 static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
304 					unsigned long disable_agg_tids,
305 					bool remove_queue)
306 {
307 	struct iwl_mvm_add_sta_cmd cmd = {};
308 	struct ieee80211_sta *sta;
309 	struct iwl_mvm_sta *mvmsta;
310 	u32 status;
311 	u8 sta_id;
312 
313 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
314 		return -EINVAL;
315 
316 	sta_id = mvm->queue_info[queue].ra_sta_id;
317 
318 	rcu_read_lock();
319 
320 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
321 
322 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
323 		rcu_read_unlock();
324 		return -EINVAL;
325 	}
326 
327 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
328 
329 	mvmsta->tid_disable_agg |= disable_agg_tids;
330 
331 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
332 	cmd.sta_id = mvmsta->deflink.sta_id;
333 	cmd.add_modify = STA_MODE_MODIFY;
334 	cmd.modify_mask = STA_MODIFY_QUEUES;
335 	if (disable_agg_tids)
336 		cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
337 	if (remove_queue)
338 		cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
339 	cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
340 	cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
341 
342 	rcu_read_unlock();
343 
344 	/* Notify FW of queue removal from the STA queues */
345 	status = ADD_STA_SUCCESS;
346 	return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
347 					   iwl_mvm_add_sta_cmd_size(mvm),
348 					   &cmd, &status);
349 }
350 
351 static int iwl_mvm_disable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
352 			       int sta_id, u16 *queueptr, u8 tid)
353 {
354 	int queue = *queueptr;
355 	struct iwl_scd_txq_cfg_cmd cmd = {
356 		.scd_queue = queue,
357 		.action = SCD_CFG_DISABLE_QUEUE,
358 	};
359 	int ret;
360 
361 	lockdep_assert_held(&mvm->mutex);
362 
363 	if (iwl_mvm_has_new_tx_api(mvm)) {
364 		if (mvm->sta_remove_requires_queue_remove) {
365 			u32 cmd_id = WIDE_ID(DATA_PATH_GROUP,
366 					     SCD_QUEUE_CONFIG_CMD);
367 			struct iwl_scd_queue_cfg_cmd remove_cmd = {
368 				.operation = cpu_to_le32(IWL_SCD_QUEUE_REMOVE),
369 				.u.remove.sta_mask = cpu_to_le32(BIT(sta_id)),
370 			};
371 
372 			if (tid == IWL_MAX_TID_COUNT)
373 				tid = IWL_MGMT_TID;
374 
375 			remove_cmd.u.remove.tid = cpu_to_le32(tid);
376 
377 			ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0,
378 						   sizeof(remove_cmd),
379 						   &remove_cmd);
380 		} else {
381 			ret = 0;
382 		}
383 
384 		iwl_trans_txq_free(mvm->trans, queue);
385 		*queueptr = IWL_MVM_INVALID_QUEUE;
386 
387 		return ret;
388 	}
389 
390 	if (WARN_ON(mvm->queue_info[queue].tid_bitmap == 0))
391 		return 0;
392 
393 	mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
394 
395 	cmd.action = mvm->queue_info[queue].tid_bitmap ?
396 		SCD_CFG_ENABLE_QUEUE : SCD_CFG_DISABLE_QUEUE;
397 	if (cmd.action == SCD_CFG_DISABLE_QUEUE)
398 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_FREE;
399 
400 	IWL_DEBUG_TX_QUEUES(mvm,
401 			    "Disabling TXQ #%d tids=0x%x\n",
402 			    queue,
403 			    mvm->queue_info[queue].tid_bitmap);
404 
405 	/* If the queue is still enabled - nothing left to do in this func */
406 	if (cmd.action == SCD_CFG_ENABLE_QUEUE)
407 		return 0;
408 
409 	cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
410 	cmd.tid = mvm->queue_info[queue].txq_tid;
411 
412 	/* Make sure queue info is correct even though we overwrite it */
413 	WARN(mvm->queue_info[queue].tid_bitmap,
414 	     "TXQ #%d info out-of-sync - tids=0x%x\n",
415 	     queue, mvm->queue_info[queue].tid_bitmap);
416 
417 	/* If we are here - the queue is freed and we can zero out these vals */
418 	mvm->queue_info[queue].tid_bitmap = 0;
419 
420 	if (sta) {
421 		struct iwl_mvm_txq *mvmtxq =
422 			iwl_mvm_txq_from_tid(sta, tid);
423 
424 		spin_lock_bh(&mvm->add_stream_lock);
425 		list_del_init(&mvmtxq->list);
426 		clear_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state);
427 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
428 		spin_unlock_bh(&mvm->add_stream_lock);
429 	}
430 
431 	/* Regardless if this is a reserved TXQ for a STA - mark it as false */
432 	mvm->queue_info[queue].reserved = false;
433 
434 	iwl_trans_txq_disable(mvm->trans, queue, false);
435 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0,
436 				   sizeof(struct iwl_scd_txq_cfg_cmd), &cmd);
437 
438 	if (ret)
439 		IWL_ERR(mvm, "Failed to disable queue %d (ret=%d)\n",
440 			queue, ret);
441 	return ret;
442 }
443 
444 static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
445 {
446 	struct ieee80211_sta *sta;
447 	struct iwl_mvm_sta *mvmsta;
448 	unsigned long tid_bitmap;
449 	unsigned long agg_tids = 0;
450 	u8 sta_id;
451 	int tid;
452 
453 	lockdep_assert_held(&mvm->mutex);
454 
455 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
456 		return -EINVAL;
457 
458 	sta_id = mvm->queue_info[queue].ra_sta_id;
459 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
460 
461 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
462 					lockdep_is_held(&mvm->mutex));
463 
464 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
465 		return -EINVAL;
466 
467 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
468 
469 	spin_lock_bh(&mvmsta->lock);
470 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
471 		if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
472 			agg_tids |= BIT(tid);
473 	}
474 	spin_unlock_bh(&mvmsta->lock);
475 
476 	return agg_tids;
477 }
478 
479 /*
480  * Remove a queue from a station's resources.
481  * Note that this only marks as free. It DOESN'T delete a BA agreement, and
482  * doesn't disable the queue
483  */
484 static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
485 {
486 	struct ieee80211_sta *sta;
487 	struct iwl_mvm_sta *mvmsta;
488 	unsigned long tid_bitmap;
489 	unsigned long disable_agg_tids = 0;
490 	u8 sta_id;
491 	int tid;
492 
493 	lockdep_assert_held(&mvm->mutex);
494 
495 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
496 		return -EINVAL;
497 
498 	sta_id = mvm->queue_info[queue].ra_sta_id;
499 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
500 
501 	rcu_read_lock();
502 
503 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
504 
505 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
506 		rcu_read_unlock();
507 		return 0;
508 	}
509 
510 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
511 
512 	spin_lock_bh(&mvmsta->lock);
513 	/* Unmap MAC queues and TIDs from this queue */
514 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
515 		struct iwl_mvm_txq *mvmtxq =
516 			iwl_mvm_txq_from_tid(sta, tid);
517 
518 		if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
519 			disable_agg_tids |= BIT(tid);
520 		mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
521 
522 		spin_lock_bh(&mvm->add_stream_lock);
523 		list_del_init(&mvmtxq->list);
524 		clear_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state);
525 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
526 		spin_unlock_bh(&mvm->add_stream_lock);
527 	}
528 
529 	mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
530 	spin_unlock_bh(&mvmsta->lock);
531 
532 	rcu_read_unlock();
533 
534 	/*
535 	 * The TX path may have been using this TXQ_ID from the tid_data,
536 	 * so make sure it's no longer running so that we can safely reuse
537 	 * this TXQ later. We've set all the TIDs to IWL_MVM_INVALID_QUEUE
538 	 * above, but nothing guarantees we've stopped using them. Thus,
539 	 * without this, we could get to iwl_mvm_disable_txq() and remove
540 	 * the queue while still sending frames to it.
541 	 */
542 	synchronize_net();
543 
544 	return disable_agg_tids;
545 }
546 
547 static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
548 				       struct ieee80211_sta *old_sta,
549 				       u8 new_sta_id)
550 {
551 	struct iwl_mvm_sta *mvmsta;
552 	u8 sta_id, tid;
553 	unsigned long disable_agg_tids = 0;
554 	bool same_sta;
555 	u16 queue_tmp = queue;
556 	int ret;
557 
558 	lockdep_assert_held(&mvm->mutex);
559 
560 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
561 		return -EINVAL;
562 
563 	sta_id = mvm->queue_info[queue].ra_sta_id;
564 	tid = mvm->queue_info[queue].txq_tid;
565 
566 	same_sta = sta_id == new_sta_id;
567 
568 	mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
569 	if (WARN_ON(!mvmsta))
570 		return -EINVAL;
571 
572 	disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
573 	/* Disable the queue */
574 	if (disable_agg_tids)
575 		iwl_mvm_invalidate_sta_queue(mvm, queue,
576 					     disable_agg_tids, false);
577 
578 	ret = iwl_mvm_disable_txq(mvm, old_sta, sta_id, &queue_tmp, tid);
579 	if (ret) {
580 		IWL_ERR(mvm,
581 			"Failed to free inactive queue %d (ret=%d)\n",
582 			queue, ret);
583 
584 		return ret;
585 	}
586 
587 	/* If TXQ is allocated to another STA, update removal in FW */
588 	if (!same_sta)
589 		iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
590 
591 	return 0;
592 }
593 
594 static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
595 				    unsigned long tfd_queue_mask, u8 ac)
596 {
597 	int queue = 0;
598 	u8 ac_to_queue[IEEE80211_NUM_ACS];
599 	int i;
600 
601 	/*
602 	 * This protects us against grabbing a queue that's being reconfigured
603 	 * by the inactivity checker.
604 	 */
605 	lockdep_assert_held(&mvm->mutex);
606 
607 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
608 		return -EINVAL;
609 
610 	memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
611 
612 	/* See what ACs the existing queues for this STA have */
613 	for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
614 		/* Only DATA queues can be shared */
615 		if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
616 		    i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
617 			continue;
618 
619 		ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
620 	}
621 
622 	/*
623 	 * The queue to share is chosen only from DATA queues as follows (in
624 	 * descending priority):
625 	 * 1. An AC_BE queue
626 	 * 2. Same AC queue
627 	 * 3. Highest AC queue that is lower than new AC
628 	 * 4. Any existing AC (there always is at least 1 DATA queue)
629 	 */
630 
631 	/* Priority 1: An AC_BE queue */
632 	if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
633 		queue = ac_to_queue[IEEE80211_AC_BE];
634 	/* Priority 2: Same AC queue */
635 	else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
636 		queue = ac_to_queue[ac];
637 	/* Priority 3a: If new AC is VO and VI exists - use VI */
638 	else if (ac == IEEE80211_AC_VO &&
639 		 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
640 		queue = ac_to_queue[IEEE80211_AC_VI];
641 	/* Priority 3b: No BE so only AC less than the new one is BK */
642 	else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
643 		queue = ac_to_queue[IEEE80211_AC_BK];
644 	/* Priority 4a: No BE nor BK - use VI if exists */
645 	else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
646 		queue = ac_to_queue[IEEE80211_AC_VI];
647 	/* Priority 4b: No BE, BK nor VI - use VO if exists */
648 	else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
649 		queue = ac_to_queue[IEEE80211_AC_VO];
650 
651 	/* Make sure queue found (or not) is legal */
652 	if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
653 	    !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
654 	    (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
655 		IWL_ERR(mvm, "No DATA queues available to share\n");
656 		return -ENOSPC;
657 	}
658 
659 	return queue;
660 }
661 
662 /* Re-configure the SCD for a queue that has already been configured */
663 static int iwl_mvm_reconfig_scd(struct iwl_mvm *mvm, int queue, int fifo,
664 				int sta_id, int tid, int frame_limit, u16 ssn)
665 {
666 	struct iwl_scd_txq_cfg_cmd cmd = {
667 		.scd_queue = queue,
668 		.action = SCD_CFG_ENABLE_QUEUE,
669 		.window = frame_limit,
670 		.sta_id = sta_id,
671 		.ssn = cpu_to_le16(ssn),
672 		.tx_fifo = fifo,
673 		.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
674 			      queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE),
675 		.tid = tid,
676 	};
677 	int ret;
678 
679 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
680 		return -EINVAL;
681 
682 	if (WARN(mvm->queue_info[queue].tid_bitmap == 0,
683 		 "Trying to reconfig unallocated queue %d\n", queue))
684 		return -ENXIO;
685 
686 	IWL_DEBUG_TX_QUEUES(mvm, "Reconfig SCD for TXQ #%d\n", queue);
687 
688 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
689 	WARN_ONCE(ret, "Failed to re-configure queue %d on FIFO %d, ret=%d\n",
690 		  queue, fifo, ret);
691 
692 	return ret;
693 }
694 
695 /*
696  * If a given queue has a higher AC than the TID stream that is being compared
697  * to, the queue needs to be redirected to the lower AC. This function does that
698  * in such a case, otherwise - if no redirection required - it does nothing,
699  * unless the %force param is true.
700  */
701 static int iwl_mvm_redirect_queue(struct iwl_mvm *mvm, int queue, int tid,
702 				  int ac, int ssn, unsigned int wdg_timeout,
703 				  bool force, struct iwl_mvm_txq *txq)
704 {
705 	struct iwl_scd_txq_cfg_cmd cmd = {
706 		.scd_queue = queue,
707 		.action = SCD_CFG_DISABLE_QUEUE,
708 	};
709 	bool shared_queue;
710 	int ret;
711 
712 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
713 		return -EINVAL;
714 
715 	/*
716 	 * If the AC is lower than current one - FIFO needs to be redirected to
717 	 * the lowest one of the streams in the queue. Check if this is needed
718 	 * here.
719 	 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
720 	 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
721 	 * we need to check if the numerical value of X is LARGER than of Y.
722 	 */
723 	if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
724 		IWL_DEBUG_TX_QUEUES(mvm,
725 				    "No redirection needed on TXQ #%d\n",
726 				    queue);
727 		return 0;
728 	}
729 
730 	cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
731 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
732 	cmd.tid = mvm->queue_info[queue].txq_tid;
733 	shared_queue = hweight16(mvm->queue_info[queue].tid_bitmap) > 1;
734 
735 	IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
736 			    queue, iwl_mvm_ac_to_tx_fifo[ac]);
737 
738 	/* Stop the queue and wait for it to empty */
739 	set_bit(IWL_MVM_TXQ_STATE_STOP_REDIRECT, &txq->state);
740 
741 	ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue));
742 	if (ret) {
743 		IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
744 			queue);
745 		ret = -EIO;
746 		goto out;
747 	}
748 
749 	/* Before redirecting the queue we need to de-activate it */
750 	iwl_trans_txq_disable(mvm->trans, queue, false);
751 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
752 	if (ret)
753 		IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
754 			ret);
755 
756 	/* Make sure the SCD wrptr is correctly set before reconfiguring */
757 	iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
758 
759 	/* Update the TID "owner" of the queue */
760 	mvm->queue_info[queue].txq_tid = tid;
761 
762 	/* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
763 
764 	/* Redirect to lower AC */
765 	iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
766 			     cmd.sta_id, tid, IWL_FRAME_LIMIT, ssn);
767 
768 	/* Update AC marking of the queue */
769 	mvm->queue_info[queue].mac80211_ac = ac;
770 
771 	/*
772 	 * Mark queue as shared in transport if shared
773 	 * Note this has to be done after queue enablement because enablement
774 	 * can also set this value, and there is no indication there to shared
775 	 * queues
776 	 */
777 	if (shared_queue)
778 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
779 
780 out:
781 	/* Continue using the queue */
782 	clear_bit(IWL_MVM_TXQ_STATE_STOP_REDIRECT, &txq->state);
783 
784 	return ret;
785 }
786 
787 static int iwl_mvm_find_free_queue(struct iwl_mvm *mvm, u8 sta_id,
788 				   u8 minq, u8 maxq)
789 {
790 	int i;
791 
792 	lockdep_assert_held(&mvm->mutex);
793 
794 	if (WARN(maxq >= mvm->trans->mac_cfg->base->num_of_queues,
795 		 "max queue %d >= num_of_queues (%d)", maxq,
796 		 mvm->trans->mac_cfg->base->num_of_queues))
797 		maxq = mvm->trans->mac_cfg->base->num_of_queues - 1;
798 
799 	/* This should not be hit with new TX path */
800 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
801 		return -ENOSPC;
802 
803 	/* Start by looking for a free queue */
804 	for (i = minq; i <= maxq; i++)
805 		if (mvm->queue_info[i].tid_bitmap == 0 &&
806 		    mvm->queue_info[i].status == IWL_MVM_QUEUE_FREE)
807 			return i;
808 
809 	return -ENOSPC;
810 }
811 
812 static int iwl_mvm_get_queue_size(struct ieee80211_sta *sta)
813 {
814 	int max_size = IWL_DEFAULT_QUEUE_SIZE;
815 	unsigned int link_id;
816 
817 	/* this queue isn't used for traffic (cab_queue) */
818 	if (!sta)
819 		return IWL_MGMT_QUEUE_SIZE;
820 
821 	rcu_read_lock();
822 
823 	for (link_id = 0; link_id < ARRAY_SIZE(sta->link); link_id++) {
824 		struct ieee80211_link_sta *link =
825 			rcu_dereference(sta->link[link_id]);
826 
827 		if (!link)
828 			continue;
829 
830 		/* support for 512 ba size */
831 		if (link->eht_cap.has_eht &&
832 		    max_size < IWL_DEFAULT_QUEUE_SIZE_EHT)
833 			max_size = IWL_DEFAULT_QUEUE_SIZE_EHT;
834 
835 		/* support for 256 ba size */
836 		if (link->he_cap.has_he &&
837 		    max_size < IWL_DEFAULT_QUEUE_SIZE_HE)
838 			max_size = IWL_DEFAULT_QUEUE_SIZE_HE;
839 	}
840 
841 	rcu_read_unlock();
842 	return max_size;
843 }
844 
845 int iwl_mvm_tvqm_enable_txq(struct iwl_mvm *mvm,
846 			    struct ieee80211_sta *sta,
847 			    u8 sta_id, u8 tid, unsigned int timeout)
848 {
849 	int queue, size;
850 	u32 sta_mask = 0;
851 
852 	if (tid == IWL_MAX_TID_COUNT) {
853 		tid = IWL_MGMT_TID;
854 		size = max_t(u32, IWL_MGMT_QUEUE_SIZE,
855 			     mvm->trans->mac_cfg->base->min_txq_size);
856 	} else {
857 		size = iwl_mvm_get_queue_size(sta);
858 	}
859 
860 	if (sta) {
861 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
862 		struct ieee80211_link_sta *link_sta;
863 		unsigned int link_id;
864 
865 		rcu_read_lock();
866 		for_each_sta_active_link(mvmsta->vif, sta, link_sta, link_id) {
867 			struct iwl_mvm_link_sta *link =
868 				rcu_dereference_protected(mvmsta->link[link_id],
869 							  lockdep_is_held(&mvm->mutex));
870 
871 			if (!link)
872 				continue;
873 
874 			sta_mask |= BIT(link->sta_id);
875 		}
876 		rcu_read_unlock();
877 	} else {
878 		sta_mask |= BIT(sta_id);
879 	}
880 
881 	if (!sta_mask)
882 		return -EINVAL;
883 
884 	queue = iwl_trans_txq_alloc(mvm->trans, 0, sta_mask,
885 				    tid, size, timeout);
886 
887 	if (queue >= 0)
888 		IWL_DEBUG_TX_QUEUES(mvm,
889 				    "Enabling TXQ #%d for sta mask 0x%x tid %d\n",
890 				    queue, sta_mask, tid);
891 
892 	return queue;
893 }
894 
895 static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
896 					struct ieee80211_sta *sta, u8 ac,
897 					int tid)
898 {
899 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
900 	struct iwl_mvm_txq *mvmtxq =
901 		iwl_mvm_txq_from_tid(sta, tid);
902 	unsigned int wdg_timeout =
903 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif);
904 	int queue = -1;
905 
906 	lockdep_assert_held(&mvm->mutex);
907 
908 	IWL_DEBUG_TX_QUEUES(mvm,
909 			    "Allocating queue for sta %d on tid %d\n",
910 			    mvmsta->deflink.sta_id, tid);
911 	queue = iwl_mvm_tvqm_enable_txq(mvm, sta, mvmsta->deflink.sta_id,
912 					tid, wdg_timeout);
913 	if (queue < 0)
914 		return queue;
915 
916 	mvmtxq->txq_id = queue;
917 	mvm->tvqm_info[queue].txq_tid = tid;
918 	mvm->tvqm_info[queue].sta_id = mvmsta->deflink.sta_id;
919 
920 	IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
921 
922 	spin_lock_bh(&mvmsta->lock);
923 	mvmsta->tid_data[tid].txq_id = queue;
924 	spin_unlock_bh(&mvmsta->lock);
925 
926 	return 0;
927 }
928 
929 static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm,
930 				       struct ieee80211_sta *sta,
931 				       int queue, u8 sta_id, u8 tid)
932 {
933 	bool enable_queue = true;
934 
935 	/* Make sure this TID isn't already enabled */
936 	if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) {
937 		IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n",
938 			queue, tid);
939 		return false;
940 	}
941 
942 	/* Update mappings and refcounts */
943 	if (mvm->queue_info[queue].tid_bitmap)
944 		enable_queue = false;
945 
946 	mvm->queue_info[queue].tid_bitmap |= BIT(tid);
947 	mvm->queue_info[queue].ra_sta_id = sta_id;
948 
949 	if (enable_queue) {
950 		if (tid != IWL_MAX_TID_COUNT)
951 			mvm->queue_info[queue].mac80211_ac =
952 				tid_to_mac80211_ac[tid];
953 		else
954 			mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO;
955 
956 		mvm->queue_info[queue].txq_tid = tid;
957 	}
958 
959 	if (sta) {
960 		struct iwl_mvm_txq *mvmtxq =
961 			iwl_mvm_txq_from_tid(sta, tid);
962 
963 		mvmtxq->txq_id = queue;
964 	}
965 
966 	IWL_DEBUG_TX_QUEUES(mvm,
967 			    "Enabling TXQ #%d tids=0x%x\n",
968 			    queue, mvm->queue_info[queue].tid_bitmap);
969 
970 	return enable_queue;
971 }
972 
973 static bool iwl_mvm_enable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
974 			       int queue, u16 ssn,
975 			       const struct iwl_trans_txq_scd_cfg *cfg,
976 			       unsigned int wdg_timeout)
977 {
978 	struct iwl_scd_txq_cfg_cmd cmd = {
979 		.scd_queue = queue,
980 		.action = SCD_CFG_ENABLE_QUEUE,
981 		.window = cfg->frame_limit,
982 		.sta_id = cfg->sta_id,
983 		.ssn = cpu_to_le16(ssn),
984 		.tx_fifo = cfg->fifo,
985 		.aggregate = cfg->aggregate,
986 		.tid = cfg->tid,
987 	};
988 	bool inc_ssn;
989 
990 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
991 		return false;
992 
993 	/* Send the enabling command if we need to */
994 	if (!iwl_mvm_update_txq_mapping(mvm, sta, queue, cfg->sta_id, cfg->tid))
995 		return false;
996 
997 	inc_ssn = iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn,
998 					   NULL, wdg_timeout);
999 	if (inc_ssn)
1000 		le16_add_cpu(&cmd.ssn, 1);
1001 
1002 	WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd),
1003 	     "Failed to configure queue %d on FIFO %d\n", queue, cfg->fifo);
1004 
1005 	return inc_ssn;
1006 }
1007 
1008 static void iwl_mvm_change_queue_tid(struct iwl_mvm *mvm, int queue)
1009 {
1010 	struct iwl_scd_txq_cfg_cmd cmd = {
1011 		.scd_queue = queue,
1012 		.action = SCD_CFG_UPDATE_QUEUE_TID,
1013 	};
1014 	int tid;
1015 	unsigned long tid_bitmap;
1016 	int ret;
1017 
1018 	lockdep_assert_held(&mvm->mutex);
1019 
1020 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1021 		return;
1022 
1023 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1024 
1025 	if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
1026 		return;
1027 
1028 	/* Find any TID for queue */
1029 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
1030 	cmd.tid = tid;
1031 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
1032 
1033 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
1034 	if (ret) {
1035 		IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
1036 			queue, ret);
1037 		return;
1038 	}
1039 
1040 	mvm->queue_info[queue].txq_tid = tid;
1041 	IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
1042 			    queue, tid);
1043 }
1044 
1045 static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
1046 {
1047 	struct ieee80211_sta *sta;
1048 	struct iwl_mvm_sta *mvmsta;
1049 	u8 sta_id;
1050 	int tid = -1;
1051 	unsigned long tid_bitmap;
1052 	unsigned int wdg_timeout;
1053 	int ssn;
1054 	int ret = true;
1055 
1056 	/* queue sharing is disabled on new TX path */
1057 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1058 		return;
1059 
1060 	lockdep_assert_held(&mvm->mutex);
1061 
1062 	sta_id = mvm->queue_info[queue].ra_sta_id;
1063 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1064 
1065 	/* Find TID for queue, and make sure it is the only one on the queue */
1066 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
1067 	if (tid_bitmap != BIT(tid)) {
1068 		IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
1069 			queue, tid_bitmap);
1070 		return;
1071 	}
1072 
1073 	IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
1074 			    tid);
1075 
1076 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1077 					lockdep_is_held(&mvm->mutex));
1078 
1079 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
1080 		return;
1081 
1082 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
1083 	wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif);
1084 
1085 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1086 
1087 	ret = iwl_mvm_redirect_queue(mvm, queue, tid,
1088 				     tid_to_mac80211_ac[tid], ssn,
1089 				     wdg_timeout, true,
1090 				     iwl_mvm_txq_from_tid(sta, tid));
1091 	if (ret) {
1092 		IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
1093 		return;
1094 	}
1095 
1096 	/* If aggs should be turned back on - do it */
1097 	if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
1098 		struct iwl_mvm_add_sta_cmd cmd = {0};
1099 
1100 		mvmsta->tid_disable_agg &= ~BIT(tid);
1101 
1102 		cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1103 		cmd.sta_id = mvmsta->deflink.sta_id;
1104 		cmd.add_modify = STA_MODE_MODIFY;
1105 		cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1106 		cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
1107 		cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
1108 
1109 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
1110 					   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
1111 		if (!ret) {
1112 			IWL_DEBUG_TX_QUEUES(mvm,
1113 					    "TXQ #%d is now aggregated again\n",
1114 					    queue);
1115 
1116 			/* Mark queue intenally as aggregating again */
1117 			iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
1118 		}
1119 	}
1120 
1121 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1122 }
1123 
1124 /*
1125  * Remove inactive TIDs of a given queue.
1126  * If all queue TIDs are inactive - mark the queue as inactive
1127  * If only some the queue TIDs are inactive - unmap them from the queue
1128  *
1129  * Returns %true if all TIDs were removed and the queue could be reused.
1130  */
1131 static bool iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
1132 					 struct iwl_mvm_sta *mvmsta, int queue,
1133 					 unsigned long tid_bitmap,
1134 					 unsigned long *unshare_queues,
1135 					 unsigned long *changetid_queues)
1136 {
1137 	unsigned int tid;
1138 
1139 	lockdep_assert_held(&mvmsta->lock);
1140 	lockdep_assert_held(&mvm->mutex);
1141 
1142 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1143 		return false;
1144 
1145 	/* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
1146 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1147 		/* If some TFDs are still queued - don't mark TID as inactive */
1148 		if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid]))
1149 			tid_bitmap &= ~BIT(tid);
1150 
1151 		/* Don't mark as inactive any TID that has an active BA */
1152 		if (mvmsta->tid_data[tid].state != IWL_AGG_OFF)
1153 			tid_bitmap &= ~BIT(tid);
1154 	}
1155 
1156 	/* If all TIDs in the queue are inactive - return it can be reused */
1157 	if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
1158 		IWL_DEBUG_TX_QUEUES(mvm, "Queue %d is inactive\n", queue);
1159 		return true;
1160 	}
1161 
1162 	/*
1163 	 * If we are here, this is a shared queue and not all TIDs timed-out.
1164 	 * Remove the ones that did.
1165 	 */
1166 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1167 		u16 q_tid_bitmap;
1168 
1169 		mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
1170 		mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
1171 
1172 		q_tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1173 
1174 		/*
1175 		 * We need to take into account a situation in which a TXQ was
1176 		 * allocated to TID x, and then turned shared by adding TIDs y
1177 		 * and z. If TID x becomes inactive and is removed from the TXQ,
1178 		 * ownership must be given to one of the remaining TIDs.
1179 		 * This is mainly because if TID x continues - a new queue can't
1180 		 * be allocated for it as long as it is an owner of another TXQ.
1181 		 *
1182 		 * Mark this queue in the right bitmap, we'll send the command
1183 		 * to the firmware later.
1184 		 */
1185 		if (!(q_tid_bitmap & BIT(mvm->queue_info[queue].txq_tid)))
1186 			set_bit(queue, changetid_queues);
1187 
1188 		IWL_DEBUG_TX_QUEUES(mvm,
1189 				    "Removing inactive TID %d from shared Q:%d\n",
1190 				    tid, queue);
1191 	}
1192 
1193 	IWL_DEBUG_TX_QUEUES(mvm,
1194 			    "TXQ #%d left with tid bitmap 0x%x\n", queue,
1195 			    mvm->queue_info[queue].tid_bitmap);
1196 
1197 	/*
1198 	 * There may be different TIDs with the same mac queues, so make
1199 	 * sure all TIDs have existing corresponding mac queues enabled
1200 	 */
1201 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1202 
1203 	/* If the queue is marked as shared - "unshare" it */
1204 	if (hweight16(mvm->queue_info[queue].tid_bitmap) == 1 &&
1205 	    mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
1206 		IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
1207 				    queue);
1208 		set_bit(queue, unshare_queues);
1209 	}
1210 
1211 	return false;
1212 }
1213 
1214 /*
1215  * Check for inactivity - this includes checking if any queue
1216  * can be unshared and finding one (and only one) that can be
1217  * reused.
1218  * This function is also invoked as a sort of clean-up task,
1219  * in which case @alloc_for_sta is IWL_INVALID_STA.
1220  *
1221  * Returns the queue number, or -ENOSPC.
1222  */
1223 static int iwl_mvm_inactivity_check(struct iwl_mvm *mvm, u8 alloc_for_sta)
1224 {
1225 	unsigned long now = jiffies;
1226 	unsigned long unshare_queues = 0;
1227 	unsigned long changetid_queues = 0;
1228 	int i, ret, free_queue = -ENOSPC;
1229 	struct ieee80211_sta *queue_owner  = NULL;
1230 
1231 	lockdep_assert_held(&mvm->mutex);
1232 
1233 	if (iwl_mvm_has_new_tx_api(mvm))
1234 		return -ENOSPC;
1235 
1236 	rcu_read_lock();
1237 
1238 	/* we skip the CMD queue below by starting at 1 */
1239 	BUILD_BUG_ON(IWL_MVM_DQA_CMD_QUEUE != 0);
1240 
1241 	for (i = 1; i < IWL_MAX_HW_QUEUES; i++) {
1242 		struct ieee80211_sta *sta;
1243 		struct iwl_mvm_sta *mvmsta;
1244 		u8 sta_id;
1245 		int tid;
1246 		unsigned long inactive_tid_bitmap = 0;
1247 		unsigned long queue_tid_bitmap;
1248 
1249 		queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
1250 		if (!queue_tid_bitmap)
1251 			continue;
1252 
1253 		/* If TXQ isn't in active use anyway - nothing to do here... */
1254 		if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
1255 		    mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED)
1256 			continue;
1257 
1258 		/* Check to see if there are inactive TIDs on this queue */
1259 		for_each_set_bit(tid, &queue_tid_bitmap,
1260 				 IWL_MAX_TID_COUNT + 1) {
1261 			if (time_after(mvm->queue_info[i].last_frame_time[tid] +
1262 				       IWL_MVM_DQA_QUEUE_TIMEOUT, now))
1263 				continue;
1264 
1265 			inactive_tid_bitmap |= BIT(tid);
1266 		}
1267 
1268 		/* If all TIDs are active - finish check on this queue */
1269 		if (!inactive_tid_bitmap)
1270 			continue;
1271 
1272 		/*
1273 		 * If we are here - the queue hadn't been served recently and is
1274 		 * in use
1275 		 */
1276 
1277 		sta_id = mvm->queue_info[i].ra_sta_id;
1278 		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1279 
1280 		/*
1281 		 * If the STA doesn't exist anymore, it isn't an error. It could
1282 		 * be that it was removed since getting the queues, and in this
1283 		 * case it should've inactivated its queues anyway.
1284 		 */
1285 		if (IS_ERR_OR_NULL(sta))
1286 			continue;
1287 
1288 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
1289 
1290 		spin_lock_bh(&mvmsta->lock);
1291 		ret = iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
1292 						   inactive_tid_bitmap,
1293 						   &unshare_queues,
1294 						   &changetid_queues);
1295 		if (ret && free_queue < 0) {
1296 			queue_owner = sta;
1297 			free_queue = i;
1298 		}
1299 		/* only unlock sta lock - we still need the queue info lock */
1300 		spin_unlock_bh(&mvmsta->lock);
1301 	}
1302 
1303 
1304 	/* Reconfigure queues requiring reconfiguation */
1305 	for_each_set_bit(i, &unshare_queues, IWL_MAX_HW_QUEUES)
1306 		iwl_mvm_unshare_queue(mvm, i);
1307 	for_each_set_bit(i, &changetid_queues, IWL_MAX_HW_QUEUES)
1308 		iwl_mvm_change_queue_tid(mvm, i);
1309 
1310 	rcu_read_unlock();
1311 
1312 	if (free_queue >= 0 && alloc_for_sta != IWL_INVALID_STA) {
1313 		ret = iwl_mvm_free_inactive_queue(mvm, free_queue, queue_owner,
1314 						  alloc_for_sta);
1315 		if (ret)
1316 			return ret;
1317 	}
1318 
1319 	return free_queue;
1320 }
1321 
1322 static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
1323 				   struct ieee80211_sta *sta, u8 ac, int tid)
1324 {
1325 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1326 	struct iwl_trans_txq_scd_cfg cfg = {
1327 		.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
1328 		.sta_id = mvmsta->deflink.sta_id,
1329 		.tid = tid,
1330 		.frame_limit = IWL_FRAME_LIMIT,
1331 	};
1332 	unsigned int wdg_timeout =
1333 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif);
1334 	int queue = -1;
1335 	u16 queue_tmp;
1336 	unsigned long disable_agg_tids = 0;
1337 	enum iwl_mvm_agg_state queue_state;
1338 	bool shared_queue = false, inc_ssn;
1339 	int ssn;
1340 	unsigned long tfd_queue_mask;
1341 	int ret;
1342 
1343 	lockdep_assert_held(&mvm->mutex);
1344 
1345 	if (iwl_mvm_has_new_tx_api(mvm))
1346 		return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
1347 
1348 	spin_lock_bh(&mvmsta->lock);
1349 	tfd_queue_mask = mvmsta->tfd_queue_msk;
1350 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1351 	spin_unlock_bh(&mvmsta->lock);
1352 
1353 	if (tid == IWL_MAX_TID_COUNT) {
1354 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->deflink.sta_id,
1355 						IWL_MVM_DQA_MIN_MGMT_QUEUE,
1356 						IWL_MVM_DQA_MAX_MGMT_QUEUE);
1357 		if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
1358 			IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
1359 					    queue);
1360 
1361 		/* If no such queue is found, we'll use a DATA queue instead */
1362 	}
1363 
1364 	if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
1365 	    (mvm->queue_info[mvmsta->reserved_queue].status ==
1366 			IWL_MVM_QUEUE_RESERVED)) {
1367 		queue = mvmsta->reserved_queue;
1368 		mvm->queue_info[queue].reserved = true;
1369 		IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
1370 	}
1371 
1372 	if (queue < 0)
1373 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->deflink.sta_id,
1374 						IWL_MVM_DQA_MIN_DATA_QUEUE,
1375 						IWL_MVM_DQA_MAX_DATA_QUEUE);
1376 	if (queue < 0) {
1377 		/* try harder - perhaps kill an inactive queue */
1378 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->deflink.sta_id);
1379 	}
1380 
1381 	/* No free queue - we'll have to share */
1382 	if (queue <= 0) {
1383 		queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
1384 		if (queue > 0) {
1385 			shared_queue = true;
1386 			mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
1387 		}
1388 	}
1389 
1390 	/*
1391 	 * Mark TXQ as ready, even though it hasn't been fully configured yet,
1392 	 * to make sure no one else takes it.
1393 	 * This will allow avoiding re-acquiring the lock at the end of the
1394 	 * configuration. On error we'll mark it back as free.
1395 	 */
1396 	if (queue > 0 && !shared_queue)
1397 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1398 
1399 	/* This shouldn't happen - out of queues */
1400 	if (WARN_ON(queue <= 0)) {
1401 		IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
1402 			tid, cfg.sta_id);
1403 		return queue;
1404 	}
1405 
1406 	/*
1407 	 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
1408 	 * but for configuring the SCD to send A-MPDUs we need to mark the queue
1409 	 * as aggregatable.
1410 	 * Mark all DATA queues as allowing to be aggregated at some point
1411 	 */
1412 	cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1413 			 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1414 
1415 	IWL_DEBUG_TX_QUEUES(mvm,
1416 			    "Allocating %squeue #%d to sta %d on tid %d\n",
1417 			    shared_queue ? "shared " : "", queue,
1418 			    mvmsta->deflink.sta_id, tid);
1419 
1420 	if (shared_queue) {
1421 		/* Disable any open aggs on this queue */
1422 		disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
1423 
1424 		if (disable_agg_tids) {
1425 			IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
1426 					    queue);
1427 			iwl_mvm_invalidate_sta_queue(mvm, queue,
1428 						     disable_agg_tids, false);
1429 		}
1430 	}
1431 
1432 	inc_ssn = iwl_mvm_enable_txq(mvm, sta, queue, ssn, &cfg, wdg_timeout);
1433 
1434 	/*
1435 	 * Mark queue as shared in transport if shared
1436 	 * Note this has to be done after queue enablement because enablement
1437 	 * can also set this value, and there is no indication there to shared
1438 	 * queues
1439 	 */
1440 	if (shared_queue)
1441 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
1442 
1443 	spin_lock_bh(&mvmsta->lock);
1444 	/*
1445 	 * This looks racy, but it is not. We have only one packet for
1446 	 * this ra/tid in our Tx path since we stop the Qdisc when we
1447 	 * need to allocate a new TFD queue.
1448 	 */
1449 	if (inc_ssn) {
1450 		mvmsta->tid_data[tid].seq_number += 0x10;
1451 		ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
1452 	}
1453 	mvmsta->tid_data[tid].txq_id = queue;
1454 	mvmsta->tfd_queue_msk |= BIT(queue);
1455 	queue_state = mvmsta->tid_data[tid].state;
1456 
1457 	if (mvmsta->reserved_queue == queue)
1458 		mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
1459 	spin_unlock_bh(&mvmsta->lock);
1460 
1461 	if (!shared_queue) {
1462 		ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
1463 		if (ret)
1464 			goto out_err;
1465 
1466 		/* If we need to re-enable aggregations... */
1467 		if (queue_state == IWL_AGG_ON) {
1468 			ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1469 			if (ret)
1470 				goto out_err;
1471 		}
1472 	} else {
1473 		/* Redirect queue, if needed */
1474 		ret = iwl_mvm_redirect_queue(mvm, queue, tid, ac, ssn,
1475 					     wdg_timeout, false,
1476 					     iwl_mvm_txq_from_tid(sta, tid));
1477 		if (ret)
1478 			goto out_err;
1479 	}
1480 
1481 	return 0;
1482 
1483 out_err:
1484 	queue_tmp = queue;
1485 	iwl_mvm_disable_txq(mvm, sta, mvmsta->deflink.sta_id, &queue_tmp, tid);
1486 
1487 	return ret;
1488 }
1489 
1490 int iwl_mvm_sta_ensure_queue(struct iwl_mvm *mvm,
1491 			     struct ieee80211_txq *txq)
1492 {
1493 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
1494 	int ret = -EINVAL;
1495 
1496 	lockdep_assert_held(&mvm->mutex);
1497 
1498 	if (likely(test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) ||
1499 	    !txq->sta) {
1500 		return 0;
1501 	}
1502 
1503 	if (!iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, txq->tid)) {
1504 		set_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state);
1505 		ret = 0;
1506 	}
1507 
1508 	local_bh_disable();
1509 	spin_lock(&mvm->add_stream_lock);
1510 	if (!list_empty(&mvmtxq->list))
1511 		list_del_init(&mvmtxq->list);
1512 	spin_unlock(&mvm->add_stream_lock);
1513 	local_bh_enable();
1514 
1515 	return ret;
1516 }
1517 
1518 void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1519 {
1520 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1521 					   add_stream_wk);
1522 
1523 	guard(mvm)(mvm);
1524 
1525 	/* will reschedule to run after restart */
1526 	if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status) ||
1527 	    test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1528 		return;
1529 
1530 	iwl_mvm_inactivity_check(mvm, IWL_INVALID_STA);
1531 
1532 	while (!list_empty(&mvm->add_stream_txqs)) {
1533 		struct iwl_mvm_txq *mvmtxq;
1534 		struct ieee80211_txq *txq;
1535 		u8 tid;
1536 
1537 		mvmtxq = list_first_entry(&mvm->add_stream_txqs,
1538 					  struct iwl_mvm_txq, list);
1539 
1540 		txq = container_of((void *)mvmtxq, struct ieee80211_txq,
1541 				   drv_priv);
1542 		tid = txq->tid;
1543 		if (tid == IEEE80211_NUM_TIDS)
1544 			tid = IWL_MAX_TID_COUNT;
1545 
1546 		/*
1547 		 * We can't really do much here, but if this fails we can't
1548 		 * transmit anyway - so just don't transmit the frame etc.
1549 		 * and let them back up ... we've tried our best to allocate
1550 		 * a queue in the function itself.
1551 		 */
1552 		if (iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, tid)) {
1553 			spin_lock_bh(&mvm->add_stream_lock);
1554 			list_del_init(&mvmtxq->list);
1555 			spin_unlock_bh(&mvm->add_stream_lock);
1556 			continue;
1557 		}
1558 
1559 		/* now we're ready, any remaining races/concurrency will be
1560 		 * handled in iwl_mvm_mac_itxq_xmit()
1561 		 */
1562 		set_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state);
1563 
1564 		local_bh_disable();
1565 		spin_lock(&mvm->add_stream_lock);
1566 		list_del_init(&mvmtxq->list);
1567 		spin_unlock(&mvm->add_stream_lock);
1568 
1569 		iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1570 		local_bh_enable();
1571 	}
1572 }
1573 
1574 static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1575 				      struct ieee80211_sta *sta,
1576 				      enum nl80211_iftype vif_type)
1577 {
1578 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1579 	int queue;
1580 
1581 	/* queue reserving is disabled on new TX path */
1582 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1583 		return 0;
1584 
1585 	/* run the general cleanup/unsharing of queues */
1586 	iwl_mvm_inactivity_check(mvm, IWL_INVALID_STA);
1587 
1588 	/* Make sure we have free resources for this STA */
1589 	if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1590 	    !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].tid_bitmap &&
1591 	    (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1592 	     IWL_MVM_QUEUE_FREE))
1593 		queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1594 	else
1595 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->deflink.sta_id,
1596 						IWL_MVM_DQA_MIN_DATA_QUEUE,
1597 						IWL_MVM_DQA_MAX_DATA_QUEUE);
1598 	if (queue < 0) {
1599 		/* try again - this time kick out a queue if needed */
1600 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->deflink.sta_id);
1601 		if (queue < 0) {
1602 			IWL_ERR(mvm, "No available queues for new station\n");
1603 			return -ENOSPC;
1604 		}
1605 	}
1606 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
1607 
1608 	mvmsta->reserved_queue = queue;
1609 
1610 	IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1611 			    queue, mvmsta->deflink.sta_id);
1612 
1613 	return 0;
1614 }
1615 
1616 /*
1617  * In DQA mode, after a HW restart the queues should be allocated as before, in
1618  * order to avoid race conditions when there are shared queues. This function
1619  * does the re-mapping and queue allocation.
1620  *
1621  * Note that re-enabling aggregations isn't done in this function.
1622  */
1623 void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1624 					  struct ieee80211_sta *sta)
1625 {
1626 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1627 	unsigned int wdg =
1628 		iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif);
1629 	int i;
1630 	struct iwl_trans_txq_scd_cfg cfg = {
1631 		.sta_id = mvm_sta->deflink.sta_id,
1632 		.frame_limit = IWL_FRAME_LIMIT,
1633 	};
1634 
1635 	/* Make sure reserved queue is still marked as such (if allocated) */
1636 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
1637 		mvm->queue_info[mvm_sta->reserved_queue].status =
1638 			IWL_MVM_QUEUE_RESERVED;
1639 
1640 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1641 		struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1642 		int txq_id = tid_data->txq_id;
1643 		int ac;
1644 
1645 		if (txq_id == IWL_MVM_INVALID_QUEUE)
1646 			continue;
1647 
1648 		ac = tid_to_mac80211_ac[i];
1649 
1650 		if (iwl_mvm_has_new_tx_api(mvm)) {
1651 			IWL_DEBUG_TX_QUEUES(mvm,
1652 					    "Re-mapping sta %d tid %d\n",
1653 					    mvm_sta->deflink.sta_id, i);
1654 			txq_id = iwl_mvm_tvqm_enable_txq(mvm, sta,
1655 							 mvm_sta->deflink.sta_id,
1656 							 i, wdg);
1657 			/*
1658 			 * on failures, just set it to IWL_MVM_INVALID_QUEUE
1659 			 * to try again later, we have no other good way of
1660 			 * failing here
1661 			 */
1662 			if (txq_id < 0)
1663 				txq_id = IWL_MVM_INVALID_QUEUE;
1664 			tid_data->txq_id = txq_id;
1665 
1666 			/*
1667 			 * Since we don't set the seq number after reset, and HW
1668 			 * sets it now, FW reset will cause the seq num to start
1669 			 * at 0 again, so driver will need to update it
1670 			 * internally as well, so it keeps in sync with real val
1671 			 */
1672 			tid_data->seq_number = 0;
1673 		} else {
1674 			u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1675 
1676 			cfg.tid = i;
1677 			cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
1678 			cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1679 					 txq_id ==
1680 					 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1681 
1682 			IWL_DEBUG_TX_QUEUES(mvm,
1683 					    "Re-mapping sta %d tid %d to queue %d\n",
1684 					    mvm_sta->deflink.sta_id, i,
1685 					    txq_id);
1686 
1687 			iwl_mvm_enable_txq(mvm, sta, txq_id, seq, &cfg, wdg);
1688 			mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
1689 		}
1690 	}
1691 }
1692 
1693 static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1694 				      struct iwl_mvm_int_sta *sta,
1695 				      const u8 *addr,
1696 				      u16 mac_id, u16 color)
1697 {
1698 	struct iwl_mvm_add_sta_cmd cmd;
1699 	int ret;
1700 	u32 status = ADD_STA_SUCCESS;
1701 
1702 	lockdep_assert_held(&mvm->mutex);
1703 
1704 	memset(&cmd, 0, sizeof(cmd));
1705 	cmd.sta_id = sta->sta_id;
1706 
1707 	if (iwl_mvm_has_new_station_api(mvm->fw) &&
1708 	    sta->type == IWL_STA_AUX_ACTIVITY)
1709 		cmd.mac_id_n_color = cpu_to_le32(mac_id);
1710 	else
1711 		cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1712 								     color));
1713 
1714 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1715 		cmd.station_type = sta->type;
1716 
1717 	if (!iwl_mvm_has_new_tx_api(mvm))
1718 		cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1719 	cmd.tid_disable_tx = cpu_to_le16(0xffff);
1720 
1721 	if (addr)
1722 		memcpy(cmd.addr, addr, ETH_ALEN);
1723 
1724 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1725 					  iwl_mvm_add_sta_cmd_size(mvm),
1726 					  &cmd, &status);
1727 	if (ret)
1728 		return ret;
1729 
1730 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1731 	case ADD_STA_SUCCESS:
1732 		IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1733 		return 0;
1734 	default:
1735 		ret = -EIO;
1736 		IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1737 			status);
1738 		break;
1739 	}
1740 	return ret;
1741 }
1742 
1743 /* Initialize driver data of a new sta */
1744 int iwl_mvm_sta_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1745 		     struct ieee80211_sta *sta, int sta_id, u8 sta_type)
1746 {
1747 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1748 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1749 	struct iwl_mvm_rxq_dup_data *dup_data;
1750 	int i, ret = 0;
1751 
1752 	lockdep_assert_held(&mvm->mutex);
1753 
1754 	mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1755 						      mvmvif->color);
1756 	mvm_sta->vif = vif;
1757 
1758 	/* for MLD sta_id(s) should be allocated for each link before calling
1759 	 * this function
1760 	 */
1761 	if (!mvm->mld_api_is_used) {
1762 		if (WARN_ON(sta_id == IWL_INVALID_STA))
1763 			return -EINVAL;
1764 
1765 		mvm_sta->deflink.sta_id = sta_id;
1766 		rcu_assign_pointer(mvm_sta->link[0], &mvm_sta->deflink);
1767 
1768 		if (!mvm->trans->mac_cfg->gen2)
1769 			mvm_sta->deflink.lq_sta.rs_drv.pers.max_agg_bufsize =
1770 				LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1771 		else
1772 			mvm_sta->deflink.lq_sta.rs_drv.pers.max_agg_bufsize =
1773 				LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
1774 	}
1775 
1776 	mvm_sta->tt_tx_protection = false;
1777 	mvm_sta->sta_type = sta_type;
1778 
1779 	mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1780 
1781 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1782 		/*
1783 		 * Mark all queues for this STA as unallocated and defer TX
1784 		 * frames until the queue is allocated
1785 		 */
1786 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1787 	}
1788 
1789 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1790 		struct iwl_mvm_txq *mvmtxq =
1791 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1792 
1793 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1794 		INIT_LIST_HEAD(&mvmtxq->list);
1795 		atomic_set(&mvmtxq->tx_request, 0);
1796 	}
1797 
1798 	if (iwl_mvm_has_new_rx_api(mvm)) {
1799 		int q;
1800 
1801 		dup_data = kzalloc_objs(*dup_data, mvm->trans->info.num_rxqs);
1802 		if (!dup_data)
1803 			return -ENOMEM;
1804 		/*
1805 		 * Initialize all the last_seq values to 0xffff which can never
1806 		 * compare equal to the frame's seq_ctrl in the check in
1807 		 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
1808 		 * number and fragmented packets don't reach that function.
1809 		 *
1810 		 * This thus allows receiving a packet with seqno 0 and the
1811 		 * retry bit set as the very first packet on a new TID.
1812 		 */
1813 		for (q = 0; q < mvm->trans->info.num_rxqs; q++)
1814 			memset(dup_data[q].last_seq, 0xff,
1815 			       sizeof(dup_data[q].last_seq));
1816 		mvm_sta->dup_data = dup_data;
1817 	}
1818 
1819 	if (!iwl_mvm_has_new_tx_api(mvm)) {
1820 		ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1821 						 ieee80211_vif_type_p2p(vif));
1822 		if (ret)
1823 			return ret;
1824 	}
1825 
1826 	/*
1827 	 * if rs is registered with mac80211, then "add station" will be handled
1828 	 * via the corresponding ops, otherwise need to notify rate scaling here
1829 	 */
1830 	if (iwl_mvm_has_tlc_offload(mvm))
1831 		iwl_mvm_rs_add_sta(mvm, mvm_sta);
1832 	else
1833 		spin_lock_init(&mvm_sta->deflink.lq_sta.rs_drv.pers.lock);
1834 
1835 	iwl_mvm_toggle_tx_ant(mvm, &mvm_sta->tx_ant);
1836 
1837 	return 0;
1838 }
1839 
1840 int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1841 		    struct ieee80211_vif *vif,
1842 		    struct ieee80211_sta *sta)
1843 {
1844 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1845 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1846 	int ret, sta_id;
1847 	bool sta_update = false;
1848 	unsigned int sta_flags = 0;
1849 
1850 	lockdep_assert_held(&mvm->mutex);
1851 
1852 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1853 		sta_id = iwl_mvm_find_free_sta_id(mvm,
1854 						  ieee80211_vif_type_p2p(vif));
1855 	else
1856 		sta_id = mvm_sta->deflink.sta_id;
1857 
1858 	if (sta_id == IWL_INVALID_STA)
1859 		return -ENOSPC;
1860 
1861 	spin_lock_init(&mvm_sta->lock);
1862 
1863 	/* if this is a HW restart re-alloc existing queues */
1864 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1865 		struct iwl_mvm_int_sta tmp_sta = {
1866 			.sta_id = sta_id,
1867 			.type = mvm_sta->sta_type,
1868 		};
1869 
1870 		/* First add an empty station since allocating
1871 		 * a queue requires a valid station
1872 		 */
1873 		ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1874 						 mvmvif->id, mvmvif->color);
1875 		if (ret)
1876 			goto err;
1877 
1878 		iwl_mvm_realloc_queues_after_restart(mvm, sta);
1879 		sta_update = true;
1880 		sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
1881 		goto update_fw;
1882 	}
1883 
1884 	ret = iwl_mvm_sta_init(mvm, vif, sta, sta_id,
1885 			       sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK);
1886 	if (ret)
1887 		goto err;
1888 
1889 update_fw:
1890 	ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
1891 	if (ret)
1892 		goto err;
1893 
1894 	if (vif->type == NL80211_IFTYPE_STATION) {
1895 		if (!sta->tdls) {
1896 			WARN_ON(mvmvif->deflink.ap_sta_id != IWL_INVALID_STA);
1897 			mvmvif->deflink.ap_sta_id = sta_id;
1898 		} else {
1899 			WARN_ON(mvmvif->deflink.ap_sta_id == IWL_INVALID_STA);
1900 		}
1901 	}
1902 
1903 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1904 
1905 	return 0;
1906 
1907 err:
1908 	return ret;
1909 }
1910 
1911 int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1912 		      bool drain)
1913 {
1914 	struct iwl_mvm_add_sta_cmd cmd = {};
1915 	int ret;
1916 	u32 status;
1917 
1918 	lockdep_assert_held(&mvm->mutex);
1919 
1920 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1921 	cmd.sta_id = mvmsta->deflink.sta_id;
1922 	cmd.add_modify = STA_MODE_MODIFY;
1923 	cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1924 	cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1925 
1926 	status = ADD_STA_SUCCESS;
1927 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1928 					  iwl_mvm_add_sta_cmd_size(mvm),
1929 					  &cmd, &status);
1930 	if (ret)
1931 		return ret;
1932 
1933 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1934 	case ADD_STA_SUCCESS:
1935 		IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1936 			       mvmsta->deflink.sta_id);
1937 		break;
1938 	default:
1939 		ret = -EIO;
1940 		IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1941 			mvmsta->deflink.sta_id);
1942 		break;
1943 	}
1944 
1945 	return ret;
1946 }
1947 
1948 /*
1949  * Remove a station from the FW table. Before sending the command to remove
1950  * the station validate that the station is indeed known to the driver (sanity
1951  * only).
1952  */
1953 static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1954 {
1955 	struct ieee80211_sta *sta;
1956 	struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1957 		.sta_id = sta_id,
1958 	};
1959 	int ret;
1960 
1961 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1962 					lockdep_is_held(&mvm->mutex));
1963 
1964 	/* Note: internal stations are marked as error values */
1965 	if (!sta) {
1966 		IWL_ERR(mvm, "Invalid station id\n");
1967 		return -EINVAL;
1968 	}
1969 
1970 	ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1971 				   sizeof(rm_sta_cmd), &rm_sta_cmd);
1972 	if (ret) {
1973 		IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1974 		return ret;
1975 	}
1976 
1977 	return 0;
1978 }
1979 
1980 static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1981 				       struct ieee80211_vif *vif,
1982 				       struct ieee80211_sta *sta)
1983 {
1984 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1985 	int i;
1986 
1987 	lockdep_assert_held(&mvm->mutex);
1988 
1989 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1990 		if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
1991 			continue;
1992 
1993 		iwl_mvm_disable_txq(mvm, sta, mvm_sta->deflink.sta_id,
1994 				    &mvm_sta->tid_data[i].txq_id, i);
1995 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1996 	}
1997 
1998 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1999 		struct iwl_mvm_txq *mvmtxq =
2000 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
2001 
2002 		spin_lock_bh(&mvm->add_stream_lock);
2003 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
2004 		list_del_init(&mvmtxq->list);
2005 		clear_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state);
2006 		spin_unlock_bh(&mvm->add_stream_lock);
2007 	}
2008 }
2009 
2010 int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
2011 				  struct iwl_mvm_sta *mvm_sta)
2012 {
2013 	int i;
2014 
2015 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
2016 		u16 txq_id;
2017 		int ret;
2018 
2019 		spin_lock_bh(&mvm_sta->lock);
2020 		txq_id = mvm_sta->tid_data[i].txq_id;
2021 		spin_unlock_bh(&mvm_sta->lock);
2022 
2023 		if (txq_id == IWL_MVM_INVALID_QUEUE)
2024 			continue;
2025 
2026 		ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
2027 		if (ret)
2028 			return ret;
2029 	}
2030 
2031 	return 0;
2032 }
2033 
2034 /* Execute the common part for both MLD and non-MLD modes.
2035  * Returns if we're done with removing the station, either
2036  * with error or success
2037  */
2038 void iwl_mvm_sta_del(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2039 		     struct ieee80211_sta *sta,
2040 		     struct ieee80211_link_sta *link_sta)
2041 {
2042 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2043 	struct iwl_mvm_vif_link_info *mvm_link =
2044 		mvmvif->link[link_sta->link_id];
2045 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2046 	struct iwl_mvm_link_sta *mvm_link_sta;
2047 	u8 sta_id;
2048 
2049 	lockdep_assert_held(&mvm->mutex);
2050 
2051 	mvm_link_sta =
2052 		rcu_dereference_protected(mvm_sta->link[link_sta->link_id],
2053 					  lockdep_is_held(&mvm->mutex));
2054 	sta_id = mvm_link_sta->sta_id;
2055 
2056 	if (vif->type == NL80211_IFTYPE_STATION &&
2057 	    mvm_link->ap_sta_id == sta_id) {
2058 		/* first remove remaining keys */
2059 		iwl_mvm_sec_key_remove_ap(mvm, vif, mvm_link,
2060 					  link_sta->link_id);
2061 
2062 		mvm_link->ap_sta_id = IWL_INVALID_STA;
2063 	}
2064 
2065 	/*
2066 	 * This shouldn't happen - the TDLS channel switch should be canceled
2067 	 * before the STA is removed.
2068 	 */
2069 	if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
2070 		mvm->tdls_cs.peer.sta_id = IWL_INVALID_STA;
2071 		cancel_delayed_work(&mvm->tdls_cs.dwork);
2072 	}
2073 }
2074 
2075 int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
2076 		   struct ieee80211_vif *vif,
2077 		   struct ieee80211_sta *sta)
2078 {
2079 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2080 	int ret;
2081 
2082 	lockdep_assert_held(&mvm->mutex);
2083 
2084 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
2085 	if (ret)
2086 		return ret;
2087 
2088 	/* flush its queues here since we are freeing mvm_sta */
2089 	ret = iwl_mvm_flush_sta(mvm, mvm_sta->deflink.sta_id,
2090 				mvm_sta->tfd_queue_msk);
2091 	if (ret)
2092 		return ret;
2093 	if (iwl_mvm_has_new_tx_api(mvm)) {
2094 		ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
2095 	} else {
2096 		u32 q_mask = mvm_sta->tfd_queue_msk;
2097 
2098 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2099 						     q_mask);
2100 	}
2101 	if (ret)
2102 		return ret;
2103 
2104 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
2105 
2106 	iwl_mvm_disable_sta_queues(mvm, vif, sta);
2107 
2108 	/* If there is a TXQ still marked as reserved - free it */
2109 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
2110 		u8 reserved_txq = mvm_sta->reserved_queue;
2111 		enum iwl_mvm_queue_status *status;
2112 
2113 		/*
2114 		 * If no traffic has gone through the reserved TXQ - it
2115 		 * is still marked as IWL_MVM_QUEUE_RESERVED, and
2116 		 * should be manually marked as free again
2117 		 */
2118 		status = &mvm->queue_info[reserved_txq].status;
2119 		if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
2120 			 (*status != IWL_MVM_QUEUE_FREE),
2121 			 "sta_id %d reserved txq %d status %d",
2122 			 mvm_sta->deflink.sta_id, reserved_txq, *status))
2123 			return -EINVAL;
2124 
2125 		*status = IWL_MVM_QUEUE_FREE;
2126 	}
2127 
2128 	iwl_mvm_sta_del(mvm, vif, sta, &sta->deflink);
2129 
2130 	ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->deflink.sta_id);
2131 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->deflink.sta_id], NULL);
2132 
2133 	return ret;
2134 }
2135 
2136 int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
2137 		      struct ieee80211_vif *vif,
2138 		      u8 sta_id)
2139 {
2140 	int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
2141 
2142 	lockdep_assert_held(&mvm->mutex);
2143 
2144 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
2145 	return ret;
2146 }
2147 
2148 int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
2149 			     struct iwl_mvm_int_sta *sta,
2150 			     u32 qmask, enum nl80211_iftype iftype,
2151 			     u8 type)
2152 {
2153 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
2154 	    sta->sta_id == IWL_INVALID_STA) {
2155 		sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
2156 		if (WARN_ON_ONCE(sta->sta_id == IWL_INVALID_STA))
2157 			return -ENOSPC;
2158 	}
2159 
2160 	sta->tfd_queue_msk = qmask;
2161 	sta->type = type;
2162 
2163 	/* put a non-NULL value so iterating over the stations won't stop */
2164 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
2165 	return 0;
2166 }
2167 
2168 void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
2169 {
2170 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
2171 	memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
2172 	sta->sta_id = IWL_INVALID_STA;
2173 }
2174 
2175 static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 queue,
2176 					  u8 sta_id, u8 fifo)
2177 {
2178 	unsigned int wdg_timeout =
2179 		mvm->trans->mac_cfg->base->wd_timeout;
2180 	struct iwl_trans_txq_scd_cfg cfg = {
2181 		.fifo = fifo,
2182 		.sta_id = sta_id,
2183 		.tid = IWL_MAX_TID_COUNT,
2184 		.aggregate = false,
2185 		.frame_limit = IWL_FRAME_LIMIT,
2186 	};
2187 
2188 	WARN_ON(iwl_mvm_has_new_tx_api(mvm));
2189 
2190 	iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2191 }
2192 
2193 static int iwl_mvm_enable_aux_snif_queue_tvqm(struct iwl_mvm *mvm, u8 sta_id)
2194 {
2195 	unsigned int wdg_timeout =
2196 		mvm->trans->mac_cfg->base->wd_timeout;
2197 
2198 	WARN_ON(!iwl_mvm_has_new_tx_api(mvm));
2199 
2200 	return iwl_mvm_tvqm_enable_txq(mvm, NULL, sta_id, IWL_MAX_TID_COUNT,
2201 				       wdg_timeout);
2202 }
2203 
2204 static int iwl_mvm_add_int_sta_with_queue(struct iwl_mvm *mvm, int macidx,
2205 					  int maccolor, u8 *addr,
2206 					  struct iwl_mvm_int_sta *sta,
2207 					  u16 *queue, int fifo)
2208 {
2209 	int ret;
2210 
2211 	/* Map queue to fifo - needs to happen before adding station */
2212 	if (!iwl_mvm_has_new_tx_api(mvm))
2213 		iwl_mvm_enable_aux_snif_queue(mvm, *queue, sta->sta_id, fifo);
2214 
2215 	ret = iwl_mvm_add_int_sta_common(mvm, sta, addr, macidx, maccolor);
2216 	if (ret) {
2217 		if (!iwl_mvm_has_new_tx_api(mvm))
2218 			iwl_mvm_disable_txq(mvm, NULL, sta->sta_id, queue,
2219 					    IWL_MAX_TID_COUNT);
2220 		return ret;
2221 	}
2222 
2223 	/*
2224 	 * For 22000 firmware and on we cannot add queue to a station unknown
2225 	 * to firmware so enable queue here - after the station was added
2226 	 */
2227 	if (iwl_mvm_has_new_tx_api(mvm)) {
2228 		int txq;
2229 
2230 		txq = iwl_mvm_enable_aux_snif_queue_tvqm(mvm, sta->sta_id);
2231 		if (txq < 0) {
2232 			iwl_mvm_rm_sta_common(mvm, sta->sta_id);
2233 			return txq;
2234 		}
2235 
2236 		*queue = txq;
2237 	}
2238 
2239 	return 0;
2240 }
2241 
2242 int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm, u32 lmac_id)
2243 {
2244 	int ret;
2245 	u32 qmask = mvm->aux_queue == IWL_MVM_INVALID_QUEUE ? 0 :
2246 		BIT(mvm->aux_queue);
2247 
2248 	lockdep_assert_held(&mvm->mutex);
2249 
2250 	/* Allocate aux station and assign to it the aux queue */
2251 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, qmask,
2252 				       NL80211_IFTYPE_UNSPECIFIED,
2253 				       IWL_STA_AUX_ACTIVITY);
2254 	if (ret)
2255 		return ret;
2256 
2257 	/*
2258 	 * In CDB NICs we need to specify which lmac to use for aux activity
2259 	 * using the mac_id argument place to send lmac_id to the function
2260 	 */
2261 	ret = iwl_mvm_add_int_sta_with_queue(mvm, lmac_id, 0, NULL,
2262 					     &mvm->aux_sta, &mvm->aux_queue,
2263 					     IWL_MVM_TX_FIFO_MCAST);
2264 	if (ret) {
2265 		iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2266 		return ret;
2267 	}
2268 
2269 	return 0;
2270 }
2271 
2272 int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2273 {
2274 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2275 
2276 	lockdep_assert_held(&mvm->mutex);
2277 
2278 	return iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
2279 					      NULL, &mvm->snif_sta,
2280 					      &mvm->snif_queue,
2281 					      IWL_MVM_TX_FIFO_BE);
2282 }
2283 
2284 int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2285 {
2286 	int ret;
2287 
2288 	lockdep_assert_held(&mvm->mutex);
2289 
2290 	if (WARN_ON_ONCE(mvm->snif_sta.sta_id == IWL_INVALID_STA))
2291 		return -EINVAL;
2292 
2293 	iwl_mvm_disable_txq(mvm, NULL, mvm->snif_sta.sta_id,
2294 			    &mvm->snif_queue, IWL_MAX_TID_COUNT);
2295 	ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
2296 	if (ret)
2297 		IWL_WARN(mvm, "Failed sending remove station\n");
2298 
2299 	return ret;
2300 }
2301 
2302 int iwl_mvm_rm_aux_sta(struct iwl_mvm *mvm)
2303 {
2304 	int ret;
2305 
2306 	lockdep_assert_held(&mvm->mutex);
2307 
2308 	if (WARN_ON_ONCE(mvm->aux_sta.sta_id == IWL_INVALID_STA))
2309 		return -EINVAL;
2310 
2311 	iwl_mvm_disable_txq(mvm, NULL, mvm->aux_sta.sta_id,
2312 			    &mvm->aux_queue, IWL_MAX_TID_COUNT);
2313 	ret = iwl_mvm_rm_sta_common(mvm, mvm->aux_sta.sta_id);
2314 	if (ret)
2315 		IWL_WARN(mvm, "Failed sending remove station\n");
2316 	iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2317 
2318 	return ret;
2319 }
2320 
2321 void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
2322 {
2323 	iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
2324 }
2325 
2326 /*
2327  * Send the add station command for the vif's broadcast station.
2328  * Assumes that the station was already allocated.
2329  *
2330  * @mvm: the mvm component
2331  * @vif: the interface to which the broadcast station is added
2332  * @bsta: the broadcast station to add.
2333  */
2334 int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2335 {
2336 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2337 	struct iwl_mvm_int_sta *bsta = &mvmvif->deflink.bcast_sta;
2338 	static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2339 	const u8 *baddr = _baddr;
2340 	int queue;
2341 	int ret;
2342 	unsigned int wdg_timeout =
2343 		iwl_mvm_get_wd_timeout(mvm, vif);
2344 	struct iwl_trans_txq_scd_cfg cfg = {
2345 		.fifo = IWL_MVM_TX_FIFO_VO,
2346 		.sta_id = mvmvif->deflink.bcast_sta.sta_id,
2347 		.tid = IWL_MAX_TID_COUNT,
2348 		.aggregate = false,
2349 		.frame_limit = IWL_FRAME_LIMIT,
2350 	};
2351 
2352 	lockdep_assert_held(&mvm->mutex);
2353 
2354 	if (!iwl_mvm_has_new_tx_api(mvm)) {
2355 		if (vif->type == NL80211_IFTYPE_AP ||
2356 		    vif->type == NL80211_IFTYPE_ADHOC) {
2357 			queue = mvm->probe_queue;
2358 		} else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2359 			queue = mvm->p2p_dev_queue;
2360 		} else {
2361 			WARN(1, "Missing required TXQ for adding bcast STA\n");
2362 			return -EINVAL;
2363 		}
2364 
2365 		bsta->tfd_queue_msk |= BIT(queue);
2366 
2367 		iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2368 	}
2369 
2370 	if (vif->type == NL80211_IFTYPE_ADHOC)
2371 		baddr = vif->bss_conf.bssid;
2372 
2373 	if (WARN_ON_ONCE(bsta->sta_id == IWL_INVALID_STA))
2374 		return -ENOSPC;
2375 
2376 	ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2377 					 mvmvif->id, mvmvif->color);
2378 	if (ret)
2379 		return ret;
2380 
2381 	/*
2382 	 * For 22000 firmware and on we cannot add queue to a station unknown
2383 	 * to firmware so enable queue here - after the station was added
2384 	 */
2385 	if (iwl_mvm_has_new_tx_api(mvm)) {
2386 		queue = iwl_mvm_tvqm_enable_txq(mvm, NULL, bsta->sta_id,
2387 						IWL_MAX_TID_COUNT,
2388 						wdg_timeout);
2389 		if (queue < 0) {
2390 			iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
2391 			return queue;
2392 		}
2393 
2394 		if (vif->type == NL80211_IFTYPE_AP ||
2395 		    vif->type == NL80211_IFTYPE_ADHOC) {
2396 			/* for queue management */
2397 			mvm->probe_queue = queue;
2398 			/* for use in TX */
2399 			mvmvif->deflink.mgmt_queue = queue;
2400 		} else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2401 			mvm->p2p_dev_queue = queue;
2402 		}
2403 	} else if (vif->type == NL80211_IFTYPE_AP ||
2404 		   vif->type == NL80211_IFTYPE_ADHOC) {
2405 		/* set it for use in TX */
2406 		mvmvif->deflink.mgmt_queue = mvm->probe_queue;
2407 	}
2408 
2409 	return 0;
2410 }
2411 
2412 void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2413 				   struct ieee80211_vif *vif)
2414 {
2415 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2416 	u16 *queueptr, queue;
2417 
2418 	lockdep_assert_held(&mvm->mutex);
2419 
2420 	iwl_mvm_flush_sta(mvm, mvmvif->deflink.bcast_sta.sta_id,
2421 			  mvmvif->deflink.bcast_sta.tfd_queue_msk);
2422 
2423 	switch (vif->type) {
2424 	case NL80211_IFTYPE_AP:
2425 	case NL80211_IFTYPE_ADHOC:
2426 		queueptr = &mvm->probe_queue;
2427 		break;
2428 	case NL80211_IFTYPE_P2P_DEVICE:
2429 		queueptr = &mvm->p2p_dev_queue;
2430 		break;
2431 	default:
2432 		WARN(1, "Can't free bcast queue on vif type %d\n",
2433 		     vif->type);
2434 		return;
2435 	}
2436 
2437 	queue = *queueptr;
2438 	iwl_mvm_disable_txq(mvm, NULL, mvmvif->deflink.bcast_sta.sta_id,
2439 			    queueptr, IWL_MAX_TID_COUNT);
2440 
2441 	if (vif->type == NL80211_IFTYPE_AP || vif->type == NL80211_IFTYPE_ADHOC)
2442 		mvmvif->deflink.mgmt_queue = mvm->probe_queue;
2443 
2444 	if (iwl_mvm_has_new_tx_api(mvm))
2445 		return;
2446 
2447 	WARN_ON(!(mvmvif->deflink.bcast_sta.tfd_queue_msk & BIT(queue)));
2448 	mvmvif->deflink.bcast_sta.tfd_queue_msk &= ~BIT(queue);
2449 }
2450 
2451 /* Send the FW a request to remove the station from its internal data
2452  * structures, but DO NOT remove the entry from the local data structures. */
2453 int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2454 {
2455 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2456 	int ret;
2457 
2458 	lockdep_assert_held(&mvm->mutex);
2459 
2460 	iwl_mvm_free_bcast_sta_queues(mvm, vif);
2461 
2462 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->deflink.bcast_sta.sta_id);
2463 	if (ret)
2464 		IWL_WARN(mvm, "Failed sending remove station\n");
2465 	return ret;
2466 }
2467 
2468 int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2469 {
2470 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2471 
2472 	lockdep_assert_held(&mvm->mutex);
2473 
2474 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->deflink.bcast_sta, 0,
2475 					ieee80211_vif_type_p2p(vif),
2476 					IWL_STA_GENERAL_PURPOSE);
2477 }
2478 
2479 /* Allocate a new station entry for the broadcast station to the given vif,
2480  * and send it to the FW.
2481  * Note that each P2P mac should have its own broadcast station.
2482  *
2483  * @mvm: the mvm component
2484  * @vif: the interface to which the broadcast station is added
2485  * @bsta: the broadcast station to add. */
2486 int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2487 {
2488 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2489 	struct iwl_mvm_int_sta *bsta = &mvmvif->deflink.bcast_sta;
2490 	int ret;
2491 
2492 	lockdep_assert_held(&mvm->mutex);
2493 
2494 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2495 	if (ret)
2496 		return ret;
2497 
2498 	ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2499 
2500 	if (ret)
2501 		iwl_mvm_dealloc_int_sta(mvm, bsta);
2502 
2503 	return ret;
2504 }
2505 
2506 void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2507 {
2508 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2509 
2510 	iwl_mvm_dealloc_int_sta(mvm, &mvmvif->deflink.bcast_sta);
2511 }
2512 
2513 /*
2514  * Send the FW a request to remove the station from its internal data
2515  * structures, and in addition remove it from the local data structure.
2516  */
2517 int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2518 {
2519 	int ret;
2520 
2521 	lockdep_assert_held(&mvm->mutex);
2522 
2523 	ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2524 
2525 	iwl_mvm_dealloc_bcast_sta(mvm, vif);
2526 
2527 	return ret;
2528 }
2529 
2530 /*
2531  * Allocate a new station entry for the multicast station to the given vif,
2532  * and send it to the FW.
2533  * Note that each AP/GO mac should have its own multicast station.
2534  *
2535  * @mvm: the mvm component
2536  * @vif: the interface to which the multicast station is added
2537  */
2538 int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2539 {
2540 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2541 	struct iwl_mvm_int_sta *msta = &mvmvif->deflink.mcast_sta;
2542 	static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
2543 	const u8 *maddr = _maddr;
2544 	struct iwl_trans_txq_scd_cfg cfg = {
2545 		.fifo = vif->type == NL80211_IFTYPE_AP ?
2546 			IWL_MVM_TX_FIFO_MCAST : IWL_MVM_TX_FIFO_BE,
2547 		.sta_id = msta->sta_id,
2548 		.tid = 0,
2549 		.aggregate = false,
2550 		.frame_limit = IWL_FRAME_LIMIT,
2551 	};
2552 	unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif);
2553 	int ret;
2554 
2555 	lockdep_assert_held(&mvm->mutex);
2556 
2557 	if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2558 		    vif->type != NL80211_IFTYPE_ADHOC))
2559 		return -EOPNOTSUPP;
2560 
2561 	/*
2562 	 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2563 	 * invalid, so make sure we use the queue we want.
2564 	 * Note that this is done here as we want to avoid making DQA
2565 	 * changes in mac80211 layer.
2566 	 */
2567 	if (vif->type == NL80211_IFTYPE_ADHOC)
2568 		mvmvif->deflink.cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2569 
2570 	/*
2571 	 * While in previous FWs we had to exclude cab queue from TFD queue
2572 	 * mask, now it is needed as any other queue.
2573 	 */
2574 	if (!iwl_mvm_has_new_tx_api(mvm) &&
2575 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2576 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->deflink.cab_queue, 0,
2577 				   &cfg,
2578 				   timeout);
2579 		msta->tfd_queue_msk |= BIT(mvmvif->deflink.cab_queue);
2580 	}
2581 	ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
2582 					 mvmvif->id, mvmvif->color);
2583 	if (ret)
2584 		goto err;
2585 
2586 	/*
2587 	 * Enable cab queue after the ADD_STA command is sent.
2588 	 * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
2589 	 * command with unknown station id, and for FW that doesn't support
2590 	 * station API since the cab queue is not included in the
2591 	 * tfd_queue_mask.
2592 	 */
2593 	if (iwl_mvm_has_new_tx_api(mvm)) {
2594 		int queue = iwl_mvm_tvqm_enable_txq(mvm, NULL, msta->sta_id,
2595 						    0, timeout);
2596 		if (queue < 0) {
2597 			ret = queue;
2598 			goto err;
2599 		}
2600 		mvmvif->deflink.cab_queue = queue;
2601 	} else if (!fw_has_api(&mvm->fw->ucode_capa,
2602 			       IWL_UCODE_TLV_API_STA_TYPE))
2603 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->deflink.cab_queue, 0,
2604 				   &cfg,
2605 				   timeout);
2606 
2607 	return 0;
2608 err:
2609 	iwl_mvm_dealloc_int_sta(mvm, msta);
2610 	return ret;
2611 }
2612 
2613 static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
2614 				    struct ieee80211_key_conf *keyconf,
2615 				    bool mcast)
2616 {
2617 	union {
2618 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
2619 		struct iwl_mvm_add_sta_key_cmd cmd;
2620 	} u = {};
2621 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
2622 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
2623 	__le16 key_flags;
2624 	int ret, size;
2625 	u32 status;
2626 
2627 	/* This is a valid situation for GTK removal */
2628 	if (sta_id == IWL_INVALID_STA)
2629 		return 0;
2630 
2631 	key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2632 				 STA_KEY_FLG_KEYID_MSK);
2633 	key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2634 	key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2635 
2636 	if (mcast)
2637 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2638 
2639 	/*
2640 	 * The fields assigned here are in the same location at the start
2641 	 * of the command, so we can do this union trick.
2642 	 */
2643 	u.cmd.common.key_flags = key_flags;
2644 	u.cmd.common.key_offset = keyconf->hw_key_idx;
2645 	u.cmd.common.sta_id = sta_id;
2646 
2647 	size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
2648 
2649 	status = ADD_STA_SUCCESS;
2650 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
2651 					  &status);
2652 
2653 	switch (status) {
2654 	case ADD_STA_SUCCESS:
2655 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2656 		break;
2657 	default:
2658 		ret = -EIO;
2659 		IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2660 		break;
2661 	}
2662 
2663 	return ret;
2664 }
2665 
2666 /*
2667  * Send the FW a request to remove the station from its internal data
2668  * structures, and in addition remove it from the local data structure.
2669  */
2670 int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2671 {
2672 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2673 	int ret;
2674 
2675 	lockdep_assert_held(&mvm->mutex);
2676 
2677 	iwl_mvm_flush_sta(mvm, mvmvif->deflink.mcast_sta.sta_id,
2678 			  mvmvif->deflink.mcast_sta.tfd_queue_msk);
2679 
2680 	iwl_mvm_disable_txq(mvm, NULL, mvmvif->deflink.mcast_sta.sta_id,
2681 			    &mvmvif->deflink.cab_queue, 0);
2682 
2683 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->deflink.mcast_sta.sta_id);
2684 	if (ret)
2685 		IWL_WARN(mvm, "Failed sending remove station\n");
2686 
2687 	return ret;
2688 }
2689 
2690 static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
2691 {
2692 	struct iwl_mvm_delba_data notif = {
2693 		.baid = baid,
2694 	};
2695 
2696 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_NOTIF_DEL_BA, true,
2697 					&notif, sizeof(notif));
2698 };
2699 
2700 static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2701 				 struct iwl_mvm_baid_data *data)
2702 {
2703 	int i;
2704 
2705 	iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2706 
2707 	for (i = 0; i < mvm->trans->info.num_rxqs; i++) {
2708 		int j;
2709 		struct iwl_mvm_reorder_buffer *reorder_buf =
2710 			&data->reorder_buf[i];
2711 		struct iwl_mvm_reorder_buf_entry *entries =
2712 			&data->entries[i * data->entries_per_queue];
2713 
2714 		spin_lock_bh(&reorder_buf->lock);
2715 		if (likely(!reorder_buf->num_stored)) {
2716 			spin_unlock_bh(&reorder_buf->lock);
2717 			continue;
2718 		}
2719 
2720 		/*
2721 		 * This shouldn't happen in regular DELBA since the internal
2722 		 * delBA notification should trigger a release of all frames in
2723 		 * the reorder buffer.
2724 		 */
2725 		WARN_ON(1);
2726 
2727 		for (j = 0; j < data->buf_size; j++)
2728 			__skb_queue_purge(&entries[j].frames);
2729 
2730 		spin_unlock_bh(&reorder_buf->lock);
2731 	}
2732 }
2733 
2734 static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2735 					struct iwl_mvm_baid_data *data,
2736 					u16 ssn)
2737 {
2738 	int i;
2739 
2740 	for (i = 0; i < mvm->trans->info.num_rxqs; i++) {
2741 		struct iwl_mvm_reorder_buffer *reorder_buf =
2742 			&data->reorder_buf[i];
2743 		struct iwl_mvm_reorder_buf_entry *entries =
2744 			&data->entries[i * data->entries_per_queue];
2745 		int j;
2746 
2747 		reorder_buf->num_stored = 0;
2748 		reorder_buf->head_sn = ssn;
2749 		spin_lock_init(&reorder_buf->lock);
2750 		reorder_buf->queue = i;
2751 		reorder_buf->valid = false;
2752 		for (j = 0; j < data->buf_size; j++)
2753 			__skb_queue_head_init(&entries[j].frames);
2754 	}
2755 }
2756 
2757 static int iwl_mvm_fw_baid_op_sta(struct iwl_mvm *mvm,
2758 				  struct ieee80211_sta *sta,
2759 				  bool start, int tid, u16 ssn,
2760 				  u16 buf_size)
2761 {
2762 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2763 	struct iwl_mvm_add_sta_cmd cmd = {
2764 		.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
2765 		.sta_id = mvm_sta->deflink.sta_id,
2766 		.add_modify = STA_MODE_MODIFY,
2767 	};
2768 	u32 status;
2769 	int ret;
2770 
2771 	if (start) {
2772 		cmd.add_immediate_ba_tid = tid;
2773 		cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
2774 		cmd.rx_ba_window = cpu_to_le16(buf_size);
2775 		cmd.modify_mask = STA_MODIFY_ADD_BA_TID;
2776 	} else {
2777 		cmd.remove_immediate_ba_tid = tid;
2778 		cmd.modify_mask = STA_MODIFY_REMOVE_BA_TID;
2779 	}
2780 
2781 	status = ADD_STA_SUCCESS;
2782 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2783 					  iwl_mvm_add_sta_cmd_size(mvm),
2784 					  &cmd, &status);
2785 	if (ret)
2786 		return ret;
2787 
2788 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2789 	case ADD_STA_SUCCESS:
2790 		IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2791 			     start ? "start" : "stopp");
2792 		if (WARN_ON(start && iwl_mvm_has_new_rx_api(mvm) &&
2793 			    !(status & IWL_ADD_STA_BAID_VALID_MASK)))
2794 			return -EINVAL;
2795 		ret = u32_get_bits(status, IWL_ADD_STA_BAID_MASK);
2796 		if (IWL_FW_CHECK(mvm, start && ret >= ARRAY_SIZE(mvm->baid_map),
2797 				 "invalid BAID %d from FW\n", ret))
2798 			return -EINVAL;
2799 		return ret;
2800 	case ADD_STA_IMMEDIATE_BA_FAILURE:
2801 		IWL_WARN(mvm, "RX BA Session refused by fw\n");
2802 		return -ENOSPC;
2803 	default:
2804 		IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2805 			start ? "start" : "stopp", status);
2806 		return -EIO;
2807 	}
2808 }
2809 
2810 static int iwl_mvm_fw_baid_op_cmd(struct iwl_mvm *mvm,
2811 				  struct ieee80211_sta *sta,
2812 				  bool start, int tid, u16 ssn,
2813 				  u16 buf_size, int baid)
2814 {
2815 	struct iwl_rx_baid_cfg_cmd cmd = {
2816 		.action = start ? cpu_to_le32(IWL_RX_BAID_ACTION_ADD) :
2817 				  cpu_to_le32(IWL_RX_BAID_ACTION_REMOVE),
2818 	};
2819 	struct iwl_host_cmd hcmd = {
2820 		.id = WIDE_ID(DATA_PATH_GROUP, RX_BAID_ALLOCATION_CONFIG_CMD),
2821 		.flags = CMD_SEND_IN_RFKILL,
2822 		.len[0] = sizeof(cmd),
2823 		.data[0] = &cmd,
2824 	};
2825 	int ret;
2826 
2827 	BUILD_BUG_ON(sizeof(struct iwl_rx_baid_cfg_resp) != sizeof(baid));
2828 
2829 	if (start) {
2830 		cmd.alloc.sta_id_mask =
2831 			cpu_to_le32(iwl_mvm_sta_fw_id_mask(mvm, sta, -1));
2832 		cmd.alloc.tid = tid;
2833 		cmd.alloc.ssn = cpu_to_le16(ssn);
2834 		cmd.alloc.win_size = cpu_to_le16(buf_size);
2835 		baid = -EIO;
2836 	} else if (iwl_fw_lookup_cmd_ver(mvm->fw, hcmd.id, 1) == 1) {
2837 		cmd.remove_v1.baid = cpu_to_le32(baid);
2838 		BUILD_BUG_ON(sizeof(cmd.remove_v1) > sizeof(cmd.remove));
2839 	} else {
2840 		cmd.remove.sta_id_mask =
2841 			cpu_to_le32(iwl_mvm_sta_fw_id_mask(mvm, sta, -1));
2842 		cmd.remove.tid = cpu_to_le32(tid);
2843 	}
2844 
2845 	ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &baid);
2846 	if (ret)
2847 		return ret;
2848 
2849 	if (!start) {
2850 		/* ignore firmware baid on remove */
2851 		baid = 0;
2852 	}
2853 
2854 	IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2855 		     start ? "start" : "stopp");
2856 
2857 	if (baid < 0 || baid >= ARRAY_SIZE(mvm->baid_map))
2858 		return -EINVAL;
2859 
2860 	return baid;
2861 }
2862 
2863 static int iwl_mvm_fw_baid_op(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2864 			      bool start, int tid, u16 ssn, u16 buf_size,
2865 			      int baid)
2866 {
2867 	if (fw_has_capa(&mvm->fw->ucode_capa,
2868 			IWL_UCODE_TLV_CAPA_BAID_ML_SUPPORT))
2869 		return iwl_mvm_fw_baid_op_cmd(mvm, sta, start,
2870 					      tid, ssn, buf_size, baid);
2871 
2872 	return iwl_mvm_fw_baid_op_sta(mvm, sta, start,
2873 				      tid, ssn, buf_size);
2874 }
2875 
2876 int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2877 		       int tid, u16 ssn, bool start, u16 buf_size, u16 timeout)
2878 {
2879 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2880 	struct iwl_mvm_baid_data *baid_data = NULL;
2881 	int ret, baid;
2882 	u32 max_ba_id_sessions = iwl_mvm_has_new_tx_api(mvm) ? IWL_MAX_BAID :
2883 							       IWL_MAX_BAID_OLD;
2884 
2885 	lockdep_assert_held(&mvm->mutex);
2886 
2887 	if (start && mvm->rx_ba_sessions >= max_ba_id_sessions) {
2888 		IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2889 		return -ENOSPC;
2890 	}
2891 
2892 	if (iwl_mvm_has_new_rx_api(mvm) && start) {
2893 		u32 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2894 
2895 		/* sparse doesn't like the __align() so don't check */
2896 #ifndef __CHECKER__
2897 		/*
2898 		 * The division below will be OK if either the cache line size
2899 		 * can be divided by the entry size (ALIGN will round up) or if
2900 		 * the entry size can be divided by the cache line size, in
2901 		 * which case the ALIGN() will do nothing.
2902 		 */
2903 		BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2904 			     sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2905 #endif
2906 
2907 		/*
2908 		 * Upward align the reorder buffer size to fill an entire cache
2909 		 * line for each queue, to avoid sharing cache lines between
2910 		 * different queues.
2911 		 */
2912 		reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2913 
2914 		/*
2915 		 * Allocate here so if allocation fails we can bail out early
2916 		 * before starting the BA session in the firmware
2917 		 */
2918 		baid_data = kzalloc(sizeof(*baid_data) +
2919 				    mvm->trans->info.num_rxqs *
2920 				    reorder_buf_size,
2921 				    GFP_KERNEL);
2922 		if (!baid_data)
2923 			return -ENOMEM;
2924 
2925 		/*
2926 		 * This division is why we need the above BUILD_BUG_ON(),
2927 		 * if that doesn't hold then this will not be right.
2928 		 */
2929 		baid_data->entries_per_queue =
2930 			reorder_buf_size / sizeof(baid_data->entries[0]);
2931 	}
2932 
2933 	if (iwl_mvm_has_new_rx_api(mvm) && !start) {
2934 		baid = mvm_sta->tid_to_baid[tid];
2935 	} else {
2936 		/* we don't really need it in this case */
2937 		baid = -1;
2938 	}
2939 
2940 	/* Don't send command to remove (start=0) BAID during restart */
2941 	if (start || !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
2942 		baid = iwl_mvm_fw_baid_op(mvm, sta, start, tid, ssn, buf_size,
2943 					  baid);
2944 
2945 	if (baid < 0) {
2946 		ret = baid;
2947 		goto out_free;
2948 	}
2949 
2950 	if (start) {
2951 		mvm->rx_ba_sessions++;
2952 
2953 		if (!iwl_mvm_has_new_rx_api(mvm))
2954 			return 0;
2955 
2956 		baid_data->baid = baid;
2957 		baid_data->timeout = timeout;
2958 		baid_data->last_rx = jiffies;
2959 		baid_data->rcu_ptr = &mvm->baid_map[baid];
2960 		timer_setup(&baid_data->session_timer,
2961 			    iwl_mvm_rx_agg_session_expired, 0);
2962 		baid_data->mvm = mvm;
2963 		baid_data->tid = tid;
2964 		baid_data->sta_mask = iwl_mvm_sta_fw_id_mask(mvm, sta, -1);
2965 		baid_data->buf_size = buf_size;
2966 
2967 		mvm_sta->tid_to_baid[tid] = baid;
2968 		if (timeout)
2969 			mod_timer(&baid_data->session_timer,
2970 				  TU_TO_EXP_TIME(timeout * 2));
2971 
2972 		iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn);
2973 		/*
2974 		 * protect the BA data with RCU to cover a case where our
2975 		 * internal RX sync mechanism will timeout (not that it's
2976 		 * supposed to happen) and we will free the session data while
2977 		 * RX is being processed in parallel
2978 		 */
2979 		IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2980 			     mvm_sta->deflink.sta_id, tid, baid);
2981 		WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2982 		rcu_assign_pointer(mvm->baid_map[baid], baid_data);
2983 	} else  {
2984 		baid = mvm_sta->tid_to_baid[tid];
2985 
2986 		if (mvm->rx_ba_sessions > 0)
2987 			/* check that restart flow didn't zero the counter */
2988 			mvm->rx_ba_sessions--;
2989 		if (!iwl_mvm_has_new_rx_api(mvm))
2990 			return 0;
2991 
2992 		if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2993 			return -EINVAL;
2994 
2995 		baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2996 		if (WARN_ON(!baid_data))
2997 			return -EINVAL;
2998 
2999 		/* synchronize all rx queues so we can safely delete */
3000 		iwl_mvm_free_reorder(mvm, baid_data);
3001 		timer_shutdown_sync(&baid_data->session_timer);
3002 		RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
3003 		kfree_rcu(baid_data, rcu_head);
3004 		IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
3005 	}
3006 	return 0;
3007 
3008 out_free:
3009 	kfree(baid_data);
3010 	return ret;
3011 }
3012 
3013 int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
3014 		       int tid, u8 queue, bool start)
3015 {
3016 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3017 	struct iwl_mvm_add_sta_cmd cmd = {};
3018 	int ret;
3019 	u32 status;
3020 
3021 	lockdep_assert_held(&mvm->mutex);
3022 
3023 	if (start) {
3024 		mvm_sta->tfd_queue_msk |= BIT(queue);
3025 		mvm_sta->tid_disable_agg &= ~BIT(tid);
3026 	} else {
3027 		/* In DQA-mode the queue isn't removed on agg termination */
3028 		mvm_sta->tid_disable_agg |= BIT(tid);
3029 	}
3030 
3031 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
3032 	cmd.sta_id = mvm_sta->deflink.sta_id;
3033 	cmd.add_modify = STA_MODE_MODIFY;
3034 	if (!iwl_mvm_has_new_tx_api(mvm))
3035 		cmd.modify_mask = STA_MODIFY_QUEUES;
3036 	cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
3037 	cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
3038 	cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
3039 
3040 	status = ADD_STA_SUCCESS;
3041 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
3042 					  iwl_mvm_add_sta_cmd_size(mvm),
3043 					  &cmd, &status);
3044 	if (ret)
3045 		return ret;
3046 
3047 	switch (status & IWL_ADD_STA_STATUS_MASK) {
3048 	case ADD_STA_SUCCESS:
3049 		break;
3050 	default:
3051 		ret = -EIO;
3052 		IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
3053 			start ? "start" : "stopp", status);
3054 		break;
3055 	}
3056 
3057 	return ret;
3058 }
3059 
3060 const u8 tid_to_mac80211_ac[] = {
3061 	IEEE80211_AC_BE,
3062 	IEEE80211_AC_BK,
3063 	IEEE80211_AC_BK,
3064 	IEEE80211_AC_BE,
3065 	IEEE80211_AC_VI,
3066 	IEEE80211_AC_VI,
3067 	IEEE80211_AC_VO,
3068 	IEEE80211_AC_VO,
3069 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
3070 };
3071 
3072 static const u8 tid_to_ucode_ac[] = {
3073 	AC_BE,
3074 	AC_BK,
3075 	AC_BK,
3076 	AC_BE,
3077 	AC_VI,
3078 	AC_VI,
3079 	AC_VO,
3080 	AC_VO,
3081 };
3082 
3083 int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3084 			     struct ieee80211_sta *sta, u16 tid, u16 *ssn)
3085 {
3086 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3087 	struct iwl_mvm_tid_data *tid_data;
3088 	u16 normalized_ssn;
3089 	u16 txq_id;
3090 	int ret;
3091 
3092 	if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
3093 		return -EINVAL;
3094 
3095 	if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
3096 	    mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
3097 		IWL_ERR(mvm,
3098 			"Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
3099 			mvmsta->tid_data[tid].state);
3100 		return -ENXIO;
3101 	}
3102 
3103 	lockdep_assert_held(&mvm->mutex);
3104 
3105 	if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE &&
3106 	    iwl_mvm_has_new_tx_api(mvm)) {
3107 		u8 ac = tid_to_mac80211_ac[tid];
3108 
3109 		ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
3110 		if (ret)
3111 			return ret;
3112 	}
3113 
3114 	spin_lock_bh(&mvmsta->lock);
3115 
3116 	/*
3117 	 * Note the possible cases:
3118 	 *  1. An enabled TXQ - TXQ needs to become agg'ed
3119 	 *  2. The TXQ hasn't yet been enabled, so find a free one and mark
3120 	 *	it as reserved
3121 	 */
3122 	txq_id = mvmsta->tid_data[tid].txq_id;
3123 	if (txq_id == IWL_MVM_INVALID_QUEUE) {
3124 		ret = iwl_mvm_find_free_queue(mvm, mvmsta->deflink.sta_id,
3125 					      IWL_MVM_DQA_MIN_DATA_QUEUE,
3126 					      IWL_MVM_DQA_MAX_DATA_QUEUE);
3127 		if (ret < 0) {
3128 			IWL_ERR(mvm, "Failed to allocate agg queue\n");
3129 			goto out;
3130 		}
3131 
3132 		txq_id = ret;
3133 
3134 		/* TXQ hasn't yet been enabled, so mark it only as reserved */
3135 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
3136 	} else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) {
3137 		ret = -ENXIO;
3138 		IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n",
3139 			tid, IWL_MAX_HW_QUEUES - 1);
3140 		goto out;
3141 
3142 	} else if (unlikely(mvm->queue_info[txq_id].status ==
3143 			    IWL_MVM_QUEUE_SHARED)) {
3144 		ret = -ENXIO;
3145 		IWL_DEBUG_TX_QUEUES(mvm,
3146 				    "Can't start tid %d agg on shared queue!\n",
3147 				    tid);
3148 		goto out;
3149 	}
3150 
3151 	IWL_DEBUG_TX_QUEUES(mvm,
3152 			    "AGG for tid %d will be on queue #%d\n",
3153 			    tid, txq_id);
3154 
3155 	tid_data = &mvmsta->tid_data[tid];
3156 	tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3157 	tid_data->txq_id = txq_id;
3158 	*ssn = tid_data->ssn;
3159 
3160 	IWL_DEBUG_TX_QUEUES(mvm,
3161 			    "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
3162 			    mvmsta->deflink.sta_id, tid, txq_id,
3163 			    tid_data->ssn,
3164 			    tid_data->next_reclaimed);
3165 
3166 	/*
3167 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
3168 	 * to align the wrap around of ssn so we compare relevant values.
3169 	 */
3170 	normalized_ssn = tid_data->ssn;
3171 	if (mvm->trans->mac_cfg->gen2)
3172 		normalized_ssn &= 0xff;
3173 
3174 	if (normalized_ssn == tid_data->next_reclaimed) {
3175 		tid_data->state = IWL_AGG_STARTING;
3176 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
3177 	} else {
3178 		tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
3179 		ret = IEEE80211_AMPDU_TX_START_DELAY_ADDBA;
3180 	}
3181 
3182 out:
3183 	spin_unlock_bh(&mvmsta->lock);
3184 
3185 	return ret;
3186 }
3187 
3188 int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3189 			    struct ieee80211_sta *sta, u16 tid, u16 buf_size,
3190 			    bool amsdu)
3191 {
3192 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3193 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3194 	unsigned int wdg_timeout =
3195 		iwl_mvm_get_wd_timeout(mvm, vif);
3196 	int queue, ret;
3197 	bool alloc_queue = true;
3198 	enum iwl_mvm_queue_status queue_status;
3199 	u16 ssn;
3200 
3201 	struct iwl_trans_txq_scd_cfg cfg = {
3202 		.sta_id = mvmsta->deflink.sta_id,
3203 		.tid = tid,
3204 		.frame_limit = buf_size,
3205 		.aggregate = true,
3206 	};
3207 
3208 	/*
3209 	 * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
3210 	 * manager, so this function should never be called in this case.
3211 	 */
3212 	if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
3213 		return -EINVAL;
3214 
3215 	BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
3216 		     != IWL_MAX_TID_COUNT);
3217 
3218 	spin_lock_bh(&mvmsta->lock);
3219 	ssn = tid_data->ssn;
3220 	queue = tid_data->txq_id;
3221 	tid_data->state = IWL_AGG_ON;
3222 	mvmsta->agg_tids |= BIT(tid);
3223 	tid_data->ssn = 0xffff;
3224 	tid_data->amsdu_in_ampdu_allowed = amsdu;
3225 	spin_unlock_bh(&mvmsta->lock);
3226 
3227 	if (iwl_mvm_has_new_tx_api(mvm)) {
3228 		/*
3229 		 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
3230 		 * would have failed, so if we are here there is no need to
3231 		 * allocate a queue.
3232 		 * However, if aggregation size is different than the default
3233 		 * size, the scheduler should be reconfigured.
3234 		 * We cannot do this with the new TX API, so return unsupported
3235 		 * for now, until it will be offloaded to firmware..
3236 		 * Note that if SCD default value changes - this condition
3237 		 * should be updated as well.
3238 		 */
3239 		if (buf_size < IWL_FRAME_LIMIT)
3240 			return -EOPNOTSUPP;
3241 
3242 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
3243 		if (ret)
3244 			return -EIO;
3245 		goto out;
3246 	}
3247 
3248 	cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
3249 
3250 	queue_status = mvm->queue_info[queue].status;
3251 
3252 	/* Maybe there is no need to even alloc a queue... */
3253 	if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
3254 		alloc_queue = false;
3255 
3256 	/*
3257 	 * Only reconfig the SCD for the queue if the window size has
3258 	 * changed from current (become smaller)
3259 	 */
3260 	if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
3261 		/*
3262 		 * If reconfiguring an existing queue, it first must be
3263 		 * drained
3264 		 */
3265 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
3266 						     BIT(queue));
3267 		if (ret) {
3268 			IWL_ERR(mvm,
3269 				"Error draining queue before reconfig\n");
3270 			return ret;
3271 		}
3272 
3273 		ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
3274 					   mvmsta->deflink.sta_id, tid,
3275 					   buf_size, ssn);
3276 		if (ret) {
3277 			IWL_ERR(mvm,
3278 				"Error reconfiguring TXQ #%d\n", queue);
3279 			return ret;
3280 		}
3281 	}
3282 
3283 	if (alloc_queue)
3284 		iwl_mvm_enable_txq(mvm, sta, queue, ssn,
3285 				   &cfg, wdg_timeout);
3286 
3287 	/* Send ADD_STA command to enable aggs only if the queue isn't shared */
3288 	if (queue_status != IWL_MVM_QUEUE_SHARED) {
3289 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
3290 		if (ret)
3291 			return -EIO;
3292 	}
3293 
3294 	/* No need to mark as reserved */
3295 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
3296 
3297 out:
3298 	/*
3299 	 * Even though in theory the peer could have different
3300 	 * aggregation reorder buffer sizes for different sessions,
3301 	 * our ucode doesn't allow for that and has a global limit
3302 	 * for each station. Therefore, use the minimum of all the
3303 	 * aggregation sessions and our default value.
3304 	 */
3305 	mvmsta->deflink.lq_sta.rs_drv.pers.max_agg_bufsize =
3306 		min(mvmsta->deflink.lq_sta.rs_drv.pers.max_agg_bufsize,
3307 		    buf_size);
3308 	mvmsta->deflink.lq_sta.rs_drv.lq.agg_frame_cnt_limit =
3309 		mvmsta->deflink.lq_sta.rs_drv.pers.max_agg_bufsize;
3310 
3311 	IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
3312 		     sta->addr, tid);
3313 
3314 	return iwl_mvm_send_lq_cmd(mvm, &mvmsta->deflink.lq_sta.rs_drv.lq);
3315 }
3316 
3317 static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
3318 					struct iwl_mvm_sta *mvmsta,
3319 					struct iwl_mvm_tid_data *tid_data)
3320 {
3321 	u16 txq_id = tid_data->txq_id;
3322 
3323 	lockdep_assert_held(&mvm->mutex);
3324 
3325 	if (iwl_mvm_has_new_tx_api(mvm))
3326 		return;
3327 
3328 	/*
3329 	 * The TXQ is marked as reserved only if no traffic came through yet
3330 	 * This means no traffic has been sent on this TID (agg'd or not), so
3331 	 * we no longer have use for the queue. Since it hasn't even been
3332 	 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
3333 	 * free.
3334 	 */
3335 	if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
3336 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
3337 		tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
3338 	}
3339 }
3340 
3341 int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3342 			    struct ieee80211_sta *sta, u16 tid)
3343 {
3344 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3345 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3346 	u16 txq_id;
3347 	int err;
3348 
3349 	/*
3350 	 * If mac80211 is cleaning its state, then say that we finished since
3351 	 * our state has been cleared anyway.
3352 	 */
3353 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3354 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3355 		return 0;
3356 	}
3357 
3358 	spin_lock_bh(&mvmsta->lock);
3359 
3360 	txq_id = tid_data->txq_id;
3361 
3362 	IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
3363 			    mvmsta->deflink.sta_id, tid, txq_id,
3364 			    tid_data->state);
3365 
3366 	mvmsta->agg_tids &= ~BIT(tid);
3367 
3368 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3369 
3370 	switch (tid_data->state) {
3371 	case IWL_AGG_ON:
3372 		tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3373 
3374 		IWL_DEBUG_TX_QUEUES(mvm,
3375 				    "ssn = %d, next_recl = %d\n",
3376 				    tid_data->ssn, tid_data->next_reclaimed);
3377 
3378 		tid_data->ssn = 0xffff;
3379 		tid_data->state = IWL_AGG_OFF;
3380 		spin_unlock_bh(&mvmsta->lock);
3381 
3382 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3383 
3384 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3385 		return 0;
3386 	case IWL_AGG_STARTING:
3387 	case IWL_EMPTYING_HW_QUEUE_ADDBA:
3388 		/*
3389 		 * The agg session has been stopped before it was set up. This
3390 		 * can happen when the AddBA timer times out for example.
3391 		 */
3392 
3393 		/* No barriers since we are under mutex */
3394 		lockdep_assert_held(&mvm->mutex);
3395 
3396 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3397 		tid_data->state = IWL_AGG_OFF;
3398 		err = 0;
3399 		break;
3400 	default:
3401 		IWL_ERR(mvm,
3402 			"Stopping AGG while state not ON or starting for %d on %d (%d)\n",
3403 			mvmsta->deflink.sta_id, tid, tid_data->state);
3404 		IWL_ERR(mvm,
3405 			"\ttid_data->txq_id = %d\n", tid_data->txq_id);
3406 		err = -EINVAL;
3407 	}
3408 
3409 	spin_unlock_bh(&mvmsta->lock);
3410 
3411 	return err;
3412 }
3413 
3414 int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3415 			    struct ieee80211_sta *sta, u16 tid)
3416 {
3417 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3418 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3419 	u16 txq_id;
3420 	enum iwl_mvm_agg_state old_state;
3421 
3422 	/*
3423 	 * First set the agg state to OFF to avoid calling
3424 	 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
3425 	 */
3426 	spin_lock_bh(&mvmsta->lock);
3427 	txq_id = tid_data->txq_id;
3428 	IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
3429 			    mvmsta->deflink.sta_id, tid, txq_id,
3430 			    tid_data->state);
3431 	old_state = tid_data->state;
3432 	tid_data->state = IWL_AGG_OFF;
3433 	mvmsta->agg_tids &= ~BIT(tid);
3434 	spin_unlock_bh(&mvmsta->lock);
3435 
3436 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3437 
3438 	if (old_state >= IWL_AGG_ON) {
3439 		iwl_mvm_drain_sta(mvm, mvmsta, true);
3440 
3441 		if (iwl_mvm_has_new_tx_api(mvm)) {
3442 			if (iwl_mvm_flush_sta_tids(mvm, mvmsta->deflink.sta_id,
3443 						   BIT(tid)))
3444 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3445 			iwl_trans_wait_txq_empty(mvm->trans, txq_id);
3446 		} else {
3447 			if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id)))
3448 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3449 			iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
3450 		}
3451 
3452 		iwl_mvm_drain_sta(mvm, mvmsta, false);
3453 
3454 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3455 	}
3456 
3457 	return 0;
3458 }
3459 
3460 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
3461 {
3462 	int i, max = -1, max_offs = -1;
3463 
3464 	lockdep_assert_held(&mvm->mutex);
3465 
3466 	/* Pick the unused key offset with the highest 'deleted'
3467 	 * counter. Every time a key is deleted, all the counters
3468 	 * are incremented and the one that was just deleted is
3469 	 * reset to zero. Thus, the highest counter is the one
3470 	 * that was deleted longest ago. Pick that one.
3471 	 */
3472 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3473 		if (test_bit(i, mvm->fw_key_table))
3474 			continue;
3475 		if (mvm->fw_key_deleted[i] > max) {
3476 			max = mvm->fw_key_deleted[i];
3477 			max_offs = i;
3478 		}
3479 	}
3480 
3481 	if (max_offs < 0)
3482 		return STA_KEY_IDX_INVALID;
3483 
3484 	return max_offs;
3485 }
3486 
3487 static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
3488 					       struct ieee80211_vif *vif,
3489 					       struct ieee80211_sta *sta)
3490 {
3491 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3492 
3493 	if (sta)
3494 		return iwl_mvm_sta_from_mac80211(sta);
3495 
3496 	/*
3497 	 * The device expects GTKs for station interfaces to be
3498 	 * installed as GTKs for the AP station. If we have no
3499 	 * station ID, then use AP's station ID.
3500 	 */
3501 	if (vif->type == NL80211_IFTYPE_STATION &&
3502 	    mvmvif->deflink.ap_sta_id != IWL_INVALID_STA) {
3503 		u8 sta_id = mvmvif->deflink.ap_sta_id;
3504 
3505 		sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
3506 					    lockdep_is_held(&mvm->mutex));
3507 
3508 		/*
3509 		 * It is possible that the 'sta' parameter is NULL,
3510 		 * for example when a GTK is removed - the sta_id will then
3511 		 * be the AP ID, and no station was passed by mac80211.
3512 		 */
3513 		if (IS_ERR_OR_NULL(sta))
3514 			return NULL;
3515 
3516 		return iwl_mvm_sta_from_mac80211(sta);
3517 	}
3518 
3519 	return NULL;
3520 }
3521 
3522 static int iwl_mvm_pn_cmp(const u8 *pn1, const u8 *pn2, int len)
3523 {
3524 	int i;
3525 
3526 	for (i = len - 1; i >= 0; i--) {
3527 		if (pn1[i] > pn2[i])
3528 			return 1;
3529 		if (pn1[i] < pn2[i])
3530 			return -1;
3531 	}
3532 
3533 	return 0;
3534 }
3535 
3536 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
3537 				u32 sta_id,
3538 				struct ieee80211_key_conf *key, bool mcast,
3539 				u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
3540 				u8 key_offset, bool mfp)
3541 {
3542 	union {
3543 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3544 		struct iwl_mvm_add_sta_key_cmd cmd;
3545 	} u = {};
3546 	__le16 key_flags;
3547 	int ret;
3548 	u32 status;
3549 	u16 keyidx;
3550 	u64 pn = 0;
3551 	int i, size;
3552 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3553 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3554 	int api_ver = iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA_KEY,
3555 					    new_api ? 2 : 1);
3556 
3557 	if (sta_id == IWL_INVALID_STA)
3558 		return -EINVAL;
3559 
3560 	keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
3561 		 STA_KEY_FLG_KEYID_MSK;
3562 	key_flags = cpu_to_le16(keyidx);
3563 	key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3564 
3565 	if (key->flags & IEEE80211_KEY_FLAG_SPP_AMSDU)
3566 		key_flags |= cpu_to_le16(STA_KEY_FLG_AMSDU_SPP);
3567 
3568 	switch (key->cipher) {
3569 	case WLAN_CIPHER_SUITE_TKIP:
3570 		key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
3571 		if (api_ver >= 2) {
3572 			memcpy((void *)&u.cmd.tx_mic_key,
3573 			       &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
3574 			       IWL_MIC_KEY_SIZE);
3575 
3576 			memcpy((void *)&u.cmd.rx_mic_key,
3577 			       &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
3578 			       IWL_MIC_KEY_SIZE);
3579 			pn = atomic64_read(&key->tx_pn);
3580 
3581 		} else {
3582 			u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3583 			for (i = 0; i < 5; i++)
3584 				u.cmd_v1.tkip_rx_ttak[i] =
3585 					cpu_to_le16(tkip_p1k[i]);
3586 		}
3587 		memcpy(u.cmd.common.key, key->key, key->keylen);
3588 		break;
3589 	case WLAN_CIPHER_SUITE_CCMP:
3590 		key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
3591 		memcpy(u.cmd.common.key, key->key, key->keylen);
3592 		if (api_ver >= 2)
3593 			pn = atomic64_read(&key->tx_pn);
3594 		break;
3595 	case WLAN_CIPHER_SUITE_WEP104:
3596 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
3597 		fallthrough;
3598 	case WLAN_CIPHER_SUITE_WEP40:
3599 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
3600 		memcpy(u.cmd.common.key + 3, key->key, key->keylen);
3601 		break;
3602 	case WLAN_CIPHER_SUITE_GCMP_256:
3603 		key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
3604 		fallthrough;
3605 	case WLAN_CIPHER_SUITE_GCMP:
3606 		key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
3607 		memcpy(u.cmd.common.key, key->key, key->keylen);
3608 		if (api_ver >= 2)
3609 			pn = atomic64_read(&key->tx_pn);
3610 		break;
3611 	default:
3612 		key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
3613 		memcpy(u.cmd.common.key, key->key, key->keylen);
3614 	}
3615 
3616 	if (mcast)
3617 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3618 	if (mfp)
3619 		key_flags |= cpu_to_le16(STA_KEY_MFP);
3620 
3621 	u.cmd.common.key_offset = key_offset;
3622 	u.cmd.common.key_flags = key_flags;
3623 	u.cmd.common.sta_id = sta_id;
3624 
3625 	if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
3626 		i = 0;
3627 	else
3628 		i = -1;
3629 
3630 	for (; i < IEEE80211_NUM_TIDS; i++) {
3631 		struct ieee80211_key_seq seq = {};
3632 		u8 _rx_pn[IEEE80211_MAX_PN_LEN] = {}, *rx_pn = _rx_pn;
3633 		int rx_pn_len = 8;
3634 		/* there's a hole at 2/3 in FW format depending on version */
3635 		int hole = api_ver >= 3 ? 0 : 2;
3636 
3637 		ieee80211_get_key_rx_seq(key, i, &seq);
3638 
3639 		if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
3640 			rx_pn[0] = seq.tkip.iv16;
3641 			rx_pn[1] = seq.tkip.iv16 >> 8;
3642 			rx_pn[2 + hole] = seq.tkip.iv32;
3643 			rx_pn[3 + hole] = seq.tkip.iv32 >> 8;
3644 			rx_pn[4 + hole] = seq.tkip.iv32 >> 16;
3645 			rx_pn[5 + hole] = seq.tkip.iv32 >> 24;
3646 		} else if (key_flags & cpu_to_le16(STA_KEY_FLG_EXT)) {
3647 			rx_pn = seq.hw.seq;
3648 			rx_pn_len = seq.hw.seq_len;
3649 		} else {
3650 			rx_pn[0] = seq.ccmp.pn[0];
3651 			rx_pn[1] = seq.ccmp.pn[1];
3652 			rx_pn[2 + hole] = seq.ccmp.pn[2];
3653 			rx_pn[3 + hole] = seq.ccmp.pn[3];
3654 			rx_pn[4 + hole] = seq.ccmp.pn[4];
3655 			rx_pn[5 + hole] = seq.ccmp.pn[5];
3656 		}
3657 
3658 		if (iwl_mvm_pn_cmp(rx_pn, (u8 *)&u.cmd.common.rx_secur_seq_cnt,
3659 				   rx_pn_len) > 0)
3660 			memcpy(&u.cmd.common.rx_secur_seq_cnt, rx_pn,
3661 			       rx_pn_len);
3662 	}
3663 
3664 	if (api_ver >= 2) {
3665 		u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
3666 		size = sizeof(u.cmd);
3667 	} else {
3668 		size = sizeof(u.cmd_v1);
3669 	}
3670 
3671 	status = ADD_STA_SUCCESS;
3672 	if (cmd_flags & CMD_ASYNC)
3673 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
3674 					   &u.cmd);
3675 	else
3676 		ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
3677 						  &u.cmd, &status);
3678 
3679 	switch (status) {
3680 	case ADD_STA_SUCCESS:
3681 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3682 		break;
3683 	default:
3684 		ret = -EIO;
3685 		IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3686 		break;
3687 	}
3688 
3689 	return ret;
3690 }
3691 
3692 static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3693 				 struct ieee80211_key_conf *keyconf,
3694 				 u8 sta_id, bool remove_key)
3695 {
3696 	struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3697 
3698 	/* verify the key details match the required command's expectations */
3699 	if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3700 		    (keyconf->keyidx != 4 && keyconf->keyidx != 5 &&
3701 		     keyconf->keyidx != 6 && keyconf->keyidx != 7) ||
3702 		    (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
3703 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
3704 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
3705 		return -EINVAL;
3706 
3707 	if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
3708 		    keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3709 		return -EINVAL;
3710 
3711 	igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3712 	igtk_cmd.sta_id = cpu_to_le32(sta_id);
3713 
3714 	if (remove_key) {
3715 		/* This is a valid situation for IGTK */
3716 		if (sta_id == IWL_INVALID_STA)
3717 			return 0;
3718 
3719 		igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3720 	} else {
3721 		struct ieee80211_key_seq seq;
3722 		const u8 *pn;
3723 
3724 		switch (keyconf->cipher) {
3725 		case WLAN_CIPHER_SUITE_AES_CMAC:
3726 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3727 			break;
3728 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3729 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3730 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
3731 			break;
3732 		default:
3733 			return -EINVAL;
3734 		}
3735 
3736 		memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
3737 		if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3738 			igtk_cmd.ctrl_flags |=
3739 				cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3740 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3741 		pn = seq.aes_cmac.pn;
3742 		igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3743 						       ((u64) pn[4] << 8) |
3744 						       ((u64) pn[3] << 16) |
3745 						       ((u64) pn[2] << 24) |
3746 						       ((u64) pn[1] << 32) |
3747 						       ((u64) pn[0] << 40));
3748 	}
3749 
3750 	IWL_DEBUG_INFO(mvm, "%s %sIGTK (%d) for sta %u\n",
3751 		       remove_key ? "removing" : "installing",
3752 		       keyconf->keyidx >= 6 ? "B" : "",
3753 		       keyconf->keyidx, igtk_cmd.sta_id);
3754 
3755 	if (!iwl_mvm_has_new_rx_api(mvm)) {
3756 		struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
3757 			.ctrl_flags = igtk_cmd.ctrl_flags,
3758 			.key_id = igtk_cmd.key_id,
3759 			.sta_id = igtk_cmd.sta_id,
3760 			.receive_seq_cnt = igtk_cmd.receive_seq_cnt
3761 		};
3762 
3763 		memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
3764 		       ARRAY_SIZE(igtk_cmd_v1.igtk));
3765 		return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3766 					    sizeof(igtk_cmd_v1), &igtk_cmd_v1);
3767 	}
3768 	return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3769 				    sizeof(igtk_cmd), &igtk_cmd);
3770 }
3771 
3772 
3773 static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3774 				       struct ieee80211_vif *vif,
3775 				       struct ieee80211_sta *sta)
3776 {
3777 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3778 
3779 	if (sta)
3780 		return sta->addr;
3781 
3782 	if (vif->type == NL80211_IFTYPE_STATION &&
3783 	    mvmvif->deflink.ap_sta_id != IWL_INVALID_STA) {
3784 		u8 sta_id = mvmvif->deflink.ap_sta_id;
3785 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3786 						lockdep_is_held(&mvm->mutex));
3787 		if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
3788 			return NULL;
3789 
3790 		return sta->addr;
3791 	}
3792 
3793 
3794 	return NULL;
3795 }
3796 
3797 static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3798 				 struct ieee80211_vif *vif,
3799 				 struct ieee80211_sta *sta,
3800 				 struct ieee80211_key_conf *keyconf,
3801 				 u8 key_offset,
3802 				 bool mcast)
3803 {
3804 	const u8 *addr;
3805 	struct ieee80211_key_seq seq;
3806 	u16 p1k[5];
3807 	u32 sta_id;
3808 	bool mfp = false;
3809 
3810 	if (sta) {
3811 		struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3812 
3813 		sta_id = mvm_sta->deflink.sta_id;
3814 		mfp = sta->mfp;
3815 	} else if (vif->type == NL80211_IFTYPE_AP &&
3816 		   !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
3817 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3818 
3819 		sta_id = mvmvif->deflink.mcast_sta.sta_id;
3820 	} else {
3821 		IWL_ERR(mvm, "Failed to find station id\n");
3822 		return -EINVAL;
3823 	}
3824 
3825 	if (keyconf->cipher == WLAN_CIPHER_SUITE_TKIP) {
3826 		addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3827 		if (!addr) {
3828 			IWL_ERR(mvm, "Failed to find mac address\n");
3829 			return -EINVAL;
3830 		}
3831 
3832 		/* get phase 1 key from mac80211 */
3833 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3834 		ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
3835 
3836 		return iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3837 					    seq.tkip.iv32, p1k, 0, key_offset,
3838 					    mfp);
3839 	}
3840 
3841 	return iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3842 				    0, NULL, 0, key_offset, mfp);
3843 }
3844 
3845 int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3846 			struct ieee80211_vif *vif,
3847 			struct ieee80211_sta *sta,
3848 			struct ieee80211_key_conf *keyconf,
3849 			u8 key_offset)
3850 {
3851 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3852 	struct iwl_mvm_sta *mvm_sta;
3853 	u8 sta_id = IWL_INVALID_STA;
3854 	int ret;
3855 	static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3856 
3857 	lockdep_assert_held(&mvm->mutex);
3858 
3859 	if (vif->type != NL80211_IFTYPE_AP ||
3860 	    keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3861 		/* Get the station id from the mvm local station table */
3862 		mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3863 		if (!mvm_sta) {
3864 			IWL_ERR(mvm, "Failed to find station\n");
3865 			return -EINVAL;
3866 		}
3867 		sta_id = mvm_sta->deflink.sta_id;
3868 
3869 		/*
3870 		 * It is possible that the 'sta' parameter is NULL, and thus
3871 		 * there is a need to retrieve the sta from the local station
3872 		 * table.
3873 		 */
3874 		if (!sta) {
3875 			sta = rcu_dereference_protected(
3876 				mvm->fw_id_to_mac_id[sta_id],
3877 				lockdep_is_held(&mvm->mutex));
3878 			if (IS_ERR_OR_NULL(sta)) {
3879 				IWL_ERR(mvm, "Invalid station id\n");
3880 				return -EINVAL;
3881 			}
3882 		}
3883 
3884 		if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3885 			return -EINVAL;
3886 	} else {
3887 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3888 
3889 		sta_id = mvmvif->deflink.mcast_sta.sta_id;
3890 	}
3891 
3892 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3893 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3894 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3895 		ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3896 		goto end;
3897 	}
3898 
3899 	/* If the key_offset is not pre-assigned, we need to find a
3900 	 * new offset to use.  In normal cases, the offset is not
3901 	 * pre-assigned, but during HW_RESTART we want to reuse the
3902 	 * same indices, so we pass them when this function is called.
3903 	 *
3904 	 * In D3 entry, we need to hardcoded the indices (because the
3905 	 * firmware hardcodes the PTK offset to 0).  In this case, we
3906 	 * need to make sure we don't overwrite the hw_key_idx in the
3907 	 * keyconf structure, because otherwise we cannot configure
3908 	 * the original ones back when resuming.
3909 	 */
3910 	if (key_offset == STA_KEY_IDX_INVALID) {
3911 		key_offset  = iwl_mvm_set_fw_key_idx(mvm);
3912 		if (key_offset == STA_KEY_IDX_INVALID)
3913 			return -ENOSPC;
3914 		keyconf->hw_key_idx = key_offset;
3915 	}
3916 
3917 	ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
3918 	if (ret)
3919 		goto end;
3920 
3921 	/*
3922 	 * For WEP, the same key is used for multicast and unicast. Upload it
3923 	 * again, using the same key offset, and now pointing the other one
3924 	 * to the same key slot (offset).
3925 	 * If this fails, remove the original as well.
3926 	 */
3927 	if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3928 	     keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
3929 	    sta) {
3930 		ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
3931 					    key_offset, !mcast);
3932 		if (ret) {
3933 			__iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3934 			goto end;
3935 		}
3936 	}
3937 
3938 	__set_bit(key_offset, mvm->fw_key_table);
3939 
3940 end:
3941 	IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3942 		      keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3943 		      sta ? sta->addr : zero_addr, ret);
3944 	return ret;
3945 }
3946 
3947 int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3948 			   struct ieee80211_vif *vif,
3949 			   struct ieee80211_sta *sta,
3950 			   struct ieee80211_key_conf *keyconf)
3951 {
3952 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3953 	struct iwl_mvm_sta *mvm_sta;
3954 	u8 sta_id = IWL_INVALID_STA;
3955 	int ret, i;
3956 
3957 	lockdep_assert_held(&mvm->mutex);
3958 
3959 	/* Get the station from the mvm local station table */
3960 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3961 	if (mvm_sta)
3962 		sta_id = mvm_sta->deflink.sta_id;
3963 	else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
3964 		sta_id = iwl_mvm_vif_from_mac80211(vif)->deflink.mcast_sta.sta_id;
3965 
3966 
3967 	IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3968 		      keyconf->keyidx, sta_id);
3969 
3970 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3971 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3972 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3973 		return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3974 
3975 	if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3976 		IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3977 			keyconf->hw_key_idx);
3978 		return -ENOENT;
3979 	}
3980 
3981 	/* track which key was deleted last */
3982 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3983 		if (mvm->fw_key_deleted[i] < U8_MAX)
3984 			mvm->fw_key_deleted[i]++;
3985 	}
3986 	mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3987 
3988 	if (sta && !mvm_sta) {
3989 		IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3990 		return 0;
3991 	}
3992 
3993 	ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3994 	if (ret)
3995 		return ret;
3996 
3997 	/* delete WEP key twice to get rid of (now useless) offset */
3998 	if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3999 	    keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
4000 		ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
4001 
4002 	return ret;
4003 }
4004 
4005 void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
4006 			     struct ieee80211_vif *vif,
4007 			     struct ieee80211_key_conf *keyconf,
4008 			     struct ieee80211_sta *sta, u32 iv32,
4009 			     u16 *phase1key)
4010 {
4011 	struct iwl_mvm_sta *mvm_sta;
4012 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
4013 	bool mfp = sta ? sta->mfp : false;
4014 
4015 	rcu_read_lock();
4016 
4017 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
4018 	if (WARN_ON_ONCE(!mvm_sta))
4019 		goto unlock;
4020 	iwl_mvm_send_sta_key(mvm, mvm_sta->deflink.sta_id, keyconf, mcast,
4021 			     iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx,
4022 			     mfp);
4023 
4024  unlock:
4025 	rcu_read_unlock();
4026 }
4027 
4028 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
4029 				struct ieee80211_sta *sta)
4030 {
4031 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
4032 	struct iwl_mvm_add_sta_cmd cmd = {
4033 		.add_modify = STA_MODE_MODIFY,
4034 		.sta_id = mvmsta->deflink.sta_id,
4035 		.station_flags_msk = cpu_to_le32(STA_FLG_PS),
4036 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
4037 	};
4038 	int ret;
4039 
4040 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
4041 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
4042 	if (ret)
4043 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
4044 }
4045 
4046 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
4047 				       struct ieee80211_sta *sta,
4048 				       enum ieee80211_frame_release_type reason,
4049 				       u16 cnt, u16 tids, bool more_data,
4050 				       bool single_sta_queue)
4051 {
4052 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
4053 	struct iwl_mvm_add_sta_cmd cmd = {
4054 		.add_modify = STA_MODE_MODIFY,
4055 		.sta_id = mvmsta->deflink.sta_id,
4056 		.modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
4057 		.sleep_tx_count = cpu_to_le16(cnt),
4058 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
4059 	};
4060 	int tid, ret;
4061 	unsigned long _tids = tids;
4062 
4063 	/* convert TIDs to ACs - we don't support TSPEC so that's OK
4064 	 * Note that this field is reserved and unused by firmware not
4065 	 * supporting GO uAPSD, so it's safe to always do this.
4066 	 */
4067 	for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
4068 		cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
4069 
4070 	/* If we're releasing frames from aggregation or dqa queues then check
4071 	 * if all the queues that we're releasing frames from, combined, have:
4072 	 *  - more frames than the service period, in which case more_data
4073 	 *    needs to be set
4074 	 *  - fewer than 'cnt' frames, in which case we need to adjust the
4075 	 *    firmware command (but do that unconditionally)
4076 	 */
4077 	if (single_sta_queue) {
4078 		int remaining = cnt;
4079 		int sleep_tx_count;
4080 
4081 		spin_lock_bh(&mvmsta->lock);
4082 		for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
4083 			struct iwl_mvm_tid_data *tid_data;
4084 			u16 n_queued;
4085 
4086 			tid_data = &mvmsta->tid_data[tid];
4087 
4088 			n_queued = iwl_mvm_tid_queued(mvm, tid_data);
4089 			if (n_queued > remaining) {
4090 				more_data = true;
4091 				remaining = 0;
4092 				break;
4093 			}
4094 			remaining -= n_queued;
4095 		}
4096 		sleep_tx_count = cnt - remaining;
4097 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
4098 			mvmsta->sleep_tx_count = sleep_tx_count;
4099 		spin_unlock_bh(&mvmsta->lock);
4100 
4101 		cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
4102 		if (WARN_ON(cnt - remaining == 0)) {
4103 			ieee80211_sta_eosp(sta);
4104 			return;
4105 		}
4106 	}
4107 
4108 	/* Note: this is ignored by firmware not supporting GO uAPSD */
4109 	if (more_data)
4110 		cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
4111 
4112 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
4113 		mvmsta->next_status_eosp = true;
4114 		cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
4115 	} else {
4116 		cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
4117 	}
4118 
4119 	/* block the Tx queues until the FW updated the sleep Tx count */
4120 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
4121 				   CMD_ASYNC | CMD_BLOCK_TXQS,
4122 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
4123 	if (ret)
4124 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
4125 }
4126 
4127 void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
4128 			   struct iwl_rx_cmd_buffer *rxb)
4129 {
4130 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
4131 	struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
4132 	struct ieee80211_sta *sta;
4133 	u32 sta_id = le32_to_cpu(notif->sta_id);
4134 
4135 	if (WARN_ON_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations))
4136 		return;
4137 
4138 	rcu_read_lock();
4139 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
4140 	if (!IS_ERR_OR_NULL(sta))
4141 		ieee80211_sta_eosp(sta);
4142 	rcu_read_unlock();
4143 }
4144 
4145 void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
4146 				   struct iwl_mvm_sta *mvmsta,
4147 				   bool disable)
4148 {
4149 	struct iwl_mvm_add_sta_cmd cmd = {
4150 		.add_modify = STA_MODE_MODIFY,
4151 		.sta_id = mvmsta->deflink.sta_id,
4152 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
4153 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
4154 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
4155 	};
4156 	int ret;
4157 
4158 	if (mvm->mld_api_is_used) {
4159 		if (!iwl_mvm_has_no_host_disable_tx(mvm))
4160 			iwl_mvm_mld_sta_modify_disable_tx(mvm, mvmsta, disable);
4161 		return;
4162 	}
4163 
4164 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
4165 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
4166 	if (ret)
4167 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
4168 }
4169 
4170 void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
4171 				      struct ieee80211_sta *sta,
4172 				      bool disable)
4173 {
4174 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4175 
4176 	if (mvm->mld_api_is_used) {
4177 		if (!iwl_mvm_has_no_host_disable_tx(mvm))
4178 			iwl_mvm_mld_sta_modify_disable_tx_ap(mvm, sta, disable);
4179 		return;
4180 	}
4181 
4182 	spin_lock_bh(&mvm_sta->lock);
4183 
4184 	if (mvm_sta->disable_tx == disable) {
4185 		spin_unlock_bh(&mvm_sta->lock);
4186 		return;
4187 	}
4188 
4189 	mvm_sta->disable_tx = disable;
4190 
4191 	/*
4192 	 * If sta PS state is handled by mac80211, tell it to start/stop
4193 	 * queuing tx for this station.
4194 	 */
4195 	if (!ieee80211_hw_check(mvm->hw, AP_LINK_PS))
4196 		ieee80211_sta_block_awake(mvm->hw, sta, disable);
4197 
4198 	iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
4199 
4200 	spin_unlock_bh(&mvm_sta->lock);
4201 }
4202 
4203 static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
4204 					      struct iwl_mvm_vif *mvmvif,
4205 					      struct iwl_mvm_int_sta *sta,
4206 					      bool disable)
4207 {
4208 	u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
4209 	struct iwl_mvm_add_sta_cmd cmd = {
4210 		.add_modify = STA_MODE_MODIFY,
4211 		.sta_id = sta->sta_id,
4212 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
4213 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
4214 		.mac_id_n_color = cpu_to_le32(id),
4215 	};
4216 	int ret;
4217 
4218 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
4219 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
4220 	if (ret)
4221 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
4222 }
4223 
4224 void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
4225 				       struct iwl_mvm_vif *mvmvif,
4226 				       bool disable)
4227 {
4228 	struct ieee80211_sta *sta;
4229 	struct iwl_mvm_sta *mvm_sta;
4230 	int i;
4231 
4232 	if (mvm->mld_api_is_used) {
4233 		if (!iwl_mvm_has_no_host_disable_tx(mvm))
4234 			iwl_mvm_mld_modify_all_sta_disable_tx(mvm, mvmvif,
4235 							      disable);
4236 		return;
4237 	}
4238 
4239 	rcu_read_lock();
4240 
4241 	/* Block/unblock all the stations of the given mvmvif */
4242 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
4243 		sta = rcu_dereference(mvm->fw_id_to_mac_id[i]);
4244 		if (IS_ERR_OR_NULL(sta))
4245 			continue;
4246 
4247 		mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4248 		if (mvm_sta->mac_id_n_color !=
4249 		    FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
4250 			continue;
4251 
4252 		iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
4253 	}
4254 
4255 	rcu_read_unlock();
4256 
4257 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
4258 		return;
4259 
4260 	/* Need to block/unblock also multicast station */
4261 	if (mvmvif->deflink.mcast_sta.sta_id != IWL_INVALID_STA)
4262 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
4263 						  &mvmvif->deflink.mcast_sta,
4264 						  disable);
4265 
4266 	/*
4267 	 * Only unblock the broadcast station (FW blocks it for immediate
4268 	 * quiet, not the driver)
4269 	 */
4270 	if (!disable && mvmvif->deflink.bcast_sta.sta_id != IWL_INVALID_STA)
4271 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
4272 						  &mvmvif->deflink.bcast_sta,
4273 						  disable);
4274 }
4275 
4276 void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
4277 {
4278 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4279 	struct iwl_mvm_sta *mvmsta;
4280 
4281 	rcu_read_lock();
4282 
4283 	mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->deflink.ap_sta_id);
4284 
4285 	if (mvmsta)
4286 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
4287 
4288 	rcu_read_unlock();
4289 }
4290 
4291 u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
4292 {
4293 	u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
4294 
4295 	/*
4296 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
4297 	 * to align the wrap around of ssn so we compare relevant values.
4298 	 */
4299 	if (mvm->trans->mac_cfg->gen2)
4300 		sn &= 0xff;
4301 
4302 	return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
4303 }
4304 
4305 void iwl_mvm_cancel_channel_switch(struct iwl_mvm *mvm,
4306 				   struct ieee80211_vif *vif,
4307 				   u32 id)
4308 {
4309 	struct iwl_cancel_channel_switch_cmd cancel_channel_switch_cmd = {
4310 		.id = cpu_to_le32(id),
4311 	};
4312 	int ret;
4313 
4314 	ret = iwl_mvm_send_cmd_pdu(mvm,
4315 				   WIDE_ID(MAC_CONF_GROUP, CANCEL_CHANNEL_SWITCH_CMD),
4316 				   CMD_ASYNC,
4317 				   sizeof(cancel_channel_switch_cmd),
4318 				   &cancel_channel_switch_cmd);
4319 	if (ret)
4320 		IWL_ERR(mvm, "Failed to cancel the channel switch\n");
4321 }
4322