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