xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/sta.c (revision 04317b129e4eb5c6f4a58bb899b2019c1545320b)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2015, 2018-2023 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_MVM_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_MVM_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->chandef.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.chandef.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 		from_timer(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->trans_cfg->base_params->num_of_queues,
795 		 "max queue %d >= num_of_queues (%d)", maxq,
796 		 mvm->trans->trans_cfg->base_params->num_of_queues))
797 		maxq = mvm->trans->trans_cfg->base_params->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->cfg->min_txq_size);
856 	} else {
857 		size = iwl_mvm_get_queue_size(sta);
858 	}
859 
860 	/* take the min with bc tbl entries allowed */
861 	size = min_t(u32, size, mvm->trans->txqs.bc_tbl_size / sizeof(u16));
862 
863 	/* size needs to be power of 2 values for calculating read/write pointers */
864 	size = rounddown_pow_of_two(size);
865 
866 	if (sta) {
867 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
868 		struct ieee80211_link_sta *link_sta;
869 		unsigned int link_id;
870 
871 		rcu_read_lock();
872 		for_each_sta_active_link(mvmsta->vif, sta, link_sta, link_id) {
873 			struct iwl_mvm_link_sta *link =
874 				rcu_dereference_protected(mvmsta->link[link_id],
875 							  lockdep_is_held(&mvm->mutex));
876 
877 			if (!link)
878 				continue;
879 
880 			sta_mask |= BIT(link->sta_id);
881 		}
882 		rcu_read_unlock();
883 	} else {
884 		sta_mask |= BIT(sta_id);
885 	}
886 
887 	if (!sta_mask)
888 		return -EINVAL;
889 
890 	do {
891 		queue = iwl_trans_txq_alloc(mvm->trans, 0, sta_mask,
892 					    tid, size, timeout);
893 
894 		if (queue < 0)
895 			IWL_DEBUG_TX_QUEUES(mvm,
896 					    "Failed allocating TXQ of size %d for sta mask %x tid %d, ret: %d\n",
897 					    size, sta_mask, tid, queue);
898 		size /= 2;
899 	} while (queue < 0 && size >= 16);
900 
901 	if (queue < 0)
902 		return queue;
903 
904 	IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d for sta mask 0x%x tid %d\n",
905 			    queue, sta_mask, tid);
906 
907 	return queue;
908 }
909 
910 static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
911 					struct ieee80211_sta *sta, u8 ac,
912 					int tid)
913 {
914 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
915 	struct iwl_mvm_txq *mvmtxq =
916 		iwl_mvm_txq_from_tid(sta, tid);
917 	unsigned int wdg_timeout =
918 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
919 	int queue = -1;
920 
921 	lockdep_assert_held(&mvm->mutex);
922 
923 	IWL_DEBUG_TX_QUEUES(mvm,
924 			    "Allocating queue for sta %d on tid %d\n",
925 			    mvmsta->deflink.sta_id, tid);
926 	queue = iwl_mvm_tvqm_enable_txq(mvm, sta, mvmsta->deflink.sta_id,
927 					tid, wdg_timeout);
928 	if (queue < 0)
929 		return queue;
930 
931 	mvmtxq->txq_id = queue;
932 	mvm->tvqm_info[queue].txq_tid = tid;
933 	mvm->tvqm_info[queue].sta_id = mvmsta->deflink.sta_id;
934 
935 	IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
936 
937 	spin_lock_bh(&mvmsta->lock);
938 	mvmsta->tid_data[tid].txq_id = queue;
939 	spin_unlock_bh(&mvmsta->lock);
940 
941 	return 0;
942 }
943 
944 static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm,
945 				       struct ieee80211_sta *sta,
946 				       int queue, u8 sta_id, u8 tid)
947 {
948 	bool enable_queue = true;
949 
950 	/* Make sure this TID isn't already enabled */
951 	if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) {
952 		IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n",
953 			queue, tid);
954 		return false;
955 	}
956 
957 	/* Update mappings and refcounts */
958 	if (mvm->queue_info[queue].tid_bitmap)
959 		enable_queue = false;
960 
961 	mvm->queue_info[queue].tid_bitmap |= BIT(tid);
962 	mvm->queue_info[queue].ra_sta_id = sta_id;
963 
964 	if (enable_queue) {
965 		if (tid != IWL_MAX_TID_COUNT)
966 			mvm->queue_info[queue].mac80211_ac =
967 				tid_to_mac80211_ac[tid];
968 		else
969 			mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO;
970 
971 		mvm->queue_info[queue].txq_tid = tid;
972 	}
973 
974 	if (sta) {
975 		struct iwl_mvm_txq *mvmtxq =
976 			iwl_mvm_txq_from_tid(sta, tid);
977 
978 		mvmtxq->txq_id = queue;
979 	}
980 
981 	IWL_DEBUG_TX_QUEUES(mvm,
982 			    "Enabling TXQ #%d tids=0x%x\n",
983 			    queue, mvm->queue_info[queue].tid_bitmap);
984 
985 	return enable_queue;
986 }
987 
988 static bool iwl_mvm_enable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
989 			       int queue, u16 ssn,
990 			       const struct iwl_trans_txq_scd_cfg *cfg,
991 			       unsigned int wdg_timeout)
992 {
993 	struct iwl_scd_txq_cfg_cmd cmd = {
994 		.scd_queue = queue,
995 		.action = SCD_CFG_ENABLE_QUEUE,
996 		.window = cfg->frame_limit,
997 		.sta_id = cfg->sta_id,
998 		.ssn = cpu_to_le16(ssn),
999 		.tx_fifo = cfg->fifo,
1000 		.aggregate = cfg->aggregate,
1001 		.tid = cfg->tid,
1002 	};
1003 	bool inc_ssn;
1004 
1005 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1006 		return false;
1007 
1008 	/* Send the enabling command if we need to */
1009 	if (!iwl_mvm_update_txq_mapping(mvm, sta, queue, cfg->sta_id, cfg->tid))
1010 		return false;
1011 
1012 	inc_ssn = iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn,
1013 					   NULL, wdg_timeout);
1014 	if (inc_ssn)
1015 		le16_add_cpu(&cmd.ssn, 1);
1016 
1017 	WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd),
1018 	     "Failed to configure queue %d on FIFO %d\n", queue, cfg->fifo);
1019 
1020 	return inc_ssn;
1021 }
1022 
1023 static void iwl_mvm_change_queue_tid(struct iwl_mvm *mvm, int queue)
1024 {
1025 	struct iwl_scd_txq_cfg_cmd cmd = {
1026 		.scd_queue = queue,
1027 		.action = SCD_CFG_UPDATE_QUEUE_TID,
1028 	};
1029 	int tid;
1030 	unsigned long tid_bitmap;
1031 	int ret;
1032 
1033 	lockdep_assert_held(&mvm->mutex);
1034 
1035 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1036 		return;
1037 
1038 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1039 
1040 	if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
1041 		return;
1042 
1043 	/* Find any TID for queue */
1044 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
1045 	cmd.tid = tid;
1046 	cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
1047 
1048 	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
1049 	if (ret) {
1050 		IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
1051 			queue, ret);
1052 		return;
1053 	}
1054 
1055 	mvm->queue_info[queue].txq_tid = tid;
1056 	IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
1057 			    queue, tid);
1058 }
1059 
1060 static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
1061 {
1062 	struct ieee80211_sta *sta;
1063 	struct iwl_mvm_sta *mvmsta;
1064 	u8 sta_id;
1065 	int tid = -1;
1066 	unsigned long tid_bitmap;
1067 	unsigned int wdg_timeout;
1068 	int ssn;
1069 	int ret = true;
1070 
1071 	/* queue sharing is disabled on new TX path */
1072 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1073 		return;
1074 
1075 	lockdep_assert_held(&mvm->mutex);
1076 
1077 	sta_id = mvm->queue_info[queue].ra_sta_id;
1078 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1079 
1080 	/* Find TID for queue, and make sure it is the only one on the queue */
1081 	tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
1082 	if (tid_bitmap != BIT(tid)) {
1083 		IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
1084 			queue, tid_bitmap);
1085 		return;
1086 	}
1087 
1088 	IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
1089 			    tid);
1090 
1091 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1092 					lockdep_is_held(&mvm->mutex));
1093 
1094 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
1095 		return;
1096 
1097 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
1098 	wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1099 
1100 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1101 
1102 	ret = iwl_mvm_redirect_queue(mvm, queue, tid,
1103 				     tid_to_mac80211_ac[tid], ssn,
1104 				     wdg_timeout, true,
1105 				     iwl_mvm_txq_from_tid(sta, tid));
1106 	if (ret) {
1107 		IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
1108 		return;
1109 	}
1110 
1111 	/* If aggs should be turned back on - do it */
1112 	if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
1113 		struct iwl_mvm_add_sta_cmd cmd = {0};
1114 
1115 		mvmsta->tid_disable_agg &= ~BIT(tid);
1116 
1117 		cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1118 		cmd.sta_id = mvmsta->deflink.sta_id;
1119 		cmd.add_modify = STA_MODE_MODIFY;
1120 		cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1121 		cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
1122 		cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
1123 
1124 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
1125 					   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
1126 		if (!ret) {
1127 			IWL_DEBUG_TX_QUEUES(mvm,
1128 					    "TXQ #%d is now aggregated again\n",
1129 					    queue);
1130 
1131 			/* Mark queue intenally as aggregating again */
1132 			iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
1133 		}
1134 	}
1135 
1136 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1137 }
1138 
1139 /*
1140  * Remove inactive TIDs of a given queue.
1141  * If all queue TIDs are inactive - mark the queue as inactive
1142  * If only some the queue TIDs are inactive - unmap them from the queue
1143  *
1144  * Returns %true if all TIDs were removed and the queue could be reused.
1145  */
1146 static bool iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
1147 					 struct iwl_mvm_sta *mvmsta, int queue,
1148 					 unsigned long tid_bitmap,
1149 					 unsigned long *unshare_queues,
1150 					 unsigned long *changetid_queues)
1151 {
1152 	unsigned int tid;
1153 
1154 	lockdep_assert_held(&mvmsta->lock);
1155 	lockdep_assert_held(&mvm->mutex);
1156 
1157 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1158 		return false;
1159 
1160 	/* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
1161 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1162 		/* If some TFDs are still queued - don't mark TID as inactive */
1163 		if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid]))
1164 			tid_bitmap &= ~BIT(tid);
1165 
1166 		/* Don't mark as inactive any TID that has an active BA */
1167 		if (mvmsta->tid_data[tid].state != IWL_AGG_OFF)
1168 			tid_bitmap &= ~BIT(tid);
1169 	}
1170 
1171 	/* If all TIDs in the queue are inactive - return it can be reused */
1172 	if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
1173 		IWL_DEBUG_TX_QUEUES(mvm, "Queue %d is inactive\n", queue);
1174 		return true;
1175 	}
1176 
1177 	/*
1178 	 * If we are here, this is a shared queue and not all TIDs timed-out.
1179 	 * Remove the ones that did.
1180 	 */
1181 	for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1182 		u16 q_tid_bitmap;
1183 
1184 		mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
1185 		mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
1186 
1187 		q_tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1188 
1189 		/*
1190 		 * We need to take into account a situation in which a TXQ was
1191 		 * allocated to TID x, and then turned shared by adding TIDs y
1192 		 * and z. If TID x becomes inactive and is removed from the TXQ,
1193 		 * ownership must be given to one of the remaining TIDs.
1194 		 * This is mainly because if TID x continues - a new queue can't
1195 		 * be allocated for it as long as it is an owner of another TXQ.
1196 		 *
1197 		 * Mark this queue in the right bitmap, we'll send the command
1198 		 * to the firmware later.
1199 		 */
1200 		if (!(q_tid_bitmap & BIT(mvm->queue_info[queue].txq_tid)))
1201 			set_bit(queue, changetid_queues);
1202 
1203 		IWL_DEBUG_TX_QUEUES(mvm,
1204 				    "Removing inactive TID %d from shared Q:%d\n",
1205 				    tid, queue);
1206 	}
1207 
1208 	IWL_DEBUG_TX_QUEUES(mvm,
1209 			    "TXQ #%d left with tid bitmap 0x%x\n", queue,
1210 			    mvm->queue_info[queue].tid_bitmap);
1211 
1212 	/*
1213 	 * There may be different TIDs with the same mac queues, so make
1214 	 * sure all TIDs have existing corresponding mac queues enabled
1215 	 */
1216 	tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1217 
1218 	/* If the queue is marked as shared - "unshare" it */
1219 	if (hweight16(mvm->queue_info[queue].tid_bitmap) == 1 &&
1220 	    mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
1221 		IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
1222 				    queue);
1223 		set_bit(queue, unshare_queues);
1224 	}
1225 
1226 	return false;
1227 }
1228 
1229 /*
1230  * Check for inactivity - this includes checking if any queue
1231  * can be unshared and finding one (and only one) that can be
1232  * reused.
1233  * This function is also invoked as a sort of clean-up task,
1234  * in which case @alloc_for_sta is IWL_MVM_INVALID_STA.
1235  *
1236  * Returns the queue number, or -ENOSPC.
1237  */
1238 static int iwl_mvm_inactivity_check(struct iwl_mvm *mvm, u8 alloc_for_sta)
1239 {
1240 	unsigned long now = jiffies;
1241 	unsigned long unshare_queues = 0;
1242 	unsigned long changetid_queues = 0;
1243 	int i, ret, free_queue = -ENOSPC;
1244 	struct ieee80211_sta *queue_owner  = NULL;
1245 
1246 	lockdep_assert_held(&mvm->mutex);
1247 
1248 	if (iwl_mvm_has_new_tx_api(mvm))
1249 		return -ENOSPC;
1250 
1251 	rcu_read_lock();
1252 
1253 	/* we skip the CMD queue below by starting at 1 */
1254 	BUILD_BUG_ON(IWL_MVM_DQA_CMD_QUEUE != 0);
1255 
1256 	for (i = 1; i < IWL_MAX_HW_QUEUES; i++) {
1257 		struct ieee80211_sta *sta;
1258 		struct iwl_mvm_sta *mvmsta;
1259 		u8 sta_id;
1260 		int tid;
1261 		unsigned long inactive_tid_bitmap = 0;
1262 		unsigned long queue_tid_bitmap;
1263 
1264 		queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
1265 		if (!queue_tid_bitmap)
1266 			continue;
1267 
1268 		/* If TXQ isn't in active use anyway - nothing to do here... */
1269 		if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
1270 		    mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED)
1271 			continue;
1272 
1273 		/* Check to see if there are inactive TIDs on this queue */
1274 		for_each_set_bit(tid, &queue_tid_bitmap,
1275 				 IWL_MAX_TID_COUNT + 1) {
1276 			if (time_after(mvm->queue_info[i].last_frame_time[tid] +
1277 				       IWL_MVM_DQA_QUEUE_TIMEOUT, now))
1278 				continue;
1279 
1280 			inactive_tid_bitmap |= BIT(tid);
1281 		}
1282 
1283 		/* If all TIDs are active - finish check on this queue */
1284 		if (!inactive_tid_bitmap)
1285 			continue;
1286 
1287 		/*
1288 		 * If we are here - the queue hadn't been served recently and is
1289 		 * in use
1290 		 */
1291 
1292 		sta_id = mvm->queue_info[i].ra_sta_id;
1293 		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1294 
1295 		/*
1296 		 * If the STA doesn't exist anymore, it isn't an error. It could
1297 		 * be that it was removed since getting the queues, and in this
1298 		 * case it should've inactivated its queues anyway.
1299 		 */
1300 		if (IS_ERR_OR_NULL(sta))
1301 			continue;
1302 
1303 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
1304 
1305 		spin_lock_bh(&mvmsta->lock);
1306 		ret = iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
1307 						   inactive_tid_bitmap,
1308 						   &unshare_queues,
1309 						   &changetid_queues);
1310 		if (ret && free_queue < 0) {
1311 			queue_owner = sta;
1312 			free_queue = i;
1313 		}
1314 		/* only unlock sta lock - we still need the queue info lock */
1315 		spin_unlock_bh(&mvmsta->lock);
1316 	}
1317 
1318 
1319 	/* Reconfigure queues requiring reconfiguation */
1320 	for_each_set_bit(i, &unshare_queues, IWL_MAX_HW_QUEUES)
1321 		iwl_mvm_unshare_queue(mvm, i);
1322 	for_each_set_bit(i, &changetid_queues, IWL_MAX_HW_QUEUES)
1323 		iwl_mvm_change_queue_tid(mvm, i);
1324 
1325 	rcu_read_unlock();
1326 
1327 	if (free_queue >= 0 && alloc_for_sta != IWL_MVM_INVALID_STA) {
1328 		ret = iwl_mvm_free_inactive_queue(mvm, free_queue, queue_owner,
1329 						  alloc_for_sta);
1330 		if (ret)
1331 			return ret;
1332 	}
1333 
1334 	return free_queue;
1335 }
1336 
1337 static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
1338 				   struct ieee80211_sta *sta, u8 ac, int tid)
1339 {
1340 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1341 	struct iwl_trans_txq_scd_cfg cfg = {
1342 		.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
1343 		.sta_id = mvmsta->deflink.sta_id,
1344 		.tid = tid,
1345 		.frame_limit = IWL_FRAME_LIMIT,
1346 	};
1347 	unsigned int wdg_timeout =
1348 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1349 	int queue = -1;
1350 	u16 queue_tmp;
1351 	unsigned long disable_agg_tids = 0;
1352 	enum iwl_mvm_agg_state queue_state;
1353 	bool shared_queue = false, inc_ssn;
1354 	int ssn;
1355 	unsigned long tfd_queue_mask;
1356 	int ret;
1357 
1358 	lockdep_assert_held(&mvm->mutex);
1359 
1360 	if (iwl_mvm_has_new_tx_api(mvm))
1361 		return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
1362 
1363 	spin_lock_bh(&mvmsta->lock);
1364 	tfd_queue_mask = mvmsta->tfd_queue_msk;
1365 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1366 	spin_unlock_bh(&mvmsta->lock);
1367 
1368 	if (tid == IWL_MAX_TID_COUNT) {
1369 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->deflink.sta_id,
1370 						IWL_MVM_DQA_MIN_MGMT_QUEUE,
1371 						IWL_MVM_DQA_MAX_MGMT_QUEUE);
1372 		if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
1373 			IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
1374 					    queue);
1375 
1376 		/* If no such queue is found, we'll use a DATA queue instead */
1377 	}
1378 
1379 	if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
1380 	    (mvm->queue_info[mvmsta->reserved_queue].status ==
1381 			IWL_MVM_QUEUE_RESERVED)) {
1382 		queue = mvmsta->reserved_queue;
1383 		mvm->queue_info[queue].reserved = true;
1384 		IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
1385 	}
1386 
1387 	if (queue < 0)
1388 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->deflink.sta_id,
1389 						IWL_MVM_DQA_MIN_DATA_QUEUE,
1390 						IWL_MVM_DQA_MAX_DATA_QUEUE);
1391 	if (queue < 0) {
1392 		/* try harder - perhaps kill an inactive queue */
1393 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->deflink.sta_id);
1394 	}
1395 
1396 	/* No free queue - we'll have to share */
1397 	if (queue <= 0) {
1398 		queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
1399 		if (queue > 0) {
1400 			shared_queue = true;
1401 			mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
1402 		}
1403 	}
1404 
1405 	/*
1406 	 * Mark TXQ as ready, even though it hasn't been fully configured yet,
1407 	 * to make sure no one else takes it.
1408 	 * This will allow avoiding re-acquiring the lock at the end of the
1409 	 * configuration. On error we'll mark it back as free.
1410 	 */
1411 	if (queue > 0 && !shared_queue)
1412 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1413 
1414 	/* This shouldn't happen - out of queues */
1415 	if (WARN_ON(queue <= 0)) {
1416 		IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
1417 			tid, cfg.sta_id);
1418 		return queue;
1419 	}
1420 
1421 	/*
1422 	 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
1423 	 * but for configuring the SCD to send A-MPDUs we need to mark the queue
1424 	 * as aggregatable.
1425 	 * Mark all DATA queues as allowing to be aggregated at some point
1426 	 */
1427 	cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1428 			 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1429 
1430 	IWL_DEBUG_TX_QUEUES(mvm,
1431 			    "Allocating %squeue #%d to sta %d on tid %d\n",
1432 			    shared_queue ? "shared " : "", queue,
1433 			    mvmsta->deflink.sta_id, tid);
1434 
1435 	if (shared_queue) {
1436 		/* Disable any open aggs on this queue */
1437 		disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
1438 
1439 		if (disable_agg_tids) {
1440 			IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
1441 					    queue);
1442 			iwl_mvm_invalidate_sta_queue(mvm, queue,
1443 						     disable_agg_tids, false);
1444 		}
1445 	}
1446 
1447 	inc_ssn = iwl_mvm_enable_txq(mvm, sta, queue, ssn, &cfg, wdg_timeout);
1448 
1449 	/*
1450 	 * Mark queue as shared in transport if shared
1451 	 * Note this has to be done after queue enablement because enablement
1452 	 * can also set this value, and there is no indication there to shared
1453 	 * queues
1454 	 */
1455 	if (shared_queue)
1456 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
1457 
1458 	spin_lock_bh(&mvmsta->lock);
1459 	/*
1460 	 * This looks racy, but it is not. We have only one packet for
1461 	 * this ra/tid in our Tx path since we stop the Qdisc when we
1462 	 * need to allocate a new TFD queue.
1463 	 */
1464 	if (inc_ssn) {
1465 		mvmsta->tid_data[tid].seq_number += 0x10;
1466 		ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
1467 	}
1468 	mvmsta->tid_data[tid].txq_id = queue;
1469 	mvmsta->tfd_queue_msk |= BIT(queue);
1470 	queue_state = mvmsta->tid_data[tid].state;
1471 
1472 	if (mvmsta->reserved_queue == queue)
1473 		mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
1474 	spin_unlock_bh(&mvmsta->lock);
1475 
1476 	if (!shared_queue) {
1477 		ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
1478 		if (ret)
1479 			goto out_err;
1480 
1481 		/* If we need to re-enable aggregations... */
1482 		if (queue_state == IWL_AGG_ON) {
1483 			ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1484 			if (ret)
1485 				goto out_err;
1486 		}
1487 	} else {
1488 		/* Redirect queue, if needed */
1489 		ret = iwl_mvm_redirect_queue(mvm, queue, tid, ac, ssn,
1490 					     wdg_timeout, false,
1491 					     iwl_mvm_txq_from_tid(sta, tid));
1492 		if (ret)
1493 			goto out_err;
1494 	}
1495 
1496 	return 0;
1497 
1498 out_err:
1499 	queue_tmp = queue;
1500 	iwl_mvm_disable_txq(mvm, sta, mvmsta->deflink.sta_id, &queue_tmp, tid);
1501 
1502 	return ret;
1503 }
1504 
1505 void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1506 {
1507 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1508 					   add_stream_wk);
1509 
1510 	mutex_lock(&mvm->mutex);
1511 
1512 	iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
1513 
1514 	while (!list_empty(&mvm->add_stream_txqs)) {
1515 		struct iwl_mvm_txq *mvmtxq;
1516 		struct ieee80211_txq *txq;
1517 		u8 tid;
1518 
1519 		mvmtxq = list_first_entry(&mvm->add_stream_txqs,
1520 					  struct iwl_mvm_txq, list);
1521 
1522 		txq = container_of((void *)mvmtxq, struct ieee80211_txq,
1523 				   drv_priv);
1524 		tid = txq->tid;
1525 		if (tid == IEEE80211_NUM_TIDS)
1526 			tid = IWL_MAX_TID_COUNT;
1527 
1528 		/*
1529 		 * We can't really do much here, but if this fails we can't
1530 		 * transmit anyway - so just don't transmit the frame etc.
1531 		 * and let them back up ... we've tried our best to allocate
1532 		 * a queue in the function itself.
1533 		 */
1534 		if (iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, tid)) {
1535 			spin_lock_bh(&mvm->add_stream_lock);
1536 			list_del_init(&mvmtxq->list);
1537 			spin_unlock_bh(&mvm->add_stream_lock);
1538 			continue;
1539 		}
1540 
1541 		/* now we're ready, any remaining races/concurrency will be
1542 		 * handled in iwl_mvm_mac_itxq_xmit()
1543 		 */
1544 		set_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state);
1545 
1546 		local_bh_disable();
1547 		spin_lock(&mvm->add_stream_lock);
1548 		list_del_init(&mvmtxq->list);
1549 		spin_unlock(&mvm->add_stream_lock);
1550 
1551 		iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1552 		local_bh_enable();
1553 	}
1554 
1555 	mutex_unlock(&mvm->mutex);
1556 }
1557 
1558 static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1559 				      struct ieee80211_sta *sta,
1560 				      enum nl80211_iftype vif_type)
1561 {
1562 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1563 	int queue;
1564 
1565 	/* queue reserving is disabled on new TX path */
1566 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1567 		return 0;
1568 
1569 	/* run the general cleanup/unsharing of queues */
1570 	iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
1571 
1572 	/* Make sure we have free resources for this STA */
1573 	if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1574 	    !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].tid_bitmap &&
1575 	    (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1576 	     IWL_MVM_QUEUE_FREE))
1577 		queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1578 	else
1579 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->deflink.sta_id,
1580 						IWL_MVM_DQA_MIN_DATA_QUEUE,
1581 						IWL_MVM_DQA_MAX_DATA_QUEUE);
1582 	if (queue < 0) {
1583 		/* try again - this time kick out a queue if needed */
1584 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->deflink.sta_id);
1585 		if (queue < 0) {
1586 			IWL_ERR(mvm, "No available queues for new station\n");
1587 			return -ENOSPC;
1588 		}
1589 	}
1590 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
1591 
1592 	mvmsta->reserved_queue = queue;
1593 
1594 	IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1595 			    queue, mvmsta->deflink.sta_id);
1596 
1597 	return 0;
1598 }
1599 
1600 /*
1601  * In DQA mode, after a HW restart the queues should be allocated as before, in
1602  * order to avoid race conditions when there are shared queues. This function
1603  * does the re-mapping and queue allocation.
1604  *
1605  * Note that re-enabling aggregations isn't done in this function.
1606  */
1607 void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1608 					  struct ieee80211_sta *sta)
1609 {
1610 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1611 	unsigned int wdg =
1612 		iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
1613 	int i;
1614 	struct iwl_trans_txq_scd_cfg cfg = {
1615 		.sta_id = mvm_sta->deflink.sta_id,
1616 		.frame_limit = IWL_FRAME_LIMIT,
1617 	};
1618 
1619 	/* Make sure reserved queue is still marked as such (if allocated) */
1620 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
1621 		mvm->queue_info[mvm_sta->reserved_queue].status =
1622 			IWL_MVM_QUEUE_RESERVED;
1623 
1624 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1625 		struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1626 		int txq_id = tid_data->txq_id;
1627 		int ac;
1628 
1629 		if (txq_id == IWL_MVM_INVALID_QUEUE)
1630 			continue;
1631 
1632 		ac = tid_to_mac80211_ac[i];
1633 
1634 		if (iwl_mvm_has_new_tx_api(mvm)) {
1635 			IWL_DEBUG_TX_QUEUES(mvm,
1636 					    "Re-mapping sta %d tid %d\n",
1637 					    mvm_sta->deflink.sta_id, i);
1638 			txq_id = iwl_mvm_tvqm_enable_txq(mvm, sta,
1639 							 mvm_sta->deflink.sta_id,
1640 							 i, wdg);
1641 			/*
1642 			 * on failures, just set it to IWL_MVM_INVALID_QUEUE
1643 			 * to try again later, we have no other good way of
1644 			 * failing here
1645 			 */
1646 			if (txq_id < 0)
1647 				txq_id = IWL_MVM_INVALID_QUEUE;
1648 			tid_data->txq_id = txq_id;
1649 
1650 			/*
1651 			 * Since we don't set the seq number after reset, and HW
1652 			 * sets it now, FW reset will cause the seq num to start
1653 			 * at 0 again, so driver will need to update it
1654 			 * internally as well, so it keeps in sync with real val
1655 			 */
1656 			tid_data->seq_number = 0;
1657 		} else {
1658 			u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1659 
1660 			cfg.tid = i;
1661 			cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
1662 			cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1663 					 txq_id ==
1664 					 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1665 
1666 			IWL_DEBUG_TX_QUEUES(mvm,
1667 					    "Re-mapping sta %d tid %d to queue %d\n",
1668 					    mvm_sta->deflink.sta_id, i,
1669 					    txq_id);
1670 
1671 			iwl_mvm_enable_txq(mvm, sta, txq_id, seq, &cfg, wdg);
1672 			mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
1673 		}
1674 	}
1675 }
1676 
1677 static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1678 				      struct iwl_mvm_int_sta *sta,
1679 				      const u8 *addr,
1680 				      u16 mac_id, u16 color)
1681 {
1682 	struct iwl_mvm_add_sta_cmd cmd;
1683 	int ret;
1684 	u32 status = ADD_STA_SUCCESS;
1685 
1686 	lockdep_assert_held(&mvm->mutex);
1687 
1688 	memset(&cmd, 0, sizeof(cmd));
1689 	cmd.sta_id = sta->sta_id;
1690 
1691 	if (iwl_mvm_has_new_station_api(mvm->fw) &&
1692 	    sta->type == IWL_STA_AUX_ACTIVITY)
1693 		cmd.mac_id_n_color = cpu_to_le32(mac_id);
1694 	else
1695 		cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1696 								     color));
1697 
1698 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1699 		cmd.station_type = sta->type;
1700 
1701 	if (!iwl_mvm_has_new_tx_api(mvm))
1702 		cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1703 	cmd.tid_disable_tx = cpu_to_le16(0xffff);
1704 
1705 	if (addr)
1706 		memcpy(cmd.addr, addr, ETH_ALEN);
1707 
1708 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1709 					  iwl_mvm_add_sta_cmd_size(mvm),
1710 					  &cmd, &status);
1711 	if (ret)
1712 		return ret;
1713 
1714 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1715 	case ADD_STA_SUCCESS:
1716 		IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1717 		return 0;
1718 	default:
1719 		ret = -EIO;
1720 		IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1721 			status);
1722 		break;
1723 	}
1724 	return ret;
1725 }
1726 
1727 /* Initialize driver data of a new sta */
1728 int iwl_mvm_sta_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1729 		     struct ieee80211_sta *sta, int sta_id, u8 sta_type)
1730 {
1731 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1732 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1733 	struct iwl_mvm_rxq_dup_data *dup_data;
1734 	int i, ret = 0;
1735 
1736 	lockdep_assert_held(&mvm->mutex);
1737 
1738 	mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1739 						      mvmvif->color);
1740 	mvm_sta->vif = vif;
1741 
1742 	/* for MLD sta_id(s) should be allocated for each link before calling
1743 	 * this function
1744 	 */
1745 	if (!mvm->mld_api_is_used) {
1746 		if (WARN_ON(sta_id == IWL_MVM_INVALID_STA))
1747 			return -EINVAL;
1748 
1749 		mvm_sta->deflink.sta_id = sta_id;
1750 		rcu_assign_pointer(mvm_sta->link[0], &mvm_sta->deflink);
1751 
1752 		if (!mvm->trans->trans_cfg->gen2)
1753 			mvm_sta->deflink.lq_sta.rs_drv.pers.max_agg_bufsize =
1754 				LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1755 		else
1756 			mvm_sta->deflink.lq_sta.rs_drv.pers.max_agg_bufsize =
1757 				LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
1758 	}
1759 
1760 	mvm_sta->tt_tx_protection = false;
1761 	mvm_sta->sta_type = sta_type;
1762 
1763 	mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1764 
1765 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1766 		/*
1767 		 * Mark all queues for this STA as unallocated and defer TX
1768 		 * frames until the queue is allocated
1769 		 */
1770 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1771 	}
1772 
1773 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1774 		struct iwl_mvm_txq *mvmtxq =
1775 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1776 
1777 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1778 		INIT_LIST_HEAD(&mvmtxq->list);
1779 		atomic_set(&mvmtxq->tx_request, 0);
1780 	}
1781 
1782 	if (iwl_mvm_has_new_rx_api(mvm)) {
1783 		int q;
1784 
1785 		dup_data = kcalloc(mvm->trans->num_rx_queues,
1786 				   sizeof(*dup_data), GFP_KERNEL);
1787 		if (!dup_data)
1788 			return -ENOMEM;
1789 		/*
1790 		 * Initialize all the last_seq values to 0xffff which can never
1791 		 * compare equal to the frame's seq_ctrl in the check in
1792 		 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
1793 		 * number and fragmented packets don't reach that function.
1794 		 *
1795 		 * This thus allows receiving a packet with seqno 0 and the
1796 		 * retry bit set as the very first packet on a new TID.
1797 		 */
1798 		for (q = 0; q < mvm->trans->num_rx_queues; q++)
1799 			memset(dup_data[q].last_seq, 0xff,
1800 			       sizeof(dup_data[q].last_seq));
1801 		mvm_sta->dup_data = dup_data;
1802 	}
1803 
1804 	if (!iwl_mvm_has_new_tx_api(mvm)) {
1805 		ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1806 						 ieee80211_vif_type_p2p(vif));
1807 		if (ret)
1808 			return ret;
1809 	}
1810 
1811 	/*
1812 	 * if rs is registered with mac80211, then "add station" will be handled
1813 	 * via the corresponding ops, otherwise need to notify rate scaling here
1814 	 */
1815 	if (iwl_mvm_has_tlc_offload(mvm))
1816 		iwl_mvm_rs_add_sta(mvm, mvm_sta);
1817 	else
1818 		spin_lock_init(&mvm_sta->deflink.lq_sta.rs_drv.pers.lock);
1819 
1820 	iwl_mvm_toggle_tx_ant(mvm, &mvm_sta->tx_ant);
1821 
1822 	return 0;
1823 }
1824 
1825 int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1826 		    struct ieee80211_vif *vif,
1827 		    struct ieee80211_sta *sta)
1828 {
1829 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1830 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1831 	int ret, sta_id;
1832 	bool sta_update = false;
1833 	unsigned int sta_flags = 0;
1834 
1835 	lockdep_assert_held(&mvm->mutex);
1836 
1837 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1838 		sta_id = iwl_mvm_find_free_sta_id(mvm,
1839 						  ieee80211_vif_type_p2p(vif));
1840 	else
1841 		sta_id = mvm_sta->deflink.sta_id;
1842 
1843 	if (sta_id == IWL_MVM_INVALID_STA)
1844 		return -ENOSPC;
1845 
1846 	spin_lock_init(&mvm_sta->lock);
1847 
1848 	/* if this is a HW restart re-alloc existing queues */
1849 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1850 		struct iwl_mvm_int_sta tmp_sta = {
1851 			.sta_id = sta_id,
1852 			.type = mvm_sta->sta_type,
1853 		};
1854 
1855 		/* First add an empty station since allocating
1856 		 * a queue requires a valid station
1857 		 */
1858 		ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1859 						 mvmvif->id, mvmvif->color);
1860 		if (ret)
1861 			goto err;
1862 
1863 		iwl_mvm_realloc_queues_after_restart(mvm, sta);
1864 		sta_update = true;
1865 		sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
1866 		goto update_fw;
1867 	}
1868 
1869 	ret = iwl_mvm_sta_init(mvm, vif, sta, sta_id,
1870 			       sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK);
1871 	if (ret)
1872 		goto err;
1873 
1874 update_fw:
1875 	ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
1876 	if (ret)
1877 		goto err;
1878 
1879 	if (vif->type == NL80211_IFTYPE_STATION) {
1880 		if (!sta->tdls) {
1881 			WARN_ON(mvmvif->deflink.ap_sta_id != IWL_MVM_INVALID_STA);
1882 			mvmvif->deflink.ap_sta_id = sta_id;
1883 		} else {
1884 			WARN_ON(mvmvif->deflink.ap_sta_id == IWL_MVM_INVALID_STA);
1885 		}
1886 	}
1887 
1888 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1889 
1890 	return 0;
1891 
1892 err:
1893 	return ret;
1894 }
1895 
1896 int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1897 		      bool drain)
1898 {
1899 	struct iwl_mvm_add_sta_cmd cmd = {};
1900 	int ret;
1901 	u32 status;
1902 
1903 	lockdep_assert_held(&mvm->mutex);
1904 
1905 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1906 	cmd.sta_id = mvmsta->deflink.sta_id;
1907 	cmd.add_modify = STA_MODE_MODIFY;
1908 	cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1909 	cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1910 
1911 	status = ADD_STA_SUCCESS;
1912 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1913 					  iwl_mvm_add_sta_cmd_size(mvm),
1914 					  &cmd, &status);
1915 	if (ret)
1916 		return ret;
1917 
1918 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1919 	case ADD_STA_SUCCESS:
1920 		IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1921 			       mvmsta->deflink.sta_id);
1922 		break;
1923 	default:
1924 		ret = -EIO;
1925 		IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1926 			mvmsta->deflink.sta_id);
1927 		break;
1928 	}
1929 
1930 	return ret;
1931 }
1932 
1933 /*
1934  * Remove a station from the FW table. Before sending the command to remove
1935  * the station validate that the station is indeed known to the driver (sanity
1936  * only).
1937  */
1938 static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1939 {
1940 	struct ieee80211_sta *sta;
1941 	struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1942 		.sta_id = sta_id,
1943 	};
1944 	int ret;
1945 
1946 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1947 					lockdep_is_held(&mvm->mutex));
1948 
1949 	/* Note: internal stations are marked as error values */
1950 	if (!sta) {
1951 		IWL_ERR(mvm, "Invalid station id\n");
1952 		return -EINVAL;
1953 	}
1954 
1955 	ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1956 				   sizeof(rm_sta_cmd), &rm_sta_cmd);
1957 	if (ret) {
1958 		IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1959 		return ret;
1960 	}
1961 
1962 	return 0;
1963 }
1964 
1965 static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1966 				       struct ieee80211_vif *vif,
1967 				       struct ieee80211_sta *sta)
1968 {
1969 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1970 	int i;
1971 
1972 	lockdep_assert_held(&mvm->mutex);
1973 
1974 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1975 		if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
1976 			continue;
1977 
1978 		iwl_mvm_disable_txq(mvm, sta, mvm_sta->deflink.sta_id,
1979 				    &mvm_sta->tid_data[i].txq_id, i);
1980 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1981 	}
1982 
1983 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1984 		struct iwl_mvm_txq *mvmtxq =
1985 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1986 
1987 		spin_lock_bh(&mvm->add_stream_lock);
1988 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1989 		list_del_init(&mvmtxq->list);
1990 		clear_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state);
1991 		spin_unlock_bh(&mvm->add_stream_lock);
1992 	}
1993 }
1994 
1995 int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1996 				  struct iwl_mvm_sta *mvm_sta)
1997 {
1998 	int i;
1999 
2000 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
2001 		u16 txq_id;
2002 		int ret;
2003 
2004 		spin_lock_bh(&mvm_sta->lock);
2005 		txq_id = mvm_sta->tid_data[i].txq_id;
2006 		spin_unlock_bh(&mvm_sta->lock);
2007 
2008 		if (txq_id == IWL_MVM_INVALID_QUEUE)
2009 			continue;
2010 
2011 		ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
2012 		if (ret)
2013 			return ret;
2014 	}
2015 
2016 	return 0;
2017 }
2018 
2019 /* Execute the common part for both MLD and non-MLD modes.
2020  * Returns if we're done with removing the station, either
2021  * with error or success
2022  */
2023 bool iwl_mvm_sta_del(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2024 		     struct ieee80211_sta *sta,
2025 		     struct ieee80211_link_sta *link_sta, int *ret)
2026 {
2027 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2028 	struct iwl_mvm_vif_link_info *mvm_link =
2029 		mvmvif->link[link_sta->link_id];
2030 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2031 	struct iwl_mvm_link_sta *mvm_link_sta;
2032 	u8 sta_id;
2033 
2034 	lockdep_assert_held(&mvm->mutex);
2035 
2036 	mvm_link_sta =
2037 		rcu_dereference_protected(mvm_sta->link[link_sta->link_id],
2038 					  lockdep_is_held(&mvm->mutex));
2039 	sta_id = mvm_link_sta->sta_id;
2040 
2041 	/* If there is a TXQ still marked as reserved - free it */
2042 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
2043 		u8 reserved_txq = mvm_sta->reserved_queue;
2044 		enum iwl_mvm_queue_status *status;
2045 
2046 		/*
2047 		 * If no traffic has gone through the reserved TXQ - it
2048 		 * is still marked as IWL_MVM_QUEUE_RESERVED, and
2049 		 * should be manually marked as free again
2050 		 */
2051 		status = &mvm->queue_info[reserved_txq].status;
2052 		if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
2053 			 (*status != IWL_MVM_QUEUE_FREE),
2054 			 "sta_id %d reserved txq %d status %d",
2055 			 sta_id, reserved_txq, *status)) {
2056 			*ret = -EINVAL;
2057 			return true;
2058 		}
2059 
2060 		*status = IWL_MVM_QUEUE_FREE;
2061 	}
2062 
2063 	if (vif->type == NL80211_IFTYPE_STATION) {
2064 		/* if associated - we can't remove the AP STA now */
2065 		if (vif->cfg.assoc)
2066 			return true;
2067 
2068 		/* first remove remaining keys */
2069 		iwl_mvm_sec_key_remove_ap(mvm, vif, mvm_link, 0);
2070 
2071 		/* unassoc - go ahead - remove the AP STA now */
2072 		mvm_link->ap_sta_id = IWL_MVM_INVALID_STA;
2073 	}
2074 
2075 	/*
2076 	 * This shouldn't happen - the TDLS channel switch should be canceled
2077 	 * before the STA is removed.
2078 	 */
2079 	if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
2080 		mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
2081 		cancel_delayed_work(&mvm->tdls_cs.dwork);
2082 	}
2083 
2084 	return false;
2085 }
2086 
2087 int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
2088 		   struct ieee80211_vif *vif,
2089 		   struct ieee80211_sta *sta)
2090 {
2091 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2092 	int ret;
2093 
2094 	lockdep_assert_held(&mvm->mutex);
2095 
2096 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
2097 	if (ret)
2098 		return ret;
2099 
2100 	/* flush its queues here since we are freeing mvm_sta */
2101 	ret = iwl_mvm_flush_sta(mvm, mvm_sta, false);
2102 	if (ret)
2103 		return ret;
2104 	if (iwl_mvm_has_new_tx_api(mvm)) {
2105 		ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
2106 	} else {
2107 		u32 q_mask = mvm_sta->tfd_queue_msk;
2108 
2109 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2110 						     q_mask);
2111 	}
2112 	if (ret)
2113 		return ret;
2114 
2115 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
2116 
2117 	iwl_mvm_disable_sta_queues(mvm, vif, sta);
2118 
2119 	if (iwl_mvm_sta_del(mvm, vif, sta, &sta->deflink, &ret))
2120 		return ret;
2121 
2122 	ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->deflink.sta_id);
2123 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->deflink.sta_id], NULL);
2124 
2125 	return ret;
2126 }
2127 
2128 int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
2129 		      struct ieee80211_vif *vif,
2130 		      u8 sta_id)
2131 {
2132 	int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
2133 
2134 	lockdep_assert_held(&mvm->mutex);
2135 
2136 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
2137 	return ret;
2138 }
2139 
2140 int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
2141 			     struct iwl_mvm_int_sta *sta,
2142 			     u32 qmask, enum nl80211_iftype iftype,
2143 			     u8 type)
2144 {
2145 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
2146 	    sta->sta_id == IWL_MVM_INVALID_STA) {
2147 		sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
2148 		if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
2149 			return -ENOSPC;
2150 	}
2151 
2152 	sta->tfd_queue_msk = qmask;
2153 	sta->type = type;
2154 
2155 	/* put a non-NULL value so iterating over the stations won't stop */
2156 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
2157 	return 0;
2158 }
2159 
2160 void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
2161 {
2162 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
2163 	memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
2164 	sta->sta_id = IWL_MVM_INVALID_STA;
2165 }
2166 
2167 static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 queue,
2168 					  u8 sta_id, u8 fifo)
2169 {
2170 	unsigned int wdg_timeout =
2171 		mvm->trans->trans_cfg->base_params->wd_timeout;
2172 	struct iwl_trans_txq_scd_cfg cfg = {
2173 		.fifo = fifo,
2174 		.sta_id = sta_id,
2175 		.tid = IWL_MAX_TID_COUNT,
2176 		.aggregate = false,
2177 		.frame_limit = IWL_FRAME_LIMIT,
2178 	};
2179 
2180 	WARN_ON(iwl_mvm_has_new_tx_api(mvm));
2181 
2182 	iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2183 }
2184 
2185 static int iwl_mvm_enable_aux_snif_queue_tvqm(struct iwl_mvm *mvm, u8 sta_id)
2186 {
2187 	unsigned int wdg_timeout =
2188 		mvm->trans->trans_cfg->base_params->wd_timeout;
2189 
2190 	WARN_ON(!iwl_mvm_has_new_tx_api(mvm));
2191 
2192 	return iwl_mvm_tvqm_enable_txq(mvm, NULL, sta_id, IWL_MAX_TID_COUNT,
2193 				       wdg_timeout);
2194 }
2195 
2196 static int iwl_mvm_add_int_sta_with_queue(struct iwl_mvm *mvm, int macidx,
2197 					  int maccolor, u8 *addr,
2198 					  struct iwl_mvm_int_sta *sta,
2199 					  u16 *queue, int fifo)
2200 {
2201 	int ret;
2202 
2203 	/* Map queue to fifo - needs to happen before adding station */
2204 	if (!iwl_mvm_has_new_tx_api(mvm))
2205 		iwl_mvm_enable_aux_snif_queue(mvm, *queue, sta->sta_id, fifo);
2206 
2207 	ret = iwl_mvm_add_int_sta_common(mvm, sta, addr, macidx, maccolor);
2208 	if (ret) {
2209 		if (!iwl_mvm_has_new_tx_api(mvm))
2210 			iwl_mvm_disable_txq(mvm, NULL, sta->sta_id, queue,
2211 					    IWL_MAX_TID_COUNT);
2212 		return ret;
2213 	}
2214 
2215 	/*
2216 	 * For 22000 firmware and on we cannot add queue to a station unknown
2217 	 * to firmware so enable queue here - after the station was added
2218 	 */
2219 	if (iwl_mvm_has_new_tx_api(mvm)) {
2220 		int txq;
2221 
2222 		txq = iwl_mvm_enable_aux_snif_queue_tvqm(mvm, sta->sta_id);
2223 		if (txq < 0) {
2224 			iwl_mvm_rm_sta_common(mvm, sta->sta_id);
2225 			return txq;
2226 		}
2227 
2228 		*queue = txq;
2229 	}
2230 
2231 	return 0;
2232 }
2233 
2234 int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm, u32 lmac_id)
2235 {
2236 	int ret;
2237 	u32 qmask = mvm->aux_queue == IWL_MVM_INVALID_QUEUE ? 0 :
2238 		BIT(mvm->aux_queue);
2239 
2240 	lockdep_assert_held(&mvm->mutex);
2241 
2242 	/* Allocate aux station and assign to it the aux queue */
2243 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, qmask,
2244 				       NL80211_IFTYPE_UNSPECIFIED,
2245 				       IWL_STA_AUX_ACTIVITY);
2246 	if (ret)
2247 		return ret;
2248 
2249 	/*
2250 	 * In CDB NICs we need to specify which lmac to use for aux activity
2251 	 * using the mac_id argument place to send lmac_id to the function
2252 	 */
2253 	ret = iwl_mvm_add_int_sta_with_queue(mvm, lmac_id, 0, NULL,
2254 					     &mvm->aux_sta, &mvm->aux_queue,
2255 					     IWL_MVM_TX_FIFO_MCAST);
2256 	if (ret) {
2257 		iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2258 		return ret;
2259 	}
2260 
2261 	return 0;
2262 }
2263 
2264 int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2265 {
2266 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2267 
2268 	lockdep_assert_held(&mvm->mutex);
2269 
2270 	return iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
2271 					      NULL, &mvm->snif_sta,
2272 					      &mvm->snif_queue,
2273 					      IWL_MVM_TX_FIFO_BE);
2274 }
2275 
2276 int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2277 {
2278 	int ret;
2279 
2280 	lockdep_assert_held(&mvm->mutex);
2281 
2282 	if (WARN_ON_ONCE(mvm->snif_sta.sta_id == IWL_MVM_INVALID_STA))
2283 		return -EINVAL;
2284 
2285 	iwl_mvm_disable_txq(mvm, NULL, mvm->snif_sta.sta_id,
2286 			    &mvm->snif_queue, IWL_MAX_TID_COUNT);
2287 	ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
2288 	if (ret)
2289 		IWL_WARN(mvm, "Failed sending remove station\n");
2290 
2291 	return ret;
2292 }
2293 
2294 int iwl_mvm_rm_aux_sta(struct iwl_mvm *mvm)
2295 {
2296 	int ret;
2297 
2298 	lockdep_assert_held(&mvm->mutex);
2299 
2300 	if (WARN_ON_ONCE(mvm->aux_sta.sta_id == IWL_MVM_INVALID_STA))
2301 		return -EINVAL;
2302 
2303 	iwl_mvm_disable_txq(mvm, NULL, mvm->aux_sta.sta_id,
2304 			    &mvm->aux_queue, IWL_MAX_TID_COUNT);
2305 	ret = iwl_mvm_rm_sta_common(mvm, mvm->aux_sta.sta_id);
2306 	if (ret)
2307 		IWL_WARN(mvm, "Failed sending remove station\n");
2308 	iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2309 
2310 	return ret;
2311 }
2312 
2313 void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
2314 {
2315 	iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
2316 }
2317 
2318 /*
2319  * Send the add station command for the vif's broadcast station.
2320  * Assumes that the station was already allocated.
2321  *
2322  * @mvm: the mvm component
2323  * @vif: the interface to which the broadcast station is added
2324  * @bsta: the broadcast station to add.
2325  */
2326 int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2327 {
2328 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2329 	struct iwl_mvm_int_sta *bsta = &mvmvif->deflink.bcast_sta;
2330 	static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2331 	const u8 *baddr = _baddr;
2332 	int queue;
2333 	int ret;
2334 	unsigned int wdg_timeout =
2335 		iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2336 	struct iwl_trans_txq_scd_cfg cfg = {
2337 		.fifo = IWL_MVM_TX_FIFO_VO,
2338 		.sta_id = mvmvif->deflink.bcast_sta.sta_id,
2339 		.tid = IWL_MAX_TID_COUNT,
2340 		.aggregate = false,
2341 		.frame_limit = IWL_FRAME_LIMIT,
2342 	};
2343 
2344 	lockdep_assert_held(&mvm->mutex);
2345 
2346 	if (!iwl_mvm_has_new_tx_api(mvm)) {
2347 		if (vif->type == NL80211_IFTYPE_AP ||
2348 		    vif->type == NL80211_IFTYPE_ADHOC) {
2349 			queue = mvm->probe_queue;
2350 		} else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2351 			queue = mvm->p2p_dev_queue;
2352 		} else {
2353 			WARN(1, "Missing required TXQ for adding bcast STA\n");
2354 			return -EINVAL;
2355 		}
2356 
2357 		bsta->tfd_queue_msk |= BIT(queue);
2358 
2359 		iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2360 	}
2361 
2362 	if (vif->type == NL80211_IFTYPE_ADHOC)
2363 		baddr = vif->bss_conf.bssid;
2364 
2365 	if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
2366 		return -ENOSPC;
2367 
2368 	ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2369 					 mvmvif->id, mvmvif->color);
2370 	if (ret)
2371 		return ret;
2372 
2373 	/*
2374 	 * For 22000 firmware and on we cannot add queue to a station unknown
2375 	 * to firmware so enable queue here - after the station was added
2376 	 */
2377 	if (iwl_mvm_has_new_tx_api(mvm)) {
2378 		queue = iwl_mvm_tvqm_enable_txq(mvm, NULL, bsta->sta_id,
2379 						IWL_MAX_TID_COUNT,
2380 						wdg_timeout);
2381 		if (queue < 0) {
2382 			iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
2383 			return queue;
2384 		}
2385 
2386 		if (vif->type == NL80211_IFTYPE_AP ||
2387 		    vif->type == NL80211_IFTYPE_ADHOC) {
2388 			/* for queue management */
2389 			mvm->probe_queue = queue;
2390 			/* for use in TX */
2391 			mvmvif->deflink.mgmt_queue = queue;
2392 		} else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2393 			mvm->p2p_dev_queue = queue;
2394 		}
2395 	} else if (vif->type == NL80211_IFTYPE_AP ||
2396 		   vif->type == NL80211_IFTYPE_ADHOC) {
2397 		/* set it for use in TX */
2398 		mvmvif->deflink.mgmt_queue = mvm->probe_queue;
2399 	}
2400 
2401 	return 0;
2402 }
2403 
2404 void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2405 				   struct ieee80211_vif *vif)
2406 {
2407 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2408 	u16 *queueptr, queue;
2409 
2410 	lockdep_assert_held(&mvm->mutex);
2411 
2412 	iwl_mvm_flush_sta(mvm, &mvmvif->deflink.bcast_sta, true);
2413 
2414 	switch (vif->type) {
2415 	case NL80211_IFTYPE_AP:
2416 	case NL80211_IFTYPE_ADHOC:
2417 		queueptr = &mvm->probe_queue;
2418 		break;
2419 	case NL80211_IFTYPE_P2P_DEVICE:
2420 		queueptr = &mvm->p2p_dev_queue;
2421 		break;
2422 	default:
2423 		WARN(1, "Can't free bcast queue on vif type %d\n",
2424 		     vif->type);
2425 		return;
2426 	}
2427 
2428 	queue = *queueptr;
2429 	iwl_mvm_disable_txq(mvm, NULL, mvmvif->deflink.bcast_sta.sta_id,
2430 			    queueptr, IWL_MAX_TID_COUNT);
2431 
2432 	if (vif->type == NL80211_IFTYPE_AP || vif->type == NL80211_IFTYPE_ADHOC)
2433 		mvmvif->deflink.mgmt_queue = mvm->probe_queue;
2434 
2435 	if (iwl_mvm_has_new_tx_api(mvm))
2436 		return;
2437 
2438 	WARN_ON(!(mvmvif->deflink.bcast_sta.tfd_queue_msk & BIT(queue)));
2439 	mvmvif->deflink.bcast_sta.tfd_queue_msk &= ~BIT(queue);
2440 }
2441 
2442 /* Send the FW a request to remove the station from it's internal data
2443  * structures, but DO NOT remove the entry from the local data structures. */
2444 int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2445 {
2446 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2447 	int ret;
2448 
2449 	lockdep_assert_held(&mvm->mutex);
2450 
2451 	iwl_mvm_free_bcast_sta_queues(mvm, vif);
2452 
2453 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->deflink.bcast_sta.sta_id);
2454 	if (ret)
2455 		IWL_WARN(mvm, "Failed sending remove station\n");
2456 	return ret;
2457 }
2458 
2459 int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2460 {
2461 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2462 
2463 	lockdep_assert_held(&mvm->mutex);
2464 
2465 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->deflink.bcast_sta, 0,
2466 					ieee80211_vif_type_p2p(vif),
2467 					IWL_STA_GENERAL_PURPOSE);
2468 }
2469 
2470 /* Allocate a new station entry for the broadcast station to the given vif,
2471  * and send it to the FW.
2472  * Note that each P2P mac should have its own broadcast station.
2473  *
2474  * @mvm: the mvm component
2475  * @vif: the interface to which the broadcast station is added
2476  * @bsta: the broadcast station to add. */
2477 int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2478 {
2479 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2480 	struct iwl_mvm_int_sta *bsta = &mvmvif->deflink.bcast_sta;
2481 	int ret;
2482 
2483 	lockdep_assert_held(&mvm->mutex);
2484 
2485 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2486 	if (ret)
2487 		return ret;
2488 
2489 	ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2490 
2491 	if (ret)
2492 		iwl_mvm_dealloc_int_sta(mvm, bsta);
2493 
2494 	return ret;
2495 }
2496 
2497 void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2498 {
2499 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2500 
2501 	iwl_mvm_dealloc_int_sta(mvm, &mvmvif->deflink.bcast_sta);
2502 }
2503 
2504 /*
2505  * Send the FW a request to remove the station from it's internal data
2506  * structures, and in addition remove it from the local data structure.
2507  */
2508 int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2509 {
2510 	int ret;
2511 
2512 	lockdep_assert_held(&mvm->mutex);
2513 
2514 	ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2515 
2516 	iwl_mvm_dealloc_bcast_sta(mvm, vif);
2517 
2518 	return ret;
2519 }
2520 
2521 /*
2522  * Allocate a new station entry for the multicast station to the given vif,
2523  * and send it to the FW.
2524  * Note that each AP/GO mac should have its own multicast station.
2525  *
2526  * @mvm: the mvm component
2527  * @vif: the interface to which the multicast station is added
2528  */
2529 int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2530 {
2531 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2532 	struct iwl_mvm_int_sta *msta = &mvmvif->deflink.mcast_sta;
2533 	static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
2534 	const u8 *maddr = _maddr;
2535 	struct iwl_trans_txq_scd_cfg cfg = {
2536 		.fifo = vif->type == NL80211_IFTYPE_AP ?
2537 			IWL_MVM_TX_FIFO_MCAST : IWL_MVM_TX_FIFO_BE,
2538 		.sta_id = msta->sta_id,
2539 		.tid = 0,
2540 		.aggregate = false,
2541 		.frame_limit = IWL_FRAME_LIMIT,
2542 	};
2543 	unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2544 	int ret;
2545 
2546 	lockdep_assert_held(&mvm->mutex);
2547 
2548 	if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2549 		    vif->type != NL80211_IFTYPE_ADHOC))
2550 		return -ENOTSUPP;
2551 
2552 	/*
2553 	 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2554 	 * invalid, so make sure we use the queue we want.
2555 	 * Note that this is done here as we want to avoid making DQA
2556 	 * changes in mac80211 layer.
2557 	 */
2558 	if (vif->type == NL80211_IFTYPE_ADHOC)
2559 		mvmvif->deflink.cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2560 
2561 	/*
2562 	 * While in previous FWs we had to exclude cab queue from TFD queue
2563 	 * mask, now it is needed as any other queue.
2564 	 */
2565 	if (!iwl_mvm_has_new_tx_api(mvm) &&
2566 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2567 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->deflink.cab_queue, 0,
2568 				   &cfg,
2569 				   timeout);
2570 		msta->tfd_queue_msk |= BIT(mvmvif->deflink.cab_queue);
2571 	}
2572 	ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
2573 					 mvmvif->id, mvmvif->color);
2574 	if (ret)
2575 		goto err;
2576 
2577 	/*
2578 	 * Enable cab queue after the ADD_STA command is sent.
2579 	 * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
2580 	 * command with unknown station id, and for FW that doesn't support
2581 	 * station API since the cab queue is not included in the
2582 	 * tfd_queue_mask.
2583 	 */
2584 	if (iwl_mvm_has_new_tx_api(mvm)) {
2585 		int queue = iwl_mvm_tvqm_enable_txq(mvm, NULL, msta->sta_id,
2586 						    0, timeout);
2587 		if (queue < 0) {
2588 			ret = queue;
2589 			goto err;
2590 		}
2591 		mvmvif->deflink.cab_queue = queue;
2592 	} else if (!fw_has_api(&mvm->fw->ucode_capa,
2593 			       IWL_UCODE_TLV_API_STA_TYPE))
2594 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->deflink.cab_queue, 0,
2595 				   &cfg,
2596 				   timeout);
2597 
2598 	return 0;
2599 err:
2600 	iwl_mvm_dealloc_int_sta(mvm, msta);
2601 	return ret;
2602 }
2603 
2604 static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
2605 				    struct ieee80211_key_conf *keyconf,
2606 				    bool mcast)
2607 {
2608 	union {
2609 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
2610 		struct iwl_mvm_add_sta_key_cmd cmd;
2611 	} u = {};
2612 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
2613 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
2614 	__le16 key_flags;
2615 	int ret, size;
2616 	u32 status;
2617 
2618 	/* This is a valid situation for GTK removal */
2619 	if (sta_id == IWL_MVM_INVALID_STA)
2620 		return 0;
2621 
2622 	key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2623 				 STA_KEY_FLG_KEYID_MSK);
2624 	key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2625 	key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2626 
2627 	if (mcast)
2628 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2629 
2630 	/*
2631 	 * The fields assigned here are in the same location at the start
2632 	 * of the command, so we can do this union trick.
2633 	 */
2634 	u.cmd.common.key_flags = key_flags;
2635 	u.cmd.common.key_offset = keyconf->hw_key_idx;
2636 	u.cmd.common.sta_id = sta_id;
2637 
2638 	size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
2639 
2640 	status = ADD_STA_SUCCESS;
2641 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
2642 					  &status);
2643 
2644 	switch (status) {
2645 	case ADD_STA_SUCCESS:
2646 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2647 		break;
2648 	default:
2649 		ret = -EIO;
2650 		IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2651 		break;
2652 	}
2653 
2654 	return ret;
2655 }
2656 
2657 /*
2658  * Send the FW a request to remove the station from it's internal data
2659  * structures, and in addition remove it from the local data structure.
2660  */
2661 int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2662 {
2663 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2664 	int ret;
2665 
2666 	lockdep_assert_held(&mvm->mutex);
2667 
2668 	iwl_mvm_flush_sta(mvm, &mvmvif->deflink.mcast_sta, true);
2669 
2670 	iwl_mvm_disable_txq(mvm, NULL, mvmvif->deflink.mcast_sta.sta_id,
2671 			    &mvmvif->deflink.cab_queue, 0);
2672 
2673 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->deflink.mcast_sta.sta_id);
2674 	if (ret)
2675 		IWL_WARN(mvm, "Failed sending remove station\n");
2676 
2677 	return ret;
2678 }
2679 
2680 static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
2681 {
2682 	struct iwl_mvm_delba_data notif = {
2683 		.baid = baid,
2684 	};
2685 
2686 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_NOTIF_DEL_BA, true,
2687 					&notif, sizeof(notif));
2688 };
2689 
2690 static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2691 				 struct iwl_mvm_baid_data *data)
2692 {
2693 	int i;
2694 
2695 	iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2696 
2697 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2698 		int j;
2699 		struct iwl_mvm_reorder_buffer *reorder_buf =
2700 			&data->reorder_buf[i];
2701 		struct iwl_mvm_reorder_buf_entry *entries =
2702 			&data->entries[i * data->entries_per_queue];
2703 
2704 		spin_lock_bh(&reorder_buf->lock);
2705 		if (likely(!reorder_buf->num_stored)) {
2706 			spin_unlock_bh(&reorder_buf->lock);
2707 			continue;
2708 		}
2709 
2710 		/*
2711 		 * This shouldn't happen in regular DELBA since the internal
2712 		 * delBA notification should trigger a release of all frames in
2713 		 * the reorder buffer.
2714 		 */
2715 		WARN_ON(1);
2716 
2717 		for (j = 0; j < reorder_buf->buf_size; j++)
2718 			__skb_queue_purge(&entries[j].e.frames);
2719 		/*
2720 		 * Prevent timer re-arm. This prevents a very far fetched case
2721 		 * where we timed out on the notification. There may be prior
2722 		 * RX frames pending in the RX queue before the notification
2723 		 * that might get processed between now and the actual deletion
2724 		 * and we would re-arm the timer although we are deleting the
2725 		 * reorder buffer.
2726 		 */
2727 		reorder_buf->removed = true;
2728 		spin_unlock_bh(&reorder_buf->lock);
2729 		del_timer_sync(&reorder_buf->reorder_timer);
2730 	}
2731 }
2732 
2733 static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2734 					struct iwl_mvm_baid_data *data,
2735 					u16 ssn, u16 buf_size)
2736 {
2737 	int i;
2738 
2739 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2740 		struct iwl_mvm_reorder_buffer *reorder_buf =
2741 			&data->reorder_buf[i];
2742 		struct iwl_mvm_reorder_buf_entry *entries =
2743 			&data->entries[i * data->entries_per_queue];
2744 		int j;
2745 
2746 		reorder_buf->num_stored = 0;
2747 		reorder_buf->head_sn = ssn;
2748 		reorder_buf->buf_size = buf_size;
2749 		/* rx reorder timer */
2750 		timer_setup(&reorder_buf->reorder_timer,
2751 			    iwl_mvm_reorder_timer_expired, 0);
2752 		spin_lock_init(&reorder_buf->lock);
2753 		reorder_buf->mvm = mvm;
2754 		reorder_buf->queue = i;
2755 		reorder_buf->valid = false;
2756 		for (j = 0; j < reorder_buf->buf_size; j++)
2757 			__skb_queue_head_init(&entries[j].e.frames);
2758 	}
2759 }
2760 
2761 static int iwl_mvm_fw_baid_op_sta(struct iwl_mvm *mvm,
2762 				  struct ieee80211_sta *sta,
2763 				  bool start, int tid, u16 ssn,
2764 				  u16 buf_size)
2765 {
2766 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2767 	struct iwl_mvm_add_sta_cmd cmd = {
2768 		.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
2769 		.sta_id = mvm_sta->deflink.sta_id,
2770 		.add_modify = STA_MODE_MODIFY,
2771 	};
2772 	u32 status;
2773 	int ret;
2774 
2775 	if (start) {
2776 		cmd.add_immediate_ba_tid = tid;
2777 		cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
2778 		cmd.rx_ba_window = cpu_to_le16(buf_size);
2779 		cmd.modify_mask = STA_MODIFY_ADD_BA_TID;
2780 	} else {
2781 		cmd.remove_immediate_ba_tid = tid;
2782 		cmd.modify_mask = STA_MODIFY_REMOVE_BA_TID;
2783 	}
2784 
2785 	status = ADD_STA_SUCCESS;
2786 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2787 					  iwl_mvm_add_sta_cmd_size(mvm),
2788 					  &cmd, &status);
2789 	if (ret)
2790 		return ret;
2791 
2792 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2793 	case ADD_STA_SUCCESS:
2794 		IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2795 			     start ? "start" : "stopp");
2796 		if (WARN_ON(start && iwl_mvm_has_new_rx_api(mvm) &&
2797 			    !(status & IWL_ADD_STA_BAID_VALID_MASK)))
2798 			return -EINVAL;
2799 		return u32_get_bits(status, IWL_ADD_STA_BAID_MASK);
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 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, RX_BAID_ALLOCATION_CONFIG_CMD);
2820 	int ret;
2821 
2822 	BUILD_BUG_ON(sizeof(struct iwl_rx_baid_cfg_resp) != sizeof(baid));
2823 
2824 	if (start) {
2825 		cmd.alloc.sta_id_mask =
2826 			cpu_to_le32(iwl_mvm_sta_fw_id_mask(mvm, sta, -1));
2827 		cmd.alloc.tid = tid;
2828 		cmd.alloc.ssn = cpu_to_le16(ssn);
2829 		cmd.alloc.win_size = cpu_to_le16(buf_size);
2830 		baid = -EIO;
2831 	} else if (iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 1) == 1) {
2832 		cmd.remove_v1.baid = cpu_to_le32(baid);
2833 		BUILD_BUG_ON(sizeof(cmd.remove_v1) > sizeof(cmd.remove));
2834 	} else {
2835 		cmd.remove.sta_id_mask =
2836 			cpu_to_le32(iwl_mvm_sta_fw_id_mask(mvm, sta, -1));
2837 		cmd.remove.tid = cpu_to_le32(tid);
2838 	}
2839 
2840 	ret = iwl_mvm_send_cmd_pdu_status(mvm, cmd_id, sizeof(cmd),
2841 					  &cmd, &baid);
2842 	if (ret)
2843 		return ret;
2844 
2845 	if (!start) {
2846 		/* ignore firmware baid on remove */
2847 		baid = 0;
2848 	}
2849 
2850 	IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2851 		     start ? "start" : "stopp");
2852 
2853 	if (baid < 0 || baid >= ARRAY_SIZE(mvm->baid_map))
2854 		return -EINVAL;
2855 
2856 	return baid;
2857 }
2858 
2859 static int iwl_mvm_fw_baid_op(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2860 			      bool start, int tid, u16 ssn, u16 buf_size,
2861 			      int baid)
2862 {
2863 	if (fw_has_capa(&mvm->fw->ucode_capa,
2864 			IWL_UCODE_TLV_CAPA_BAID_ML_SUPPORT))
2865 		return iwl_mvm_fw_baid_op_cmd(mvm, sta, start,
2866 					      tid, ssn, buf_size, baid);
2867 
2868 	return iwl_mvm_fw_baid_op_sta(mvm, sta, start,
2869 				      tid, ssn, buf_size);
2870 }
2871 
2872 int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2873 		       int tid, u16 ssn, bool start, u16 buf_size, u16 timeout)
2874 {
2875 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2876 	struct iwl_mvm_baid_data *baid_data = NULL;
2877 	int ret, baid;
2878 	u32 max_ba_id_sessions = iwl_mvm_has_new_tx_api(mvm) ? IWL_MAX_BAID :
2879 							       IWL_MAX_BAID_OLD;
2880 
2881 	lockdep_assert_held(&mvm->mutex);
2882 
2883 	if (start && mvm->rx_ba_sessions >= max_ba_id_sessions) {
2884 		IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2885 		return -ENOSPC;
2886 	}
2887 
2888 	if (iwl_mvm_has_new_rx_api(mvm) && start) {
2889 		u32 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2890 
2891 		/* sparse doesn't like the __align() so don't check */
2892 #ifndef __CHECKER__
2893 		/*
2894 		 * The division below will be OK if either the cache line size
2895 		 * can be divided by the entry size (ALIGN will round up) or if
2896 		 * if the entry size can be divided by the cache line size, in
2897 		 * which case the ALIGN() will do nothing.
2898 		 */
2899 		BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2900 			     sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2901 #endif
2902 
2903 		/*
2904 		 * Upward align the reorder buffer size to fill an entire cache
2905 		 * line for each queue, to avoid sharing cache lines between
2906 		 * different queues.
2907 		 */
2908 		reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2909 
2910 		/*
2911 		 * Allocate here so if allocation fails we can bail out early
2912 		 * before starting the BA session in the firmware
2913 		 */
2914 		baid_data = kzalloc(sizeof(*baid_data) +
2915 				    mvm->trans->num_rx_queues *
2916 				    reorder_buf_size,
2917 				    GFP_KERNEL);
2918 		if (!baid_data)
2919 			return -ENOMEM;
2920 
2921 		/*
2922 		 * This division is why we need the above BUILD_BUG_ON(),
2923 		 * if that doesn't hold then this will not be right.
2924 		 */
2925 		baid_data->entries_per_queue =
2926 			reorder_buf_size / sizeof(baid_data->entries[0]);
2927 	}
2928 
2929 	if (iwl_mvm_has_new_rx_api(mvm) && !start) {
2930 		baid = mvm_sta->tid_to_baid[tid];
2931 	} else {
2932 		/* we don't really need it in this case */
2933 		baid = -1;
2934 	}
2935 
2936 	/* Don't send command to remove (start=0) BAID during restart */
2937 	if (start || !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
2938 		baid = iwl_mvm_fw_baid_op(mvm, sta, start, tid, ssn, buf_size,
2939 					  baid);
2940 
2941 	if (baid < 0) {
2942 		ret = baid;
2943 		goto out_free;
2944 	}
2945 
2946 	if (start) {
2947 		mvm->rx_ba_sessions++;
2948 
2949 		if (!iwl_mvm_has_new_rx_api(mvm))
2950 			return 0;
2951 
2952 		baid_data->baid = baid;
2953 		baid_data->timeout = timeout;
2954 		baid_data->last_rx = jiffies;
2955 		baid_data->rcu_ptr = &mvm->baid_map[baid];
2956 		timer_setup(&baid_data->session_timer,
2957 			    iwl_mvm_rx_agg_session_expired, 0);
2958 		baid_data->mvm = mvm;
2959 		baid_data->tid = tid;
2960 		baid_data->sta_mask = iwl_mvm_sta_fw_id_mask(mvm, sta, -1);
2961 
2962 		mvm_sta->tid_to_baid[tid] = baid;
2963 		if (timeout)
2964 			mod_timer(&baid_data->session_timer,
2965 				  TU_TO_EXP_TIME(timeout * 2));
2966 
2967 		iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size);
2968 		/*
2969 		 * protect the BA data with RCU to cover a case where our
2970 		 * internal RX sync mechanism will timeout (not that it's
2971 		 * supposed to happen) and we will free the session data while
2972 		 * RX is being processed in parallel
2973 		 */
2974 		IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2975 			     mvm_sta->deflink.sta_id, tid, baid);
2976 		WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2977 		rcu_assign_pointer(mvm->baid_map[baid], baid_data);
2978 	} else  {
2979 		baid = mvm_sta->tid_to_baid[tid];
2980 
2981 		if (mvm->rx_ba_sessions > 0)
2982 			/* check that restart flow didn't zero the counter */
2983 			mvm->rx_ba_sessions--;
2984 		if (!iwl_mvm_has_new_rx_api(mvm))
2985 			return 0;
2986 
2987 		if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2988 			return -EINVAL;
2989 
2990 		baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2991 		if (WARN_ON(!baid_data))
2992 			return -EINVAL;
2993 
2994 		/* synchronize all rx queues so we can safely delete */
2995 		iwl_mvm_free_reorder(mvm, baid_data);
2996 		timer_shutdown_sync(&baid_data->session_timer);
2997 		RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2998 		kfree_rcu(baid_data, rcu_head);
2999 		IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
3000 
3001 		/*
3002 		 * After we've deleted it, do another queue sync
3003 		 * so if an IWL_MVM_RXQ_NSSN_SYNC was concurrently
3004 		 * running it won't find a new session in the old
3005 		 * BAID. It can find the NULL pointer for the BAID,
3006 		 * but we must not have it find a different session.
3007 		 */
3008 		iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY,
3009 						true, NULL, 0);
3010 	}
3011 	return 0;
3012 
3013 out_free:
3014 	kfree(baid_data);
3015 	return ret;
3016 }
3017 
3018 int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
3019 		       int tid, u8 queue, bool start)
3020 {
3021 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3022 	struct iwl_mvm_add_sta_cmd cmd = {};
3023 	int ret;
3024 	u32 status;
3025 
3026 	lockdep_assert_held(&mvm->mutex);
3027 
3028 	if (start) {
3029 		mvm_sta->tfd_queue_msk |= BIT(queue);
3030 		mvm_sta->tid_disable_agg &= ~BIT(tid);
3031 	} else {
3032 		/* In DQA-mode the queue isn't removed on agg termination */
3033 		mvm_sta->tid_disable_agg |= BIT(tid);
3034 	}
3035 
3036 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
3037 	cmd.sta_id = mvm_sta->deflink.sta_id;
3038 	cmd.add_modify = STA_MODE_MODIFY;
3039 	if (!iwl_mvm_has_new_tx_api(mvm))
3040 		cmd.modify_mask = STA_MODIFY_QUEUES;
3041 	cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
3042 	cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
3043 	cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
3044 
3045 	status = ADD_STA_SUCCESS;
3046 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
3047 					  iwl_mvm_add_sta_cmd_size(mvm),
3048 					  &cmd, &status);
3049 	if (ret)
3050 		return ret;
3051 
3052 	switch (status & IWL_ADD_STA_STATUS_MASK) {
3053 	case ADD_STA_SUCCESS:
3054 		break;
3055 	default:
3056 		ret = -EIO;
3057 		IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
3058 			start ? "start" : "stopp", status);
3059 		break;
3060 	}
3061 
3062 	return ret;
3063 }
3064 
3065 const u8 tid_to_mac80211_ac[] = {
3066 	IEEE80211_AC_BE,
3067 	IEEE80211_AC_BK,
3068 	IEEE80211_AC_BK,
3069 	IEEE80211_AC_BE,
3070 	IEEE80211_AC_VI,
3071 	IEEE80211_AC_VI,
3072 	IEEE80211_AC_VO,
3073 	IEEE80211_AC_VO,
3074 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
3075 };
3076 
3077 static const u8 tid_to_ucode_ac[] = {
3078 	AC_BE,
3079 	AC_BK,
3080 	AC_BK,
3081 	AC_BE,
3082 	AC_VI,
3083 	AC_VI,
3084 	AC_VO,
3085 	AC_VO,
3086 };
3087 
3088 int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3089 			     struct ieee80211_sta *sta, u16 tid, u16 *ssn)
3090 {
3091 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3092 	struct iwl_mvm_tid_data *tid_data;
3093 	u16 normalized_ssn;
3094 	u16 txq_id;
3095 	int ret;
3096 
3097 	if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
3098 		return -EINVAL;
3099 
3100 	if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
3101 	    mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
3102 		IWL_ERR(mvm,
3103 			"Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
3104 			mvmsta->tid_data[tid].state);
3105 		return -ENXIO;
3106 	}
3107 
3108 	lockdep_assert_held(&mvm->mutex);
3109 
3110 	if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE &&
3111 	    iwl_mvm_has_new_tx_api(mvm)) {
3112 		u8 ac = tid_to_mac80211_ac[tid];
3113 
3114 		ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
3115 		if (ret)
3116 			return ret;
3117 	}
3118 
3119 	spin_lock_bh(&mvmsta->lock);
3120 
3121 	/*
3122 	 * Note the possible cases:
3123 	 *  1. An enabled TXQ - TXQ needs to become agg'ed
3124 	 *  2. The TXQ hasn't yet been enabled, so find a free one and mark
3125 	 *	it as reserved
3126 	 */
3127 	txq_id = mvmsta->tid_data[tid].txq_id;
3128 	if (txq_id == IWL_MVM_INVALID_QUEUE) {
3129 		ret = iwl_mvm_find_free_queue(mvm, mvmsta->deflink.sta_id,
3130 					      IWL_MVM_DQA_MIN_DATA_QUEUE,
3131 					      IWL_MVM_DQA_MAX_DATA_QUEUE);
3132 		if (ret < 0) {
3133 			IWL_ERR(mvm, "Failed to allocate agg queue\n");
3134 			goto out;
3135 		}
3136 
3137 		txq_id = ret;
3138 
3139 		/* TXQ hasn't yet been enabled, so mark it only as reserved */
3140 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
3141 	} else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) {
3142 		ret = -ENXIO;
3143 		IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n",
3144 			tid, IWL_MAX_HW_QUEUES - 1);
3145 		goto out;
3146 
3147 	} else if (unlikely(mvm->queue_info[txq_id].status ==
3148 			    IWL_MVM_QUEUE_SHARED)) {
3149 		ret = -ENXIO;
3150 		IWL_DEBUG_TX_QUEUES(mvm,
3151 				    "Can't start tid %d agg on shared queue!\n",
3152 				    tid);
3153 		goto out;
3154 	}
3155 
3156 	IWL_DEBUG_TX_QUEUES(mvm,
3157 			    "AGG for tid %d will be on queue #%d\n",
3158 			    tid, txq_id);
3159 
3160 	tid_data = &mvmsta->tid_data[tid];
3161 	tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3162 	tid_data->txq_id = txq_id;
3163 	*ssn = tid_data->ssn;
3164 
3165 	IWL_DEBUG_TX_QUEUES(mvm,
3166 			    "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
3167 			    mvmsta->deflink.sta_id, tid, txq_id,
3168 			    tid_data->ssn,
3169 			    tid_data->next_reclaimed);
3170 
3171 	/*
3172 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
3173 	 * to align the wrap around of ssn so we compare relevant values.
3174 	 */
3175 	normalized_ssn = tid_data->ssn;
3176 	if (mvm->trans->trans_cfg->gen2)
3177 		normalized_ssn &= 0xff;
3178 
3179 	if (normalized_ssn == tid_data->next_reclaimed) {
3180 		tid_data->state = IWL_AGG_STARTING;
3181 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
3182 	} else {
3183 		tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
3184 		ret = IEEE80211_AMPDU_TX_START_DELAY_ADDBA;
3185 	}
3186 
3187 out:
3188 	spin_unlock_bh(&mvmsta->lock);
3189 
3190 	return ret;
3191 }
3192 
3193 int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3194 			    struct ieee80211_sta *sta, u16 tid, u16 buf_size,
3195 			    bool amsdu)
3196 {
3197 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3198 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3199 	unsigned int wdg_timeout =
3200 		iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
3201 	int queue, ret;
3202 	bool alloc_queue = true;
3203 	enum iwl_mvm_queue_status queue_status;
3204 	u16 ssn;
3205 
3206 	struct iwl_trans_txq_scd_cfg cfg = {
3207 		.sta_id = mvmsta->deflink.sta_id,
3208 		.tid = tid,
3209 		.frame_limit = buf_size,
3210 		.aggregate = true,
3211 	};
3212 
3213 	/*
3214 	 * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
3215 	 * manager, so this function should never be called in this case.
3216 	 */
3217 	if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
3218 		return -EINVAL;
3219 
3220 	BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
3221 		     != IWL_MAX_TID_COUNT);
3222 
3223 	spin_lock_bh(&mvmsta->lock);
3224 	ssn = tid_data->ssn;
3225 	queue = tid_data->txq_id;
3226 	tid_data->state = IWL_AGG_ON;
3227 	mvmsta->agg_tids |= BIT(tid);
3228 	tid_data->ssn = 0xffff;
3229 	tid_data->amsdu_in_ampdu_allowed = amsdu;
3230 	spin_unlock_bh(&mvmsta->lock);
3231 
3232 	if (iwl_mvm_has_new_tx_api(mvm)) {
3233 		/*
3234 		 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
3235 		 * would have failed, so if we are here there is no need to
3236 		 * allocate a queue.
3237 		 * However, if aggregation size is different than the default
3238 		 * size, the scheduler should be reconfigured.
3239 		 * We cannot do this with the new TX API, so return unsupported
3240 		 * for now, until it will be offloaded to firmware..
3241 		 * Note that if SCD default value changes - this condition
3242 		 * should be updated as well.
3243 		 */
3244 		if (buf_size < IWL_FRAME_LIMIT)
3245 			return -ENOTSUPP;
3246 
3247 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
3248 		if (ret)
3249 			return -EIO;
3250 		goto out;
3251 	}
3252 
3253 	cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
3254 
3255 	queue_status = mvm->queue_info[queue].status;
3256 
3257 	/* Maybe there is no need to even alloc a queue... */
3258 	if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
3259 		alloc_queue = false;
3260 
3261 	/*
3262 	 * Only reconfig the SCD for the queue if the window size has
3263 	 * changed from current (become smaller)
3264 	 */
3265 	if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
3266 		/*
3267 		 * If reconfiguring an existing queue, it first must be
3268 		 * drained
3269 		 */
3270 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
3271 						     BIT(queue));
3272 		if (ret) {
3273 			IWL_ERR(mvm,
3274 				"Error draining queue before reconfig\n");
3275 			return ret;
3276 		}
3277 
3278 		ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
3279 					   mvmsta->deflink.sta_id, tid,
3280 					   buf_size, ssn);
3281 		if (ret) {
3282 			IWL_ERR(mvm,
3283 				"Error reconfiguring TXQ #%d\n", queue);
3284 			return ret;
3285 		}
3286 	}
3287 
3288 	if (alloc_queue)
3289 		iwl_mvm_enable_txq(mvm, sta, queue, ssn,
3290 				   &cfg, wdg_timeout);
3291 
3292 	/* Send ADD_STA command to enable aggs only if the queue isn't shared */
3293 	if (queue_status != IWL_MVM_QUEUE_SHARED) {
3294 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
3295 		if (ret)
3296 			return -EIO;
3297 	}
3298 
3299 	/* No need to mark as reserved */
3300 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
3301 
3302 out:
3303 	/*
3304 	 * Even though in theory the peer could have different
3305 	 * aggregation reorder buffer sizes for different sessions,
3306 	 * our ucode doesn't allow for that and has a global limit
3307 	 * for each station. Therefore, use the minimum of all the
3308 	 * aggregation sessions and our default value.
3309 	 */
3310 	mvmsta->deflink.lq_sta.rs_drv.pers.max_agg_bufsize =
3311 		min(mvmsta->deflink.lq_sta.rs_drv.pers.max_agg_bufsize,
3312 		    buf_size);
3313 	mvmsta->deflink.lq_sta.rs_drv.lq.agg_frame_cnt_limit =
3314 		mvmsta->deflink.lq_sta.rs_drv.pers.max_agg_bufsize;
3315 
3316 	IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
3317 		     sta->addr, tid);
3318 
3319 	return iwl_mvm_send_lq_cmd(mvm, &mvmsta->deflink.lq_sta.rs_drv.lq);
3320 }
3321 
3322 static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
3323 					struct iwl_mvm_sta *mvmsta,
3324 					struct iwl_mvm_tid_data *tid_data)
3325 {
3326 	u16 txq_id = tid_data->txq_id;
3327 
3328 	lockdep_assert_held(&mvm->mutex);
3329 
3330 	if (iwl_mvm_has_new_tx_api(mvm))
3331 		return;
3332 
3333 	/*
3334 	 * The TXQ is marked as reserved only if no traffic came through yet
3335 	 * This means no traffic has been sent on this TID (agg'd or not), so
3336 	 * we no longer have use for the queue. Since it hasn't even been
3337 	 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
3338 	 * free.
3339 	 */
3340 	if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
3341 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
3342 		tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
3343 	}
3344 }
3345 
3346 int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3347 			    struct ieee80211_sta *sta, u16 tid)
3348 {
3349 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3350 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3351 	u16 txq_id;
3352 	int err;
3353 
3354 	/*
3355 	 * If mac80211 is cleaning its state, then say that we finished since
3356 	 * our state has been cleared anyway.
3357 	 */
3358 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3359 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3360 		return 0;
3361 	}
3362 
3363 	spin_lock_bh(&mvmsta->lock);
3364 
3365 	txq_id = tid_data->txq_id;
3366 
3367 	IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
3368 			    mvmsta->deflink.sta_id, tid, txq_id,
3369 			    tid_data->state);
3370 
3371 	mvmsta->agg_tids &= ~BIT(tid);
3372 
3373 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3374 
3375 	switch (tid_data->state) {
3376 	case IWL_AGG_ON:
3377 		tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3378 
3379 		IWL_DEBUG_TX_QUEUES(mvm,
3380 				    "ssn = %d, next_recl = %d\n",
3381 				    tid_data->ssn, tid_data->next_reclaimed);
3382 
3383 		tid_data->ssn = 0xffff;
3384 		tid_data->state = IWL_AGG_OFF;
3385 		spin_unlock_bh(&mvmsta->lock);
3386 
3387 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3388 
3389 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3390 		return 0;
3391 	case IWL_AGG_STARTING:
3392 	case IWL_EMPTYING_HW_QUEUE_ADDBA:
3393 		/*
3394 		 * The agg session has been stopped before it was set up. This
3395 		 * can happen when the AddBA timer times out for example.
3396 		 */
3397 
3398 		/* No barriers since we are under mutex */
3399 		lockdep_assert_held(&mvm->mutex);
3400 
3401 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3402 		tid_data->state = IWL_AGG_OFF;
3403 		err = 0;
3404 		break;
3405 	default:
3406 		IWL_ERR(mvm,
3407 			"Stopping AGG while state not ON or starting for %d on %d (%d)\n",
3408 			mvmsta->deflink.sta_id, tid, tid_data->state);
3409 		IWL_ERR(mvm,
3410 			"\ttid_data->txq_id = %d\n", tid_data->txq_id);
3411 		err = -EINVAL;
3412 	}
3413 
3414 	spin_unlock_bh(&mvmsta->lock);
3415 
3416 	return err;
3417 }
3418 
3419 int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3420 			    struct ieee80211_sta *sta, u16 tid)
3421 {
3422 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3423 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3424 	u16 txq_id;
3425 	enum iwl_mvm_agg_state old_state;
3426 
3427 	/*
3428 	 * First set the agg state to OFF to avoid calling
3429 	 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
3430 	 */
3431 	spin_lock_bh(&mvmsta->lock);
3432 	txq_id = tid_data->txq_id;
3433 	IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
3434 			    mvmsta->deflink.sta_id, tid, txq_id,
3435 			    tid_data->state);
3436 	old_state = tid_data->state;
3437 	tid_data->state = IWL_AGG_OFF;
3438 	mvmsta->agg_tids &= ~BIT(tid);
3439 	spin_unlock_bh(&mvmsta->lock);
3440 
3441 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3442 
3443 	if (old_state >= IWL_AGG_ON) {
3444 		iwl_mvm_drain_sta(mvm, mvmsta, true);
3445 
3446 		if (iwl_mvm_has_new_tx_api(mvm)) {
3447 			if (iwl_mvm_flush_sta_tids(mvm, mvmsta->deflink.sta_id,
3448 						   BIT(tid)))
3449 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3450 			iwl_trans_wait_txq_empty(mvm->trans, txq_id);
3451 		} else {
3452 			if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id)))
3453 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3454 			iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
3455 		}
3456 
3457 		iwl_mvm_drain_sta(mvm, mvmsta, false);
3458 
3459 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3460 	}
3461 
3462 	return 0;
3463 }
3464 
3465 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
3466 {
3467 	int i, max = -1, max_offs = -1;
3468 
3469 	lockdep_assert_held(&mvm->mutex);
3470 
3471 	/* Pick the unused key offset with the highest 'deleted'
3472 	 * counter. Every time a key is deleted, all the counters
3473 	 * are incremented and the one that was just deleted is
3474 	 * reset to zero. Thus, the highest counter is the one
3475 	 * that was deleted longest ago. Pick that one.
3476 	 */
3477 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3478 		if (test_bit(i, mvm->fw_key_table))
3479 			continue;
3480 		if (mvm->fw_key_deleted[i] > max) {
3481 			max = mvm->fw_key_deleted[i];
3482 			max_offs = i;
3483 		}
3484 	}
3485 
3486 	if (max_offs < 0)
3487 		return STA_KEY_IDX_INVALID;
3488 
3489 	return max_offs;
3490 }
3491 
3492 static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
3493 					       struct ieee80211_vif *vif,
3494 					       struct ieee80211_sta *sta)
3495 {
3496 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3497 
3498 	if (sta)
3499 		return iwl_mvm_sta_from_mac80211(sta);
3500 
3501 	/*
3502 	 * The device expects GTKs for station interfaces to be
3503 	 * installed as GTKs for the AP station. If we have no
3504 	 * station ID, then use AP's station ID.
3505 	 */
3506 	if (vif->type == NL80211_IFTYPE_STATION &&
3507 	    mvmvif->deflink.ap_sta_id != IWL_MVM_INVALID_STA) {
3508 		u8 sta_id = mvmvif->deflink.ap_sta_id;
3509 
3510 		sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
3511 					    lockdep_is_held(&mvm->mutex));
3512 
3513 		/*
3514 		 * It is possible that the 'sta' parameter is NULL,
3515 		 * for example when a GTK is removed - the sta_id will then
3516 		 * be the AP ID, and no station was passed by mac80211.
3517 		 */
3518 		if (IS_ERR_OR_NULL(sta))
3519 			return NULL;
3520 
3521 		return iwl_mvm_sta_from_mac80211(sta);
3522 	}
3523 
3524 	return NULL;
3525 }
3526 
3527 static int iwl_mvm_pn_cmp(const u8 *pn1, const u8 *pn2, int len)
3528 {
3529 	int i;
3530 
3531 	for (i = len - 1; i >= 0; i--) {
3532 		if (pn1[i] > pn2[i])
3533 			return 1;
3534 		if (pn1[i] < pn2[i])
3535 			return -1;
3536 	}
3537 
3538 	return 0;
3539 }
3540 
3541 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
3542 				u32 sta_id,
3543 				struct ieee80211_key_conf *key, bool mcast,
3544 				u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
3545 				u8 key_offset, bool mfp)
3546 {
3547 	union {
3548 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3549 		struct iwl_mvm_add_sta_key_cmd cmd;
3550 	} u = {};
3551 	__le16 key_flags;
3552 	int ret;
3553 	u32 status;
3554 	u16 keyidx;
3555 	u64 pn = 0;
3556 	int i, size;
3557 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3558 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3559 	int api_ver = iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA_KEY,
3560 					    new_api ? 2 : 1);
3561 
3562 	if (sta_id == IWL_MVM_INVALID_STA)
3563 		return -EINVAL;
3564 
3565 	keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
3566 		 STA_KEY_FLG_KEYID_MSK;
3567 	key_flags = cpu_to_le16(keyidx);
3568 	key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3569 
3570 	switch (key->cipher) {
3571 	case WLAN_CIPHER_SUITE_TKIP:
3572 		key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
3573 		if (api_ver >= 2) {
3574 			memcpy((void *)&u.cmd.tx_mic_key,
3575 			       &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
3576 			       IWL_MIC_KEY_SIZE);
3577 
3578 			memcpy((void *)&u.cmd.rx_mic_key,
3579 			       &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
3580 			       IWL_MIC_KEY_SIZE);
3581 			pn = atomic64_read(&key->tx_pn);
3582 
3583 		} else {
3584 			u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3585 			for (i = 0; i < 5; i++)
3586 				u.cmd_v1.tkip_rx_ttak[i] =
3587 					cpu_to_le16(tkip_p1k[i]);
3588 		}
3589 		memcpy(u.cmd.common.key, key->key, key->keylen);
3590 		break;
3591 	case WLAN_CIPHER_SUITE_CCMP:
3592 		key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
3593 		memcpy(u.cmd.common.key, key->key, key->keylen);
3594 		if (api_ver >= 2)
3595 			pn = atomic64_read(&key->tx_pn);
3596 		break;
3597 	case WLAN_CIPHER_SUITE_WEP104:
3598 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
3599 		fallthrough;
3600 	case WLAN_CIPHER_SUITE_WEP40:
3601 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
3602 		memcpy(u.cmd.common.key + 3, key->key, key->keylen);
3603 		break;
3604 	case WLAN_CIPHER_SUITE_GCMP_256:
3605 		key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
3606 		fallthrough;
3607 	case WLAN_CIPHER_SUITE_GCMP:
3608 		key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
3609 		memcpy(u.cmd.common.key, key->key, key->keylen);
3610 		if (api_ver >= 2)
3611 			pn = atomic64_read(&key->tx_pn);
3612 		break;
3613 	default:
3614 		key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
3615 		memcpy(u.cmd.common.key, key->key, key->keylen);
3616 	}
3617 
3618 	if (mcast)
3619 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3620 	if (mfp)
3621 		key_flags |= cpu_to_le16(STA_KEY_MFP);
3622 
3623 	u.cmd.common.key_offset = key_offset;
3624 	u.cmd.common.key_flags = key_flags;
3625 	u.cmd.common.sta_id = sta_id;
3626 
3627 	if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
3628 		i = 0;
3629 	else
3630 		i = -1;
3631 
3632 	for (; i < IEEE80211_NUM_TIDS; i++) {
3633 		struct ieee80211_key_seq seq = {};
3634 		u8 _rx_pn[IEEE80211_MAX_PN_LEN] = {}, *rx_pn = _rx_pn;
3635 		int rx_pn_len = 8;
3636 		/* there's a hole at 2/3 in FW format depending on version */
3637 		int hole = api_ver >= 3 ? 0 : 2;
3638 
3639 		ieee80211_get_key_rx_seq(key, i, &seq);
3640 
3641 		if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
3642 			rx_pn[0] = seq.tkip.iv16;
3643 			rx_pn[1] = seq.tkip.iv16 >> 8;
3644 			rx_pn[2 + hole] = seq.tkip.iv32;
3645 			rx_pn[3 + hole] = seq.tkip.iv32 >> 8;
3646 			rx_pn[4 + hole] = seq.tkip.iv32 >> 16;
3647 			rx_pn[5 + hole] = seq.tkip.iv32 >> 24;
3648 		} else if (key_flags & cpu_to_le16(STA_KEY_FLG_EXT)) {
3649 			rx_pn = seq.hw.seq;
3650 			rx_pn_len = seq.hw.seq_len;
3651 		} else {
3652 			rx_pn[0] = seq.ccmp.pn[0];
3653 			rx_pn[1] = seq.ccmp.pn[1];
3654 			rx_pn[2 + hole] = seq.ccmp.pn[2];
3655 			rx_pn[3 + hole] = seq.ccmp.pn[3];
3656 			rx_pn[4 + hole] = seq.ccmp.pn[4];
3657 			rx_pn[5 + hole] = seq.ccmp.pn[5];
3658 		}
3659 
3660 		if (iwl_mvm_pn_cmp(rx_pn, (u8 *)&u.cmd.common.rx_secur_seq_cnt,
3661 				   rx_pn_len) > 0)
3662 			memcpy(&u.cmd.common.rx_secur_seq_cnt, rx_pn,
3663 			       rx_pn_len);
3664 	}
3665 
3666 	if (api_ver >= 2) {
3667 		u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
3668 		size = sizeof(u.cmd);
3669 	} else {
3670 		size = sizeof(u.cmd_v1);
3671 	}
3672 
3673 	status = ADD_STA_SUCCESS;
3674 	if (cmd_flags & CMD_ASYNC)
3675 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
3676 					   &u.cmd);
3677 	else
3678 		ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
3679 						  &u.cmd, &status);
3680 
3681 	switch (status) {
3682 	case ADD_STA_SUCCESS:
3683 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3684 		break;
3685 	default:
3686 		ret = -EIO;
3687 		IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3688 		break;
3689 	}
3690 
3691 	return ret;
3692 }
3693 
3694 static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3695 				 struct ieee80211_key_conf *keyconf,
3696 				 u8 sta_id, bool remove_key)
3697 {
3698 	struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3699 
3700 	/* verify the key details match the required command's expectations */
3701 	if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3702 		    (keyconf->keyidx != 4 && keyconf->keyidx != 5 &&
3703 		     keyconf->keyidx != 6 && keyconf->keyidx != 7) ||
3704 		    (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
3705 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
3706 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
3707 		return -EINVAL;
3708 
3709 	if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
3710 		    keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3711 		return -EINVAL;
3712 
3713 	igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3714 	igtk_cmd.sta_id = cpu_to_le32(sta_id);
3715 
3716 	if (remove_key) {
3717 		/* This is a valid situation for IGTK */
3718 		if (sta_id == IWL_MVM_INVALID_STA)
3719 			return 0;
3720 
3721 		igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3722 	} else {
3723 		struct ieee80211_key_seq seq;
3724 		const u8 *pn;
3725 
3726 		switch (keyconf->cipher) {
3727 		case WLAN_CIPHER_SUITE_AES_CMAC:
3728 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3729 			break;
3730 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3731 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3732 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
3733 			break;
3734 		default:
3735 			return -EINVAL;
3736 		}
3737 
3738 		memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
3739 		if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3740 			igtk_cmd.ctrl_flags |=
3741 				cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3742 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3743 		pn = seq.aes_cmac.pn;
3744 		igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3745 						       ((u64) pn[4] << 8) |
3746 						       ((u64) pn[3] << 16) |
3747 						       ((u64) pn[2] << 24) |
3748 						       ((u64) pn[1] << 32) |
3749 						       ((u64) pn[0] << 40));
3750 	}
3751 
3752 	IWL_DEBUG_INFO(mvm, "%s %sIGTK (%d) for sta %u\n",
3753 		       remove_key ? "removing" : "installing",
3754 		       keyconf->keyidx >= 6 ? "B" : "",
3755 		       keyconf->keyidx, igtk_cmd.sta_id);
3756 
3757 	if (!iwl_mvm_has_new_rx_api(mvm)) {
3758 		struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
3759 			.ctrl_flags = igtk_cmd.ctrl_flags,
3760 			.key_id = igtk_cmd.key_id,
3761 			.sta_id = igtk_cmd.sta_id,
3762 			.receive_seq_cnt = igtk_cmd.receive_seq_cnt
3763 		};
3764 
3765 		memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
3766 		       ARRAY_SIZE(igtk_cmd_v1.igtk));
3767 		return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3768 					    sizeof(igtk_cmd_v1), &igtk_cmd_v1);
3769 	}
3770 	return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3771 				    sizeof(igtk_cmd), &igtk_cmd);
3772 }
3773 
3774 
3775 static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3776 				       struct ieee80211_vif *vif,
3777 				       struct ieee80211_sta *sta)
3778 {
3779 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3780 
3781 	if (sta)
3782 		return sta->addr;
3783 
3784 	if (vif->type == NL80211_IFTYPE_STATION &&
3785 	    mvmvif->deflink.ap_sta_id != IWL_MVM_INVALID_STA) {
3786 		u8 sta_id = mvmvif->deflink.ap_sta_id;
3787 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3788 						lockdep_is_held(&mvm->mutex));
3789 		if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
3790 			return NULL;
3791 
3792 		return sta->addr;
3793 	}
3794 
3795 
3796 	return NULL;
3797 }
3798 
3799 static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3800 				 struct ieee80211_vif *vif,
3801 				 struct ieee80211_sta *sta,
3802 				 struct ieee80211_key_conf *keyconf,
3803 				 u8 key_offset,
3804 				 bool mcast)
3805 {
3806 	const u8 *addr;
3807 	struct ieee80211_key_seq seq;
3808 	u16 p1k[5];
3809 	u32 sta_id;
3810 	bool mfp = false;
3811 
3812 	if (sta) {
3813 		struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3814 
3815 		sta_id = mvm_sta->deflink.sta_id;
3816 		mfp = sta->mfp;
3817 	} else if (vif->type == NL80211_IFTYPE_AP &&
3818 		   !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
3819 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3820 
3821 		sta_id = mvmvif->deflink.mcast_sta.sta_id;
3822 	} else {
3823 		IWL_ERR(mvm, "Failed to find station id\n");
3824 		return -EINVAL;
3825 	}
3826 
3827 	if (keyconf->cipher == WLAN_CIPHER_SUITE_TKIP) {
3828 		addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3829 		if (!addr) {
3830 			IWL_ERR(mvm, "Failed to find mac address\n");
3831 			return -EINVAL;
3832 		}
3833 
3834 		/* get phase 1 key from mac80211 */
3835 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3836 		ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
3837 
3838 		return iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3839 					    seq.tkip.iv32, p1k, 0, key_offset,
3840 					    mfp);
3841 	}
3842 
3843 	return iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3844 				    0, NULL, 0, key_offset, mfp);
3845 }
3846 
3847 int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3848 			struct ieee80211_vif *vif,
3849 			struct ieee80211_sta *sta,
3850 			struct ieee80211_key_conf *keyconf,
3851 			u8 key_offset)
3852 {
3853 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3854 	struct iwl_mvm_sta *mvm_sta;
3855 	u8 sta_id = IWL_MVM_INVALID_STA;
3856 	int ret;
3857 	static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3858 
3859 	lockdep_assert_held(&mvm->mutex);
3860 
3861 	if (vif->type != NL80211_IFTYPE_AP ||
3862 	    keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3863 		/* Get the station id from the mvm local station table */
3864 		mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3865 		if (!mvm_sta) {
3866 			IWL_ERR(mvm, "Failed to find station\n");
3867 			return -EINVAL;
3868 		}
3869 		sta_id = mvm_sta->deflink.sta_id;
3870 
3871 		/*
3872 		 * It is possible that the 'sta' parameter is NULL, and thus
3873 		 * there is a need to retrieve the sta from the local station
3874 		 * table.
3875 		 */
3876 		if (!sta) {
3877 			sta = rcu_dereference_protected(
3878 				mvm->fw_id_to_mac_id[sta_id],
3879 				lockdep_is_held(&mvm->mutex));
3880 			if (IS_ERR_OR_NULL(sta)) {
3881 				IWL_ERR(mvm, "Invalid station id\n");
3882 				return -EINVAL;
3883 			}
3884 		}
3885 
3886 		if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3887 			return -EINVAL;
3888 	} else {
3889 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3890 
3891 		sta_id = mvmvif->deflink.mcast_sta.sta_id;
3892 	}
3893 
3894 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3895 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3896 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3897 		ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3898 		goto end;
3899 	}
3900 
3901 	/* If the key_offset is not pre-assigned, we need to find a
3902 	 * new offset to use.  In normal cases, the offset is not
3903 	 * pre-assigned, but during HW_RESTART we want to reuse the
3904 	 * same indices, so we pass them when this function is called.
3905 	 *
3906 	 * In D3 entry, we need to hardcoded the indices (because the
3907 	 * firmware hardcodes the PTK offset to 0).  In this case, we
3908 	 * need to make sure we don't overwrite the hw_key_idx in the
3909 	 * keyconf structure, because otherwise we cannot configure
3910 	 * the original ones back when resuming.
3911 	 */
3912 	if (key_offset == STA_KEY_IDX_INVALID) {
3913 		key_offset  = iwl_mvm_set_fw_key_idx(mvm);
3914 		if (key_offset == STA_KEY_IDX_INVALID)
3915 			return -ENOSPC;
3916 		keyconf->hw_key_idx = key_offset;
3917 	}
3918 
3919 	ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
3920 	if (ret)
3921 		goto end;
3922 
3923 	/*
3924 	 * For WEP, the same key is used for multicast and unicast. Upload it
3925 	 * again, using the same key offset, and now pointing the other one
3926 	 * to the same key slot (offset).
3927 	 * If this fails, remove the original as well.
3928 	 */
3929 	if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3930 	     keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
3931 	    sta) {
3932 		ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
3933 					    key_offset, !mcast);
3934 		if (ret) {
3935 			__iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3936 			goto end;
3937 		}
3938 	}
3939 
3940 	__set_bit(key_offset, mvm->fw_key_table);
3941 
3942 end:
3943 	IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3944 		      keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3945 		      sta ? sta->addr : zero_addr, ret);
3946 	return ret;
3947 }
3948 
3949 int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3950 			   struct ieee80211_vif *vif,
3951 			   struct ieee80211_sta *sta,
3952 			   struct ieee80211_key_conf *keyconf)
3953 {
3954 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3955 	struct iwl_mvm_sta *mvm_sta;
3956 	u8 sta_id = IWL_MVM_INVALID_STA;
3957 	int ret, i;
3958 
3959 	lockdep_assert_held(&mvm->mutex);
3960 
3961 	/* Get the station from the mvm local station table */
3962 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3963 	if (mvm_sta)
3964 		sta_id = mvm_sta->deflink.sta_id;
3965 	else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
3966 		sta_id = iwl_mvm_vif_from_mac80211(vif)->deflink.mcast_sta.sta_id;
3967 
3968 
3969 	IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3970 		      keyconf->keyidx, sta_id);
3971 
3972 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3973 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3974 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3975 		return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3976 
3977 	if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3978 		IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3979 			keyconf->hw_key_idx);
3980 		return -ENOENT;
3981 	}
3982 
3983 	/* track which key was deleted last */
3984 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3985 		if (mvm->fw_key_deleted[i] < U8_MAX)
3986 			mvm->fw_key_deleted[i]++;
3987 	}
3988 	mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3989 
3990 	if (sta && !mvm_sta) {
3991 		IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3992 		return 0;
3993 	}
3994 
3995 	ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3996 	if (ret)
3997 		return ret;
3998 
3999 	/* delete WEP key twice to get rid of (now useless) offset */
4000 	if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
4001 	    keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
4002 		ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
4003 
4004 	return ret;
4005 }
4006 
4007 void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
4008 			     struct ieee80211_vif *vif,
4009 			     struct ieee80211_key_conf *keyconf,
4010 			     struct ieee80211_sta *sta, u32 iv32,
4011 			     u16 *phase1key)
4012 {
4013 	struct iwl_mvm_sta *mvm_sta;
4014 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
4015 	bool mfp = sta ? sta->mfp : false;
4016 
4017 	rcu_read_lock();
4018 
4019 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
4020 	if (WARN_ON_ONCE(!mvm_sta))
4021 		goto unlock;
4022 	iwl_mvm_send_sta_key(mvm, mvm_sta->deflink.sta_id, keyconf, mcast,
4023 			     iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx,
4024 			     mfp);
4025 
4026  unlock:
4027 	rcu_read_unlock();
4028 }
4029 
4030 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
4031 				struct ieee80211_sta *sta)
4032 {
4033 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
4034 	struct iwl_mvm_add_sta_cmd cmd = {
4035 		.add_modify = STA_MODE_MODIFY,
4036 		.sta_id = mvmsta->deflink.sta_id,
4037 		.station_flags_msk = cpu_to_le32(STA_FLG_PS),
4038 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
4039 	};
4040 	int ret;
4041 
4042 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
4043 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
4044 	if (ret)
4045 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
4046 }
4047 
4048 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
4049 				       struct ieee80211_sta *sta,
4050 				       enum ieee80211_frame_release_type reason,
4051 				       u16 cnt, u16 tids, bool more_data,
4052 				       bool single_sta_queue)
4053 {
4054 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
4055 	struct iwl_mvm_add_sta_cmd cmd = {
4056 		.add_modify = STA_MODE_MODIFY,
4057 		.sta_id = mvmsta->deflink.sta_id,
4058 		.modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
4059 		.sleep_tx_count = cpu_to_le16(cnt),
4060 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
4061 	};
4062 	int tid, ret;
4063 	unsigned long _tids = tids;
4064 
4065 	/* convert TIDs to ACs - we don't support TSPEC so that's OK
4066 	 * Note that this field is reserved and unused by firmware not
4067 	 * supporting GO uAPSD, so it's safe to always do this.
4068 	 */
4069 	for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
4070 		cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
4071 
4072 	/* If we're releasing frames from aggregation or dqa queues then check
4073 	 * if all the queues that we're releasing frames from, combined, have:
4074 	 *  - more frames than the service period, in which case more_data
4075 	 *    needs to be set
4076 	 *  - fewer than 'cnt' frames, in which case we need to adjust the
4077 	 *    firmware command (but do that unconditionally)
4078 	 */
4079 	if (single_sta_queue) {
4080 		int remaining = cnt;
4081 		int sleep_tx_count;
4082 
4083 		spin_lock_bh(&mvmsta->lock);
4084 		for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
4085 			struct iwl_mvm_tid_data *tid_data;
4086 			u16 n_queued;
4087 
4088 			tid_data = &mvmsta->tid_data[tid];
4089 
4090 			n_queued = iwl_mvm_tid_queued(mvm, tid_data);
4091 			if (n_queued > remaining) {
4092 				more_data = true;
4093 				remaining = 0;
4094 				break;
4095 			}
4096 			remaining -= n_queued;
4097 		}
4098 		sleep_tx_count = cnt - remaining;
4099 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
4100 			mvmsta->sleep_tx_count = sleep_tx_count;
4101 		spin_unlock_bh(&mvmsta->lock);
4102 
4103 		cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
4104 		if (WARN_ON(cnt - remaining == 0)) {
4105 			ieee80211_sta_eosp(sta);
4106 			return;
4107 		}
4108 	}
4109 
4110 	/* Note: this is ignored by firmware not supporting GO uAPSD */
4111 	if (more_data)
4112 		cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
4113 
4114 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
4115 		mvmsta->next_status_eosp = true;
4116 		cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
4117 	} else {
4118 		cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
4119 	}
4120 
4121 	/* block the Tx queues until the FW updated the sleep Tx count */
4122 	iwl_trans_block_txq_ptrs(mvm->trans, true);
4123 
4124 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
4125 				   CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
4126 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
4127 	if (ret)
4128 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
4129 }
4130 
4131 void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
4132 			   struct iwl_rx_cmd_buffer *rxb)
4133 {
4134 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
4135 	struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
4136 	struct ieee80211_sta *sta;
4137 	u32 sta_id = le32_to_cpu(notif->sta_id);
4138 
4139 	if (WARN_ON_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations))
4140 		return;
4141 
4142 	rcu_read_lock();
4143 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
4144 	if (!IS_ERR_OR_NULL(sta))
4145 		ieee80211_sta_eosp(sta);
4146 	rcu_read_unlock();
4147 }
4148 
4149 void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
4150 				   struct iwl_mvm_sta *mvmsta,
4151 				   bool disable)
4152 {
4153 	struct iwl_mvm_add_sta_cmd cmd = {
4154 		.add_modify = STA_MODE_MODIFY,
4155 		.sta_id = mvmsta->deflink.sta_id,
4156 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
4157 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
4158 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
4159 	};
4160 	int ret;
4161 
4162 	if (mvm->mld_api_is_used) {
4163 		iwl_mvm_mld_sta_modify_disable_tx(mvm, mvmsta, disable);
4164 		return;
4165 	}
4166 
4167 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
4168 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
4169 	if (ret)
4170 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
4171 }
4172 
4173 void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
4174 				      struct ieee80211_sta *sta,
4175 				      bool disable)
4176 {
4177 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4178 
4179 	if (mvm->mld_api_is_used) {
4180 		iwl_mvm_mld_sta_modify_disable_tx_ap(mvm, sta, disable);
4181 		return;
4182 	}
4183 
4184 	spin_lock_bh(&mvm_sta->lock);
4185 
4186 	if (mvm_sta->disable_tx == disable) {
4187 		spin_unlock_bh(&mvm_sta->lock);
4188 		return;
4189 	}
4190 
4191 	mvm_sta->disable_tx = disable;
4192 
4193 	/*
4194 	 * If sta PS state is handled by mac80211, tell it to start/stop
4195 	 * queuing tx for this station.
4196 	 */
4197 	if (!ieee80211_hw_check(mvm->hw, AP_LINK_PS))
4198 		ieee80211_sta_block_awake(mvm->hw, sta, disable);
4199 
4200 	iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
4201 
4202 	spin_unlock_bh(&mvm_sta->lock);
4203 }
4204 
4205 static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
4206 					      struct iwl_mvm_vif *mvmvif,
4207 					      struct iwl_mvm_int_sta *sta,
4208 					      bool disable)
4209 {
4210 	u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
4211 	struct iwl_mvm_add_sta_cmd cmd = {
4212 		.add_modify = STA_MODE_MODIFY,
4213 		.sta_id = sta->sta_id,
4214 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
4215 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
4216 		.mac_id_n_color = cpu_to_le32(id),
4217 	};
4218 	int ret;
4219 
4220 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
4221 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
4222 	if (ret)
4223 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
4224 }
4225 
4226 void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
4227 				       struct iwl_mvm_vif *mvmvif,
4228 				       bool disable)
4229 {
4230 	struct ieee80211_sta *sta;
4231 	struct iwl_mvm_sta *mvm_sta;
4232 	int i;
4233 
4234 	if (mvm->mld_api_is_used) {
4235 		iwl_mvm_mld_modify_all_sta_disable_tx(mvm, mvmvif, 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_MVM_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_MVM_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->trans_cfg->gen2)
4300 		sn &= 0xff;
4301 
4302 	return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
4303 }
4304 
4305 int iwl_mvm_add_pasn_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
4306 			 struct iwl_mvm_int_sta *sta, u8 *addr, u32 cipher,
4307 			 u8 *key, u32 key_len)
4308 {
4309 	int ret;
4310 	u16 queue;
4311 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4312 	struct ieee80211_key_conf *keyconf;
4313 	unsigned int wdg_timeout =
4314 		iwl_mvm_get_wd_timeout(mvm, vif, false, false);
4315 	bool mld = iwl_mvm_has_mld_api(mvm->fw);
4316 	u32 type = mld ? STATION_TYPE_PEER : IWL_STA_LINK;
4317 
4318 	ret = iwl_mvm_allocate_int_sta(mvm, sta, 0,
4319 				       NL80211_IFTYPE_UNSPECIFIED, type);
4320 	if (ret)
4321 		return ret;
4322 
4323 	if (mld)
4324 		ret = iwl_mvm_mld_add_int_sta_with_queue(mvm, sta, addr,
4325 							 mvmvif->deflink.fw_link_id,
4326 							 &queue,
4327 							 IWL_MAX_TID_COUNT,
4328 							 &wdg_timeout);
4329 	else
4330 		ret = iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id,
4331 						     mvmvif->color, addr, sta,
4332 						     &queue,
4333 						     IWL_MVM_TX_FIFO_BE);
4334 	if (ret)
4335 		goto out;
4336 
4337 	keyconf = kzalloc(sizeof(*keyconf) + key_len, GFP_KERNEL);
4338 	if (!keyconf) {
4339 		ret = -ENOBUFS;
4340 		goto out;
4341 	}
4342 
4343 	keyconf->cipher = cipher;
4344 	memcpy(keyconf->key, key, key_len);
4345 	keyconf->keylen = key_len;
4346 	keyconf->flags = IEEE80211_KEY_FLAG_PAIRWISE;
4347 
4348 	if (mld) {
4349 		/* The MFP flag is set according to the station mfp field. Since
4350 		 * we don't have a station, set it manually.
4351 		 */
4352 		u32 key_flags =
4353 			iwl_mvm_get_sec_flags(mvm, vif, NULL, keyconf) |
4354 			IWL_SEC_KEY_FLAG_MFP;
4355 		u32 sta_mask = BIT(sta->sta_id);
4356 
4357 		ret = iwl_mvm_mld_send_key(mvm, sta_mask, key_flags, keyconf);
4358 	} else {
4359 		ret = iwl_mvm_send_sta_key(mvm, sta->sta_id, keyconf, false,
4360 					   0, NULL, 0, 0, true);
4361 	}
4362 
4363 	kfree(keyconf);
4364 	return 0;
4365 out:
4366 	iwl_mvm_dealloc_int_sta(mvm, sta);
4367 	return ret;
4368 }
4369 
4370 void iwl_mvm_cancel_channel_switch(struct iwl_mvm *mvm,
4371 				   struct ieee80211_vif *vif,
4372 				   u32 id)
4373 {
4374 	struct iwl_cancel_channel_switch_cmd cancel_channel_switch_cmd = {
4375 		.id = cpu_to_le32(id),
4376 	};
4377 	int ret;
4378 
4379 	ret = iwl_mvm_send_cmd_pdu(mvm,
4380 				   WIDE_ID(MAC_CONF_GROUP, CANCEL_CHANNEL_SWITCH_CMD),
4381 				   CMD_ASYNC,
4382 				   sizeof(cancel_channel_switch_cmd),
4383 				   &cancel_channel_switch_cmd);
4384 	if (ret)
4385 		IWL_ERR(mvm, "Failed to cancel the channel switch\n");
4386 }
4387