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