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