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