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