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