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