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