xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/sta.c (revision 48dea9a700c8728cc31a1dd44588b97578de86ee)
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 	rcu_read_unlock();
1193 
1194 	if (free_queue >= 0 && alloc_for_sta != IWL_MVM_INVALID_STA) {
1195 		ret = iwl_mvm_free_inactive_queue(mvm, free_queue, queue_owner,
1196 						  alloc_for_sta);
1197 		if (ret)
1198 			return ret;
1199 	}
1200 
1201 	return free_queue;
1202 }
1203 
1204 static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
1205 				   struct ieee80211_sta *sta, u8 ac, int tid)
1206 {
1207 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1208 	struct iwl_trans_txq_scd_cfg cfg = {
1209 		.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
1210 		.sta_id = mvmsta->sta_id,
1211 		.tid = tid,
1212 		.frame_limit = IWL_FRAME_LIMIT,
1213 	};
1214 	unsigned int wdg_timeout =
1215 		iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1216 	int queue = -1;
1217 	unsigned long disable_agg_tids = 0;
1218 	enum iwl_mvm_agg_state queue_state;
1219 	bool shared_queue = false, inc_ssn;
1220 	int ssn;
1221 	unsigned long tfd_queue_mask;
1222 	int ret;
1223 
1224 	lockdep_assert_held(&mvm->mutex);
1225 
1226 	if (iwl_mvm_has_new_tx_api(mvm))
1227 		return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
1228 
1229 	spin_lock_bh(&mvmsta->lock);
1230 	tfd_queue_mask = mvmsta->tfd_queue_msk;
1231 	ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1232 	spin_unlock_bh(&mvmsta->lock);
1233 
1234 	if (tid == IWL_MAX_TID_COUNT) {
1235 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1236 						IWL_MVM_DQA_MIN_MGMT_QUEUE,
1237 						IWL_MVM_DQA_MAX_MGMT_QUEUE);
1238 		if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
1239 			IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
1240 					    queue);
1241 
1242 		/* If no such queue is found, we'll use a DATA queue instead */
1243 	}
1244 
1245 	if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
1246 	    (mvm->queue_info[mvmsta->reserved_queue].status ==
1247 			IWL_MVM_QUEUE_RESERVED)) {
1248 		queue = mvmsta->reserved_queue;
1249 		mvm->queue_info[queue].reserved = true;
1250 		IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
1251 	}
1252 
1253 	if (queue < 0)
1254 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1255 						IWL_MVM_DQA_MIN_DATA_QUEUE,
1256 						IWL_MVM_DQA_MAX_DATA_QUEUE);
1257 	if (queue < 0) {
1258 		/* try harder - perhaps kill an inactive queue */
1259 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1260 	}
1261 
1262 	/* No free queue - we'll have to share */
1263 	if (queue <= 0) {
1264 		queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
1265 		if (queue > 0) {
1266 			shared_queue = true;
1267 			mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
1268 		}
1269 	}
1270 
1271 	/*
1272 	 * Mark TXQ as ready, even though it hasn't been fully configured yet,
1273 	 * to make sure no one else takes it.
1274 	 * This will allow avoiding re-acquiring the lock at the end of the
1275 	 * configuration. On error we'll mark it back as free.
1276 	 */
1277 	if (queue > 0 && !shared_queue)
1278 		mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1279 
1280 	/* This shouldn't happen - out of queues */
1281 	if (WARN_ON(queue <= 0)) {
1282 		IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
1283 			tid, cfg.sta_id);
1284 		return queue;
1285 	}
1286 
1287 	/*
1288 	 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
1289 	 * but for configuring the SCD to send A-MPDUs we need to mark the queue
1290 	 * as aggregatable.
1291 	 * Mark all DATA queues as allowing to be aggregated at some point
1292 	 */
1293 	cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1294 			 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1295 
1296 	IWL_DEBUG_TX_QUEUES(mvm,
1297 			    "Allocating %squeue #%d to sta %d on tid %d\n",
1298 			    shared_queue ? "shared " : "", queue,
1299 			    mvmsta->sta_id, tid);
1300 
1301 	if (shared_queue) {
1302 		/* Disable any open aggs on this queue */
1303 		disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
1304 
1305 		if (disable_agg_tids) {
1306 			IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
1307 					    queue);
1308 			iwl_mvm_invalidate_sta_queue(mvm, queue,
1309 						     disable_agg_tids, false);
1310 		}
1311 	}
1312 
1313 	inc_ssn = iwl_mvm_enable_txq(mvm, sta, queue, ssn, &cfg, wdg_timeout);
1314 
1315 	/*
1316 	 * Mark queue as shared in transport if shared
1317 	 * Note this has to be done after queue enablement because enablement
1318 	 * can also set this value, and there is no indication there to shared
1319 	 * queues
1320 	 */
1321 	if (shared_queue)
1322 		iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
1323 
1324 	spin_lock_bh(&mvmsta->lock);
1325 	/*
1326 	 * This looks racy, but it is not. We have only one packet for
1327 	 * this ra/tid in our Tx path since we stop the Qdisc when we
1328 	 * need to allocate a new TFD queue.
1329 	 */
1330 	if (inc_ssn) {
1331 		mvmsta->tid_data[tid].seq_number += 0x10;
1332 		ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
1333 	}
1334 	mvmsta->tid_data[tid].txq_id = queue;
1335 	mvmsta->tfd_queue_msk |= BIT(queue);
1336 	queue_state = mvmsta->tid_data[tid].state;
1337 
1338 	if (mvmsta->reserved_queue == queue)
1339 		mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
1340 	spin_unlock_bh(&mvmsta->lock);
1341 
1342 	if (!shared_queue) {
1343 		ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
1344 		if (ret)
1345 			goto out_err;
1346 
1347 		/* If we need to re-enable aggregations... */
1348 		if (queue_state == IWL_AGG_ON) {
1349 			ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1350 			if (ret)
1351 				goto out_err;
1352 		}
1353 	} else {
1354 		/* Redirect queue, if needed */
1355 		ret = iwl_mvm_redirect_queue(mvm, queue, tid, ac, ssn,
1356 					     wdg_timeout, false,
1357 					     iwl_mvm_txq_from_tid(sta, tid));
1358 		if (ret)
1359 			goto out_err;
1360 	}
1361 
1362 	return 0;
1363 
1364 out_err:
1365 	iwl_mvm_disable_txq(mvm, sta, queue, tid, 0);
1366 
1367 	return ret;
1368 }
1369 
1370 void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1371 {
1372 	struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1373 					   add_stream_wk);
1374 
1375 	mutex_lock(&mvm->mutex);
1376 
1377 	iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
1378 
1379 	while (!list_empty(&mvm->add_stream_txqs)) {
1380 		struct iwl_mvm_txq *mvmtxq;
1381 		struct ieee80211_txq *txq;
1382 		u8 tid;
1383 
1384 		mvmtxq = list_first_entry(&mvm->add_stream_txqs,
1385 					  struct iwl_mvm_txq, list);
1386 
1387 		txq = container_of((void *)mvmtxq, struct ieee80211_txq,
1388 				   drv_priv);
1389 		tid = txq->tid;
1390 		if (tid == IEEE80211_NUM_TIDS)
1391 			tid = IWL_MAX_TID_COUNT;
1392 
1393 		/*
1394 		 * We can't really do much here, but if this fails we can't
1395 		 * transmit anyway - so just don't transmit the frame etc.
1396 		 * and let them back up ... we've tried our best to allocate
1397 		 * a queue in the function itself.
1398 		 */
1399 		if (iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, tid)) {
1400 			list_del_init(&mvmtxq->list);
1401 			continue;
1402 		}
1403 
1404 		list_del_init(&mvmtxq->list);
1405 		local_bh_disable();
1406 		iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1407 		local_bh_enable();
1408 	}
1409 
1410 	mutex_unlock(&mvm->mutex);
1411 }
1412 
1413 static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1414 				      struct ieee80211_sta *sta,
1415 				      enum nl80211_iftype vif_type)
1416 {
1417 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1418 	int queue;
1419 
1420 	/* queue reserving is disabled on new TX path */
1421 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1422 		return 0;
1423 
1424 	/* run the general cleanup/unsharing of queues */
1425 	iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
1426 
1427 	/* Make sure we have free resources for this STA */
1428 	if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1429 	    !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].tid_bitmap &&
1430 	    (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1431 	     IWL_MVM_QUEUE_FREE))
1432 		queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1433 	else
1434 		queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1435 						IWL_MVM_DQA_MIN_DATA_QUEUE,
1436 						IWL_MVM_DQA_MAX_DATA_QUEUE);
1437 	if (queue < 0) {
1438 		/* try again - this time kick out a queue if needed */
1439 		queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1440 		if (queue < 0) {
1441 			IWL_ERR(mvm, "No available queues for new station\n");
1442 			return -ENOSPC;
1443 		}
1444 	}
1445 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
1446 
1447 	mvmsta->reserved_queue = queue;
1448 
1449 	IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1450 			    queue, mvmsta->sta_id);
1451 
1452 	return 0;
1453 }
1454 
1455 /*
1456  * In DQA mode, after a HW restart the queues should be allocated as before, in
1457  * order to avoid race conditions when there are shared queues. This function
1458  * does the re-mapping and queue allocation.
1459  *
1460  * Note that re-enabling aggregations isn't done in this function.
1461  */
1462 static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1463 						 struct ieee80211_sta *sta)
1464 {
1465 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1466 	unsigned int wdg =
1467 		iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
1468 	int i;
1469 	struct iwl_trans_txq_scd_cfg cfg = {
1470 		.sta_id = mvm_sta->sta_id,
1471 		.frame_limit = IWL_FRAME_LIMIT,
1472 	};
1473 
1474 	/* Make sure reserved queue is still marked as such (if allocated) */
1475 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
1476 		mvm->queue_info[mvm_sta->reserved_queue].status =
1477 			IWL_MVM_QUEUE_RESERVED;
1478 
1479 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1480 		struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1481 		int txq_id = tid_data->txq_id;
1482 		int ac;
1483 
1484 		if (txq_id == IWL_MVM_INVALID_QUEUE)
1485 			continue;
1486 
1487 		ac = tid_to_mac80211_ac[i];
1488 
1489 		if (iwl_mvm_has_new_tx_api(mvm)) {
1490 			IWL_DEBUG_TX_QUEUES(mvm,
1491 					    "Re-mapping sta %d tid %d\n",
1492 					    mvm_sta->sta_id, i);
1493 			txq_id = iwl_mvm_tvqm_enable_txq(mvm, mvm_sta->sta_id,
1494 							 i, wdg);
1495 			/*
1496 			 * on failures, just set it to IWL_MVM_INVALID_QUEUE
1497 			 * to try again later, we have no other good way of
1498 			 * failing here
1499 			 */
1500 			if (txq_id < 0)
1501 				txq_id = IWL_MVM_INVALID_QUEUE;
1502 			tid_data->txq_id = txq_id;
1503 
1504 			/*
1505 			 * Since we don't set the seq number after reset, and HW
1506 			 * sets it now, FW reset will cause the seq num to start
1507 			 * at 0 again, so driver will need to update it
1508 			 * internally as well, so it keeps in sync with real val
1509 			 */
1510 			tid_data->seq_number = 0;
1511 		} else {
1512 			u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1513 
1514 			cfg.tid = i;
1515 			cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
1516 			cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1517 					 txq_id ==
1518 					 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1519 
1520 			IWL_DEBUG_TX_QUEUES(mvm,
1521 					    "Re-mapping sta %d tid %d to queue %d\n",
1522 					    mvm_sta->sta_id, i, txq_id);
1523 
1524 			iwl_mvm_enable_txq(mvm, sta, txq_id, seq, &cfg, wdg);
1525 			mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
1526 		}
1527 	}
1528 }
1529 
1530 static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1531 				      struct iwl_mvm_int_sta *sta,
1532 				      const u8 *addr,
1533 				      u16 mac_id, u16 color)
1534 {
1535 	struct iwl_mvm_add_sta_cmd cmd;
1536 	int ret;
1537 	u32 status = ADD_STA_SUCCESS;
1538 
1539 	lockdep_assert_held(&mvm->mutex);
1540 
1541 	memset(&cmd, 0, sizeof(cmd));
1542 	cmd.sta_id = sta->sta_id;
1543 	cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1544 							     color));
1545 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1546 		cmd.station_type = sta->type;
1547 
1548 	if (!iwl_mvm_has_new_tx_api(mvm))
1549 		cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1550 	cmd.tid_disable_tx = cpu_to_le16(0xffff);
1551 
1552 	if (addr)
1553 		memcpy(cmd.addr, addr, ETH_ALEN);
1554 
1555 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1556 					  iwl_mvm_add_sta_cmd_size(mvm),
1557 					  &cmd, &status);
1558 	if (ret)
1559 		return ret;
1560 
1561 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1562 	case ADD_STA_SUCCESS:
1563 		IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1564 		return 0;
1565 	default:
1566 		ret = -EIO;
1567 		IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1568 			status);
1569 		break;
1570 	}
1571 	return ret;
1572 }
1573 
1574 int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1575 		    struct ieee80211_vif *vif,
1576 		    struct ieee80211_sta *sta)
1577 {
1578 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1579 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1580 	struct iwl_mvm_rxq_dup_data *dup_data;
1581 	int i, ret, sta_id;
1582 	bool sta_update = false;
1583 	unsigned int sta_flags = 0;
1584 
1585 	lockdep_assert_held(&mvm->mutex);
1586 
1587 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1588 		sta_id = iwl_mvm_find_free_sta_id(mvm,
1589 						  ieee80211_vif_type_p2p(vif));
1590 	else
1591 		sta_id = mvm_sta->sta_id;
1592 
1593 	if (sta_id == IWL_MVM_INVALID_STA)
1594 		return -ENOSPC;
1595 
1596 	spin_lock_init(&mvm_sta->lock);
1597 
1598 	/* if this is a HW restart re-alloc existing queues */
1599 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1600 		struct iwl_mvm_int_sta tmp_sta = {
1601 			.sta_id = sta_id,
1602 			.type = mvm_sta->sta_type,
1603 		};
1604 
1605 		/*
1606 		 * First add an empty station since allocating
1607 		 * a queue requires a valid station
1608 		 */
1609 		ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1610 						 mvmvif->id, mvmvif->color);
1611 		if (ret)
1612 			goto err;
1613 
1614 		iwl_mvm_realloc_queues_after_restart(mvm, sta);
1615 		sta_update = true;
1616 		sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
1617 		goto update_fw;
1618 	}
1619 
1620 	mvm_sta->sta_id = sta_id;
1621 	mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1622 						      mvmvif->color);
1623 	mvm_sta->vif = vif;
1624 	if (!mvm->trans->trans_cfg->gen2)
1625 		mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1626 	else
1627 		mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
1628 	mvm_sta->tx_protection = 0;
1629 	mvm_sta->tt_tx_protection = false;
1630 	mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
1631 
1632 	/* HW restart, don't assume the memory has been zeroed */
1633 	mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1634 	mvm_sta->tfd_queue_msk = 0;
1635 
1636 	/* for HW restart - reset everything but the sequence number */
1637 	for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1638 		u16 seq = mvm_sta->tid_data[i].seq_number;
1639 		memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1640 		mvm_sta->tid_data[i].seq_number = seq;
1641 
1642 		/*
1643 		 * Mark all queues for this STA as unallocated and defer TX
1644 		 * frames until the queue is allocated
1645 		 */
1646 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1647 	}
1648 
1649 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1650 		struct iwl_mvm_txq *mvmtxq =
1651 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1652 
1653 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1654 		INIT_LIST_HEAD(&mvmtxq->list);
1655 		atomic_set(&mvmtxq->tx_request, 0);
1656 	}
1657 
1658 	mvm_sta->agg_tids = 0;
1659 
1660 	if (iwl_mvm_has_new_rx_api(mvm) &&
1661 	    !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1662 		int q;
1663 
1664 		dup_data = kcalloc(mvm->trans->num_rx_queues,
1665 				   sizeof(*dup_data), GFP_KERNEL);
1666 		if (!dup_data)
1667 			return -ENOMEM;
1668 		/*
1669 		 * Initialize all the last_seq values to 0xffff which can never
1670 		 * compare equal to the frame's seq_ctrl in the check in
1671 		 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
1672 		 * number and fragmented packets don't reach that function.
1673 		 *
1674 		 * This thus allows receiving a packet with seqno 0 and the
1675 		 * retry bit set as the very first packet on a new TID.
1676 		 */
1677 		for (q = 0; q < mvm->trans->num_rx_queues; q++)
1678 			memset(dup_data[q].last_seq, 0xff,
1679 			       sizeof(dup_data[q].last_seq));
1680 		mvm_sta->dup_data = dup_data;
1681 	}
1682 
1683 	if (!iwl_mvm_has_new_tx_api(mvm)) {
1684 		ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1685 						 ieee80211_vif_type_p2p(vif));
1686 		if (ret)
1687 			goto err;
1688 	}
1689 
1690 	/*
1691 	 * if rs is registered with mac80211, then "add station" will be handled
1692 	 * via the corresponding ops, otherwise need to notify rate scaling here
1693 	 */
1694 	if (iwl_mvm_has_tlc_offload(mvm))
1695 		iwl_mvm_rs_add_sta(mvm, mvm_sta);
1696 	else
1697 		spin_lock_init(&mvm_sta->lq_sta.rs_drv.pers.lock);
1698 
1699 	iwl_mvm_toggle_tx_ant(mvm, &mvm_sta->tx_ant);
1700 
1701 update_fw:
1702 	ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
1703 	if (ret)
1704 		goto err;
1705 
1706 	if (vif->type == NL80211_IFTYPE_STATION) {
1707 		if (!sta->tdls) {
1708 			WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
1709 			mvmvif->ap_sta_id = sta_id;
1710 		} else {
1711 			WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
1712 		}
1713 	}
1714 
1715 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1716 
1717 	return 0;
1718 
1719 err:
1720 	return ret;
1721 }
1722 
1723 int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1724 		      bool drain)
1725 {
1726 	struct iwl_mvm_add_sta_cmd cmd = {};
1727 	int ret;
1728 	u32 status;
1729 
1730 	lockdep_assert_held(&mvm->mutex);
1731 
1732 	cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1733 	cmd.sta_id = mvmsta->sta_id;
1734 	cmd.add_modify = STA_MODE_MODIFY;
1735 	cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1736 	cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1737 
1738 	status = ADD_STA_SUCCESS;
1739 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1740 					  iwl_mvm_add_sta_cmd_size(mvm),
1741 					  &cmd, &status);
1742 	if (ret)
1743 		return ret;
1744 
1745 	switch (status & IWL_ADD_STA_STATUS_MASK) {
1746 	case ADD_STA_SUCCESS:
1747 		IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1748 			       mvmsta->sta_id);
1749 		break;
1750 	default:
1751 		ret = -EIO;
1752 		IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1753 			mvmsta->sta_id);
1754 		break;
1755 	}
1756 
1757 	return ret;
1758 }
1759 
1760 /*
1761  * Remove a station from the FW table. Before sending the command to remove
1762  * the station validate that the station is indeed known to the driver (sanity
1763  * only).
1764  */
1765 static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1766 {
1767 	struct ieee80211_sta *sta;
1768 	struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1769 		.sta_id = sta_id,
1770 	};
1771 	int ret;
1772 
1773 	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1774 					lockdep_is_held(&mvm->mutex));
1775 
1776 	/* Note: internal stations are marked as error values */
1777 	if (!sta) {
1778 		IWL_ERR(mvm, "Invalid station id\n");
1779 		return -EINVAL;
1780 	}
1781 
1782 	ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1783 				   sizeof(rm_sta_cmd), &rm_sta_cmd);
1784 	if (ret) {
1785 		IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1786 		return ret;
1787 	}
1788 
1789 	return 0;
1790 }
1791 
1792 static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1793 				       struct ieee80211_vif *vif,
1794 				       struct ieee80211_sta *sta)
1795 {
1796 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1797 	int i;
1798 
1799 	lockdep_assert_held(&mvm->mutex);
1800 
1801 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1802 		if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
1803 			continue;
1804 
1805 		iwl_mvm_disable_txq(mvm, sta, mvm_sta->tid_data[i].txq_id, i,
1806 				    0);
1807 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1808 	}
1809 
1810 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1811 		struct iwl_mvm_txq *mvmtxq =
1812 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1813 
1814 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1815 	}
1816 }
1817 
1818 int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1819 				  struct iwl_mvm_sta *mvm_sta)
1820 {
1821 	int i;
1822 
1823 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1824 		u16 txq_id;
1825 		int ret;
1826 
1827 		spin_lock_bh(&mvm_sta->lock);
1828 		txq_id = mvm_sta->tid_data[i].txq_id;
1829 		spin_unlock_bh(&mvm_sta->lock);
1830 
1831 		if (txq_id == IWL_MVM_INVALID_QUEUE)
1832 			continue;
1833 
1834 		ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1835 		if (ret)
1836 			return ret;
1837 	}
1838 
1839 	return 0;
1840 }
1841 
1842 int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1843 		   struct ieee80211_vif *vif,
1844 		   struct ieee80211_sta *sta)
1845 {
1846 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1847 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1848 	u8 sta_id = mvm_sta->sta_id;
1849 	int ret;
1850 
1851 	lockdep_assert_held(&mvm->mutex);
1852 
1853 	if (iwl_mvm_has_new_rx_api(mvm))
1854 		kfree(mvm_sta->dup_data);
1855 
1856 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1857 	if (ret)
1858 		return ret;
1859 
1860 	/* flush its queues here since we are freeing mvm_sta */
1861 	ret = iwl_mvm_flush_sta(mvm, mvm_sta, false, 0);
1862 	if (ret)
1863 		return ret;
1864 	if (iwl_mvm_has_new_tx_api(mvm)) {
1865 		ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1866 	} else {
1867 		u32 q_mask = mvm_sta->tfd_queue_msk;
1868 
1869 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1870 						     q_mask);
1871 	}
1872 	if (ret)
1873 		return ret;
1874 
1875 	ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1876 
1877 	iwl_mvm_disable_sta_queues(mvm, vif, sta);
1878 
1879 	/* If there is a TXQ still marked as reserved - free it */
1880 	if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1881 		u8 reserved_txq = mvm_sta->reserved_queue;
1882 		enum iwl_mvm_queue_status *status;
1883 
1884 		/*
1885 		 * If no traffic has gone through the reserved TXQ - it
1886 		 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1887 		 * should be manually marked as free again
1888 		 */
1889 		status = &mvm->queue_info[reserved_txq].status;
1890 		if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1891 			 (*status != IWL_MVM_QUEUE_FREE),
1892 			 "sta_id %d reserved txq %d status %d",
1893 			 sta_id, reserved_txq, *status))
1894 			return -EINVAL;
1895 
1896 		*status = IWL_MVM_QUEUE_FREE;
1897 	}
1898 
1899 	if (vif->type == NL80211_IFTYPE_STATION &&
1900 	    mvmvif->ap_sta_id == sta_id) {
1901 		/* if associated - we can't remove the AP STA now */
1902 		if (vif->bss_conf.assoc)
1903 			return ret;
1904 
1905 		/* unassoc - go ahead - remove the AP STA now */
1906 		mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1907 	}
1908 
1909 	/*
1910 	 * This shouldn't happen - the TDLS channel switch should be canceled
1911 	 * before the STA is removed.
1912 	 */
1913 	if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
1914 		mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1915 		cancel_delayed_work(&mvm->tdls_cs.dwork);
1916 	}
1917 
1918 	/*
1919 	 * Make sure that the tx response code sees the station as -EBUSY and
1920 	 * calls the drain worker.
1921 	 */
1922 	spin_lock_bh(&mvm_sta->lock);
1923 	spin_unlock_bh(&mvm_sta->lock);
1924 
1925 	ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1926 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1927 
1928 	return ret;
1929 }
1930 
1931 int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1932 		      struct ieee80211_vif *vif,
1933 		      u8 sta_id)
1934 {
1935 	int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1936 
1937 	lockdep_assert_held(&mvm->mutex);
1938 
1939 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1940 	return ret;
1941 }
1942 
1943 int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1944 			     struct iwl_mvm_int_sta *sta,
1945 			     u32 qmask, enum nl80211_iftype iftype,
1946 			     enum iwl_sta_type type)
1947 {
1948 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1949 	    sta->sta_id == IWL_MVM_INVALID_STA) {
1950 		sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
1951 		if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
1952 			return -ENOSPC;
1953 	}
1954 
1955 	sta->tfd_queue_msk = qmask;
1956 	sta->type = type;
1957 
1958 	/* put a non-NULL value so iterating over the stations won't stop */
1959 	rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1960 	return 0;
1961 }
1962 
1963 void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
1964 {
1965 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
1966 	memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
1967 	sta->sta_id = IWL_MVM_INVALID_STA;
1968 }
1969 
1970 static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 queue,
1971 					  u8 sta_id, u8 fifo)
1972 {
1973 	unsigned int wdg_timeout =
1974 		mvm->trans->trans_cfg->base_params->wd_timeout;
1975 	struct iwl_trans_txq_scd_cfg cfg = {
1976 		.fifo = fifo,
1977 		.sta_id = sta_id,
1978 		.tid = IWL_MAX_TID_COUNT,
1979 		.aggregate = false,
1980 		.frame_limit = IWL_FRAME_LIMIT,
1981 	};
1982 
1983 	WARN_ON(iwl_mvm_has_new_tx_api(mvm));
1984 
1985 	iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
1986 }
1987 
1988 static int iwl_mvm_enable_aux_snif_queue_tvqm(struct iwl_mvm *mvm, u8 sta_id)
1989 {
1990 	unsigned int wdg_timeout =
1991 		mvm->trans->trans_cfg->base_params->wd_timeout;
1992 
1993 	WARN_ON(!iwl_mvm_has_new_tx_api(mvm));
1994 
1995 	return iwl_mvm_tvqm_enable_txq(mvm, sta_id, IWL_MAX_TID_COUNT,
1996 				       wdg_timeout);
1997 }
1998 
1999 static int iwl_mvm_add_int_sta_with_queue(struct iwl_mvm *mvm, int macidx,
2000 					  int maccolor,
2001 					  struct iwl_mvm_int_sta *sta,
2002 					  u16 *queue, int fifo)
2003 {
2004 	int ret;
2005 
2006 	/* Map queue to fifo - needs to happen before adding station */
2007 	if (!iwl_mvm_has_new_tx_api(mvm))
2008 		iwl_mvm_enable_aux_snif_queue(mvm, *queue, sta->sta_id, fifo);
2009 
2010 	ret = iwl_mvm_add_int_sta_common(mvm, sta, NULL, macidx, maccolor);
2011 	if (ret) {
2012 		if (!iwl_mvm_has_new_tx_api(mvm))
2013 			iwl_mvm_disable_txq(mvm, NULL, *queue,
2014 					    IWL_MAX_TID_COUNT, 0);
2015 		return ret;
2016 	}
2017 
2018 	/*
2019 	 * For 22000 firmware and on we cannot add queue to a station unknown
2020 	 * to firmware so enable queue here - after the station was added
2021 	 */
2022 	if (iwl_mvm_has_new_tx_api(mvm)) {
2023 		int txq;
2024 
2025 		txq = iwl_mvm_enable_aux_snif_queue_tvqm(mvm, sta->sta_id);
2026 		if (txq < 0) {
2027 			iwl_mvm_rm_sta_common(mvm, sta->sta_id);
2028 			return txq;
2029 		}
2030 
2031 		*queue = txq;
2032 	}
2033 
2034 	return 0;
2035 }
2036 
2037 int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
2038 {
2039 	int ret;
2040 
2041 	lockdep_assert_held(&mvm->mutex);
2042 
2043 	/* Allocate aux station and assign to it the aux queue */
2044 	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
2045 				       NL80211_IFTYPE_UNSPECIFIED,
2046 				       IWL_STA_AUX_ACTIVITY);
2047 	if (ret)
2048 		return ret;
2049 
2050 	ret = iwl_mvm_add_int_sta_with_queue(mvm, MAC_INDEX_AUX, 0,
2051 					     &mvm->aux_sta, &mvm->aux_queue,
2052 					     IWL_MVM_TX_FIFO_MCAST);
2053 	if (ret) {
2054 		iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2055 		return ret;
2056 	}
2057 
2058 	return 0;
2059 }
2060 
2061 int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2062 {
2063 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2064 
2065 	lockdep_assert_held(&mvm->mutex);
2066 
2067 	return iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
2068 					      &mvm->snif_sta, &mvm->snif_queue,
2069 					      IWL_MVM_TX_FIFO_BE);
2070 }
2071 
2072 int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2073 {
2074 	int ret;
2075 
2076 	lockdep_assert_held(&mvm->mutex);
2077 
2078 	iwl_mvm_disable_txq(mvm, NULL, mvm->snif_queue, IWL_MAX_TID_COUNT, 0);
2079 	ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
2080 	if (ret)
2081 		IWL_WARN(mvm, "Failed sending remove station\n");
2082 
2083 	return ret;
2084 }
2085 
2086 int iwl_mvm_rm_aux_sta(struct iwl_mvm *mvm)
2087 {
2088 	int ret;
2089 
2090 	lockdep_assert_held(&mvm->mutex);
2091 
2092 	iwl_mvm_disable_txq(mvm, NULL, mvm->aux_queue, IWL_MAX_TID_COUNT, 0);
2093 	ret = iwl_mvm_rm_sta_common(mvm, mvm->aux_sta.sta_id);
2094 	if (ret)
2095 		IWL_WARN(mvm, "Failed sending remove station\n");
2096 	iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2097 
2098 	return ret;
2099 }
2100 
2101 void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
2102 {
2103 	iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
2104 }
2105 
2106 /*
2107  * Send the add station command for the vif's broadcast station.
2108  * Assumes that the station was already allocated.
2109  *
2110  * @mvm: the mvm component
2111  * @vif: the interface to which the broadcast station is added
2112  * @bsta: the broadcast station to add.
2113  */
2114 int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2115 {
2116 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2117 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2118 	static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2119 	const u8 *baddr = _baddr;
2120 	int queue;
2121 	int ret;
2122 	unsigned int wdg_timeout =
2123 		iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2124 	struct iwl_trans_txq_scd_cfg cfg = {
2125 		.fifo = IWL_MVM_TX_FIFO_VO,
2126 		.sta_id = mvmvif->bcast_sta.sta_id,
2127 		.tid = IWL_MAX_TID_COUNT,
2128 		.aggregate = false,
2129 		.frame_limit = IWL_FRAME_LIMIT,
2130 	};
2131 
2132 	lockdep_assert_held(&mvm->mutex);
2133 
2134 	if (!iwl_mvm_has_new_tx_api(mvm)) {
2135 		if (vif->type == NL80211_IFTYPE_AP ||
2136 		    vif->type == NL80211_IFTYPE_ADHOC) {
2137 			queue = mvm->probe_queue;
2138 		} else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2139 			queue = mvm->p2p_dev_queue;
2140 		} else {
2141 			WARN(1, "Missing required TXQ for adding bcast STA\n");
2142 			return -EINVAL;
2143 		}
2144 
2145 		bsta->tfd_queue_msk |= BIT(queue);
2146 
2147 		iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2148 	}
2149 
2150 	if (vif->type == NL80211_IFTYPE_ADHOC)
2151 		baddr = vif->bss_conf.bssid;
2152 
2153 	if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
2154 		return -ENOSPC;
2155 
2156 	ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2157 					 mvmvif->id, mvmvif->color);
2158 	if (ret)
2159 		return ret;
2160 
2161 	/*
2162 	 * For 22000 firmware and on we cannot add queue to a station unknown
2163 	 * to firmware so enable queue here - after the station was added
2164 	 */
2165 	if (iwl_mvm_has_new_tx_api(mvm)) {
2166 		queue = iwl_mvm_tvqm_enable_txq(mvm, bsta->sta_id,
2167 						IWL_MAX_TID_COUNT,
2168 						wdg_timeout);
2169 		if (queue < 0) {
2170 			iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
2171 			return queue;
2172 		}
2173 
2174 		if (vif->type == NL80211_IFTYPE_AP ||
2175 		    vif->type == NL80211_IFTYPE_ADHOC)
2176 			mvm->probe_queue = queue;
2177 		else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
2178 			mvm->p2p_dev_queue = queue;
2179 	}
2180 
2181 	return 0;
2182 }
2183 
2184 static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2185 					  struct ieee80211_vif *vif)
2186 {
2187 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2188 	int queue;
2189 
2190 	lockdep_assert_held(&mvm->mutex);
2191 
2192 	iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true, 0);
2193 
2194 	switch (vif->type) {
2195 	case NL80211_IFTYPE_AP:
2196 	case NL80211_IFTYPE_ADHOC:
2197 		queue = mvm->probe_queue;
2198 		break;
2199 	case NL80211_IFTYPE_P2P_DEVICE:
2200 		queue = mvm->p2p_dev_queue;
2201 		break;
2202 	default:
2203 		WARN(1, "Can't free bcast queue on vif type %d\n",
2204 		     vif->type);
2205 		return;
2206 	}
2207 
2208 	iwl_mvm_disable_txq(mvm, NULL, queue, IWL_MAX_TID_COUNT, 0);
2209 	if (iwl_mvm_has_new_tx_api(mvm))
2210 		return;
2211 
2212 	WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
2213 	mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
2214 }
2215 
2216 /* Send the FW a request to remove the station from it's internal data
2217  * structures, but DO NOT remove the entry from the local data structures. */
2218 int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2219 {
2220 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2221 	int ret;
2222 
2223 	lockdep_assert_held(&mvm->mutex);
2224 
2225 	iwl_mvm_free_bcast_sta_queues(mvm, vif);
2226 
2227 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
2228 	if (ret)
2229 		IWL_WARN(mvm, "Failed sending remove station\n");
2230 	return ret;
2231 }
2232 
2233 int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2234 {
2235 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2236 
2237 	lockdep_assert_held(&mvm->mutex);
2238 
2239 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, 0,
2240 					ieee80211_vif_type_p2p(vif),
2241 					IWL_STA_GENERAL_PURPOSE);
2242 }
2243 
2244 /* Allocate a new station entry for the broadcast station to the given vif,
2245  * and send it to the FW.
2246  * Note that each P2P mac should have its own broadcast station.
2247  *
2248  * @mvm: the mvm component
2249  * @vif: the interface to which the broadcast station is added
2250  * @bsta: the broadcast station to add. */
2251 int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2252 {
2253 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2254 	struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2255 	int ret;
2256 
2257 	lockdep_assert_held(&mvm->mutex);
2258 
2259 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2260 	if (ret)
2261 		return ret;
2262 
2263 	ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2264 
2265 	if (ret)
2266 		iwl_mvm_dealloc_int_sta(mvm, bsta);
2267 
2268 	return ret;
2269 }
2270 
2271 void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2272 {
2273 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2274 
2275 	iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2276 }
2277 
2278 /*
2279  * Send the FW a request to remove the station from it's internal data
2280  * structures, and in addition remove it from the local data structure.
2281  */
2282 int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2283 {
2284 	int ret;
2285 
2286 	lockdep_assert_held(&mvm->mutex);
2287 
2288 	ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2289 
2290 	iwl_mvm_dealloc_bcast_sta(mvm, vif);
2291 
2292 	return ret;
2293 }
2294 
2295 /*
2296  * Allocate a new station entry for the multicast station to the given vif,
2297  * and send it to the FW.
2298  * Note that each AP/GO mac should have its own multicast station.
2299  *
2300  * @mvm: the mvm component
2301  * @vif: the interface to which the multicast station is added
2302  */
2303 int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2304 {
2305 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2306 	struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
2307 	static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
2308 	const u8 *maddr = _maddr;
2309 	struct iwl_trans_txq_scd_cfg cfg = {
2310 		.fifo = vif->type == NL80211_IFTYPE_AP ?
2311 			IWL_MVM_TX_FIFO_MCAST : IWL_MVM_TX_FIFO_BE,
2312 		.sta_id = msta->sta_id,
2313 		.tid = 0,
2314 		.aggregate = false,
2315 		.frame_limit = IWL_FRAME_LIMIT,
2316 	};
2317 	unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2318 	int ret;
2319 
2320 	lockdep_assert_held(&mvm->mutex);
2321 
2322 	if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2323 		    vif->type != NL80211_IFTYPE_ADHOC))
2324 		return -ENOTSUPP;
2325 
2326 	/*
2327 	 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2328 	 * invalid, so make sure we use the queue we want.
2329 	 * Note that this is done here as we want to avoid making DQA
2330 	 * changes in mac80211 layer.
2331 	 */
2332 	if (vif->type == NL80211_IFTYPE_ADHOC)
2333 		mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2334 
2335 	/*
2336 	 * While in previous FWs we had to exclude cab queue from TFD queue
2337 	 * mask, now it is needed as any other queue.
2338 	 */
2339 	if (!iwl_mvm_has_new_tx_api(mvm) &&
2340 	    fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2341 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2342 				   timeout);
2343 		msta->tfd_queue_msk |= BIT(mvmvif->cab_queue);
2344 	}
2345 	ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
2346 					 mvmvif->id, mvmvif->color);
2347 	if (ret)
2348 		goto err;
2349 
2350 	/*
2351 	 * Enable cab queue after the ADD_STA command is sent.
2352 	 * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
2353 	 * command with unknown station id, and for FW that doesn't support
2354 	 * station API since the cab queue is not included in the
2355 	 * tfd_queue_mask.
2356 	 */
2357 	if (iwl_mvm_has_new_tx_api(mvm)) {
2358 		int queue = iwl_mvm_tvqm_enable_txq(mvm, msta->sta_id,
2359 						    0,
2360 						    timeout);
2361 		if (queue < 0) {
2362 			ret = queue;
2363 			goto err;
2364 		}
2365 		mvmvif->cab_queue = queue;
2366 	} else if (!fw_has_api(&mvm->fw->ucode_capa,
2367 			       IWL_UCODE_TLV_API_STA_TYPE))
2368 		iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2369 				   timeout);
2370 
2371 	return 0;
2372 err:
2373 	iwl_mvm_dealloc_int_sta(mvm, msta);
2374 	return ret;
2375 }
2376 
2377 static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
2378 				    struct ieee80211_key_conf *keyconf,
2379 				    bool mcast)
2380 {
2381 	union {
2382 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
2383 		struct iwl_mvm_add_sta_key_cmd cmd;
2384 	} u = {};
2385 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
2386 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
2387 	__le16 key_flags;
2388 	int ret, size;
2389 	u32 status;
2390 
2391 	/* This is a valid situation for GTK removal */
2392 	if (sta_id == IWL_MVM_INVALID_STA)
2393 		return 0;
2394 
2395 	key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2396 				 STA_KEY_FLG_KEYID_MSK);
2397 	key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2398 	key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2399 
2400 	if (mcast)
2401 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2402 
2403 	/*
2404 	 * The fields assigned here are in the same location at the start
2405 	 * of the command, so we can do this union trick.
2406 	 */
2407 	u.cmd.common.key_flags = key_flags;
2408 	u.cmd.common.key_offset = keyconf->hw_key_idx;
2409 	u.cmd.common.sta_id = sta_id;
2410 
2411 	size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
2412 
2413 	status = ADD_STA_SUCCESS;
2414 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
2415 					  &status);
2416 
2417 	switch (status) {
2418 	case ADD_STA_SUCCESS:
2419 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2420 		break;
2421 	default:
2422 		ret = -EIO;
2423 		IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2424 		break;
2425 	}
2426 
2427 	return ret;
2428 }
2429 
2430 /*
2431  * Send the FW a request to remove the station from it's internal data
2432  * structures, and in addition remove it from the local data structure.
2433  */
2434 int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2435 {
2436 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2437 	int ret;
2438 
2439 	lockdep_assert_held(&mvm->mutex);
2440 
2441 	iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true, 0);
2442 
2443 	iwl_mvm_disable_txq(mvm, NULL, mvmvif->cab_queue, 0, 0);
2444 
2445 	ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
2446 	if (ret)
2447 		IWL_WARN(mvm, "Failed sending remove station\n");
2448 
2449 	return ret;
2450 }
2451 
2452 #define IWL_MAX_RX_BA_SESSIONS 16
2453 
2454 static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
2455 {
2456 	struct iwl_mvm_rss_sync_notif notif = {
2457 		.metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
2458 		.metadata.sync = 1,
2459 		.delba.baid = baid,
2460 	};
2461 	iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
2462 };
2463 
2464 static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2465 				 struct iwl_mvm_baid_data *data)
2466 {
2467 	int i;
2468 
2469 	iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2470 
2471 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2472 		int j;
2473 		struct iwl_mvm_reorder_buffer *reorder_buf =
2474 			&data->reorder_buf[i];
2475 		struct iwl_mvm_reorder_buf_entry *entries =
2476 			&data->entries[i * data->entries_per_queue];
2477 
2478 		spin_lock_bh(&reorder_buf->lock);
2479 		if (likely(!reorder_buf->num_stored)) {
2480 			spin_unlock_bh(&reorder_buf->lock);
2481 			continue;
2482 		}
2483 
2484 		/*
2485 		 * This shouldn't happen in regular DELBA since the internal
2486 		 * delBA notification should trigger a release of all frames in
2487 		 * the reorder buffer.
2488 		 */
2489 		WARN_ON(1);
2490 
2491 		for (j = 0; j < reorder_buf->buf_size; j++)
2492 			__skb_queue_purge(&entries[j].e.frames);
2493 		/*
2494 		 * Prevent timer re-arm. This prevents a very far fetched case
2495 		 * where we timed out on the notification. There may be prior
2496 		 * RX frames pending in the RX queue before the notification
2497 		 * that might get processed between now and the actual deletion
2498 		 * and we would re-arm the timer although we are deleting the
2499 		 * reorder buffer.
2500 		 */
2501 		reorder_buf->removed = true;
2502 		spin_unlock_bh(&reorder_buf->lock);
2503 		del_timer_sync(&reorder_buf->reorder_timer);
2504 	}
2505 }
2506 
2507 static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2508 					struct iwl_mvm_baid_data *data,
2509 					u16 ssn, u16 buf_size)
2510 {
2511 	int i;
2512 
2513 	for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2514 		struct iwl_mvm_reorder_buffer *reorder_buf =
2515 			&data->reorder_buf[i];
2516 		struct iwl_mvm_reorder_buf_entry *entries =
2517 			&data->entries[i * data->entries_per_queue];
2518 		int j;
2519 
2520 		reorder_buf->num_stored = 0;
2521 		reorder_buf->head_sn = ssn;
2522 		reorder_buf->buf_size = buf_size;
2523 		/* rx reorder timer */
2524 		timer_setup(&reorder_buf->reorder_timer,
2525 			    iwl_mvm_reorder_timer_expired, 0);
2526 		spin_lock_init(&reorder_buf->lock);
2527 		reorder_buf->mvm = mvm;
2528 		reorder_buf->queue = i;
2529 		reorder_buf->valid = false;
2530 		for (j = 0; j < reorder_buf->buf_size; j++)
2531 			__skb_queue_head_init(&entries[j].e.frames);
2532 	}
2533 }
2534 
2535 int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2536 		       int tid, u16 ssn, bool start, u16 buf_size, u16 timeout)
2537 {
2538 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2539 	struct iwl_mvm_add_sta_cmd cmd = {};
2540 	struct iwl_mvm_baid_data *baid_data = NULL;
2541 	int ret;
2542 	u32 status;
2543 
2544 	lockdep_assert_held(&mvm->mutex);
2545 
2546 	if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
2547 		IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2548 		return -ENOSPC;
2549 	}
2550 
2551 	if (iwl_mvm_has_new_rx_api(mvm) && start) {
2552 		u16 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2553 
2554 		/* sparse doesn't like the __align() so don't check */
2555 #ifndef __CHECKER__
2556 		/*
2557 		 * The division below will be OK if either the cache line size
2558 		 * can be divided by the entry size (ALIGN will round up) or if
2559 		 * if the entry size can be divided by the cache line size, in
2560 		 * which case the ALIGN() will do nothing.
2561 		 */
2562 		BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2563 			     sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2564 #endif
2565 
2566 		/*
2567 		 * Upward align the reorder buffer size to fill an entire cache
2568 		 * line for each queue, to avoid sharing cache lines between
2569 		 * different queues.
2570 		 */
2571 		reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2572 
2573 		/*
2574 		 * Allocate here so if allocation fails we can bail out early
2575 		 * before starting the BA session in the firmware
2576 		 */
2577 		baid_data = kzalloc(sizeof(*baid_data) +
2578 				    mvm->trans->num_rx_queues *
2579 				    reorder_buf_size,
2580 				    GFP_KERNEL);
2581 		if (!baid_data)
2582 			return -ENOMEM;
2583 
2584 		/*
2585 		 * This division is why we need the above BUILD_BUG_ON(),
2586 		 * if that doesn't hold then this will not be right.
2587 		 */
2588 		baid_data->entries_per_queue =
2589 			reorder_buf_size / sizeof(baid_data->entries[0]);
2590 	}
2591 
2592 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2593 	cmd.sta_id = mvm_sta->sta_id;
2594 	cmd.add_modify = STA_MODE_MODIFY;
2595 	if (start) {
2596 		cmd.add_immediate_ba_tid = (u8) tid;
2597 		cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
2598 		cmd.rx_ba_window = cpu_to_le16(buf_size);
2599 	} else {
2600 		cmd.remove_immediate_ba_tid = (u8) tid;
2601 	}
2602 	cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2603 				  STA_MODIFY_REMOVE_BA_TID;
2604 
2605 	status = ADD_STA_SUCCESS;
2606 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2607 					  iwl_mvm_add_sta_cmd_size(mvm),
2608 					  &cmd, &status);
2609 	if (ret)
2610 		goto out_free;
2611 
2612 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2613 	case ADD_STA_SUCCESS:
2614 		IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2615 			     start ? "start" : "stopp");
2616 		break;
2617 	case ADD_STA_IMMEDIATE_BA_FAILURE:
2618 		IWL_WARN(mvm, "RX BA Session refused by fw\n");
2619 		ret = -ENOSPC;
2620 		break;
2621 	default:
2622 		ret = -EIO;
2623 		IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2624 			start ? "start" : "stopp", status);
2625 		break;
2626 	}
2627 
2628 	if (ret)
2629 		goto out_free;
2630 
2631 	if (start) {
2632 		u8 baid;
2633 
2634 		mvm->rx_ba_sessions++;
2635 
2636 		if (!iwl_mvm_has_new_rx_api(mvm))
2637 			return 0;
2638 
2639 		if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
2640 			ret = -EINVAL;
2641 			goto out_free;
2642 		}
2643 		baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
2644 			    IWL_ADD_STA_BAID_SHIFT);
2645 		baid_data->baid = baid;
2646 		baid_data->timeout = timeout;
2647 		baid_data->last_rx = jiffies;
2648 		baid_data->rcu_ptr = &mvm->baid_map[baid];
2649 		timer_setup(&baid_data->session_timer,
2650 			    iwl_mvm_rx_agg_session_expired, 0);
2651 		baid_data->mvm = mvm;
2652 		baid_data->tid = tid;
2653 		baid_data->sta_id = mvm_sta->sta_id;
2654 
2655 		mvm_sta->tid_to_baid[tid] = baid;
2656 		if (timeout)
2657 			mod_timer(&baid_data->session_timer,
2658 				  TU_TO_EXP_TIME(timeout * 2));
2659 
2660 		iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size);
2661 		/*
2662 		 * protect the BA data with RCU to cover a case where our
2663 		 * internal RX sync mechanism will timeout (not that it's
2664 		 * supposed to happen) and we will free the session data while
2665 		 * RX is being processed in parallel
2666 		 */
2667 		IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2668 			     mvm_sta->sta_id, tid, baid);
2669 		WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2670 		rcu_assign_pointer(mvm->baid_map[baid], baid_data);
2671 	} else  {
2672 		u8 baid = mvm_sta->tid_to_baid[tid];
2673 
2674 		if (mvm->rx_ba_sessions > 0)
2675 			/* check that restart flow didn't zero the counter */
2676 			mvm->rx_ba_sessions--;
2677 		if (!iwl_mvm_has_new_rx_api(mvm))
2678 			return 0;
2679 
2680 		if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2681 			return -EINVAL;
2682 
2683 		baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2684 		if (WARN_ON(!baid_data))
2685 			return -EINVAL;
2686 
2687 		/* synchronize all rx queues so we can safely delete */
2688 		iwl_mvm_free_reorder(mvm, baid_data);
2689 		del_timer_sync(&baid_data->session_timer);
2690 		RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2691 		kfree_rcu(baid_data, rcu_head);
2692 		IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
2693 	}
2694 	return 0;
2695 
2696 out_free:
2697 	kfree(baid_data);
2698 	return ret;
2699 }
2700 
2701 int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2702 		       int tid, u8 queue, bool start)
2703 {
2704 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2705 	struct iwl_mvm_add_sta_cmd cmd = {};
2706 	int ret;
2707 	u32 status;
2708 
2709 	lockdep_assert_held(&mvm->mutex);
2710 
2711 	if (start) {
2712 		mvm_sta->tfd_queue_msk |= BIT(queue);
2713 		mvm_sta->tid_disable_agg &= ~BIT(tid);
2714 	} else {
2715 		/* In DQA-mode the queue isn't removed on agg termination */
2716 		mvm_sta->tid_disable_agg |= BIT(tid);
2717 	}
2718 
2719 	cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2720 	cmd.sta_id = mvm_sta->sta_id;
2721 	cmd.add_modify = STA_MODE_MODIFY;
2722 	if (!iwl_mvm_has_new_tx_api(mvm))
2723 		cmd.modify_mask = STA_MODIFY_QUEUES;
2724 	cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
2725 	cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2726 	cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2727 
2728 	status = ADD_STA_SUCCESS;
2729 	ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2730 					  iwl_mvm_add_sta_cmd_size(mvm),
2731 					  &cmd, &status);
2732 	if (ret)
2733 		return ret;
2734 
2735 	switch (status & IWL_ADD_STA_STATUS_MASK) {
2736 	case ADD_STA_SUCCESS:
2737 		break;
2738 	default:
2739 		ret = -EIO;
2740 		IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2741 			start ? "start" : "stopp", status);
2742 		break;
2743 	}
2744 
2745 	return ret;
2746 }
2747 
2748 const u8 tid_to_mac80211_ac[] = {
2749 	IEEE80211_AC_BE,
2750 	IEEE80211_AC_BK,
2751 	IEEE80211_AC_BK,
2752 	IEEE80211_AC_BE,
2753 	IEEE80211_AC_VI,
2754 	IEEE80211_AC_VI,
2755 	IEEE80211_AC_VO,
2756 	IEEE80211_AC_VO,
2757 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2758 };
2759 
2760 static const u8 tid_to_ucode_ac[] = {
2761 	AC_BE,
2762 	AC_BK,
2763 	AC_BK,
2764 	AC_BE,
2765 	AC_VI,
2766 	AC_VI,
2767 	AC_VO,
2768 	AC_VO,
2769 };
2770 
2771 int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2772 			     struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2773 {
2774 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2775 	struct iwl_mvm_tid_data *tid_data;
2776 	u16 normalized_ssn;
2777 	u16 txq_id;
2778 	int ret;
2779 
2780 	if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2781 		return -EINVAL;
2782 
2783 	if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
2784 	    mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2785 		IWL_ERR(mvm,
2786 			"Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
2787 			mvmsta->tid_data[tid].state);
2788 		return -ENXIO;
2789 	}
2790 
2791 	lockdep_assert_held(&mvm->mutex);
2792 
2793 	if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE &&
2794 	    iwl_mvm_has_new_tx_api(mvm)) {
2795 		u8 ac = tid_to_mac80211_ac[tid];
2796 
2797 		ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
2798 		if (ret)
2799 			return ret;
2800 	}
2801 
2802 	spin_lock_bh(&mvmsta->lock);
2803 
2804 	/*
2805 	 * Note the possible cases:
2806 	 *  1. An enabled TXQ - TXQ needs to become agg'ed
2807 	 *  2. The TXQ hasn't yet been enabled, so find a free one and mark
2808 	 *	it as reserved
2809 	 */
2810 	txq_id = mvmsta->tid_data[tid].txq_id;
2811 	if (txq_id == IWL_MVM_INVALID_QUEUE) {
2812 		ret = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2813 					      IWL_MVM_DQA_MIN_DATA_QUEUE,
2814 					      IWL_MVM_DQA_MAX_DATA_QUEUE);
2815 		if (ret < 0) {
2816 			IWL_ERR(mvm, "Failed to allocate agg queue\n");
2817 			goto out;
2818 		}
2819 
2820 		txq_id = ret;
2821 
2822 		/* TXQ hasn't yet been enabled, so mark it only as reserved */
2823 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2824 	} else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) {
2825 		ret = -ENXIO;
2826 		IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n",
2827 			tid, IWL_MAX_HW_QUEUES - 1);
2828 		goto out;
2829 
2830 	} else if (unlikely(mvm->queue_info[txq_id].status ==
2831 			    IWL_MVM_QUEUE_SHARED)) {
2832 		ret = -ENXIO;
2833 		IWL_DEBUG_TX_QUEUES(mvm,
2834 				    "Can't start tid %d agg on shared queue!\n",
2835 				    tid);
2836 		goto out;
2837 	}
2838 
2839 	IWL_DEBUG_TX_QUEUES(mvm,
2840 			    "AGG for tid %d will be on queue #%d\n",
2841 			    tid, txq_id);
2842 
2843 	tid_data = &mvmsta->tid_data[tid];
2844 	tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2845 	tid_data->txq_id = txq_id;
2846 	*ssn = tid_data->ssn;
2847 
2848 	IWL_DEBUG_TX_QUEUES(mvm,
2849 			    "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2850 			    mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2851 			    tid_data->next_reclaimed);
2852 
2853 	/*
2854 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
2855 	 * to align the wrap around of ssn so we compare relevant values.
2856 	 */
2857 	normalized_ssn = tid_data->ssn;
2858 	if (mvm->trans->trans_cfg->gen2)
2859 		normalized_ssn &= 0xff;
2860 
2861 	if (normalized_ssn == tid_data->next_reclaimed) {
2862 		tid_data->state = IWL_AGG_STARTING;
2863 		ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
2864 	} else {
2865 		tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2866 		ret = 0;
2867 	}
2868 
2869 out:
2870 	spin_unlock_bh(&mvmsta->lock);
2871 
2872 	return ret;
2873 }
2874 
2875 int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2876 			    struct ieee80211_sta *sta, u16 tid, u16 buf_size,
2877 			    bool amsdu)
2878 {
2879 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2880 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2881 	unsigned int wdg_timeout =
2882 		iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
2883 	int queue, ret;
2884 	bool alloc_queue = true;
2885 	enum iwl_mvm_queue_status queue_status;
2886 	u16 ssn;
2887 
2888 	struct iwl_trans_txq_scd_cfg cfg = {
2889 		.sta_id = mvmsta->sta_id,
2890 		.tid = tid,
2891 		.frame_limit = buf_size,
2892 		.aggregate = true,
2893 	};
2894 
2895 	/*
2896 	 * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
2897 	 * manager, so this function should never be called in this case.
2898 	 */
2899 	if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
2900 		return -EINVAL;
2901 
2902 	BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2903 		     != IWL_MAX_TID_COUNT);
2904 
2905 	spin_lock_bh(&mvmsta->lock);
2906 	ssn = tid_data->ssn;
2907 	queue = tid_data->txq_id;
2908 	tid_data->state = IWL_AGG_ON;
2909 	mvmsta->agg_tids |= BIT(tid);
2910 	tid_data->ssn = 0xffff;
2911 	tid_data->amsdu_in_ampdu_allowed = amsdu;
2912 	spin_unlock_bh(&mvmsta->lock);
2913 
2914 	if (iwl_mvm_has_new_tx_api(mvm)) {
2915 		/*
2916 		 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
2917 		 * would have failed, so if we are here there is no need to
2918 		 * allocate a queue.
2919 		 * However, if aggregation size is different than the default
2920 		 * size, the scheduler should be reconfigured.
2921 		 * We cannot do this with the new TX API, so return unsupported
2922 		 * for now, until it will be offloaded to firmware..
2923 		 * Note that if SCD default value changes - this condition
2924 		 * should be updated as well.
2925 		 */
2926 		if (buf_size < IWL_FRAME_LIMIT)
2927 			return -ENOTSUPP;
2928 
2929 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2930 		if (ret)
2931 			return -EIO;
2932 		goto out;
2933 	}
2934 
2935 	cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
2936 
2937 	queue_status = mvm->queue_info[queue].status;
2938 
2939 	/* Maybe there is no need to even alloc a queue... */
2940 	if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2941 		alloc_queue = false;
2942 
2943 	/*
2944 	 * Only reconfig the SCD for the queue if the window size has
2945 	 * changed from current (become smaller)
2946 	 */
2947 	if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
2948 		/*
2949 		 * If reconfiguring an existing queue, it first must be
2950 		 * drained
2951 		 */
2952 		ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2953 						     BIT(queue));
2954 		if (ret) {
2955 			IWL_ERR(mvm,
2956 				"Error draining queue before reconfig\n");
2957 			return ret;
2958 		}
2959 
2960 		ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2961 					   mvmsta->sta_id, tid,
2962 					   buf_size, ssn);
2963 		if (ret) {
2964 			IWL_ERR(mvm,
2965 				"Error reconfiguring TXQ #%d\n", queue);
2966 			return ret;
2967 		}
2968 	}
2969 
2970 	if (alloc_queue)
2971 		iwl_mvm_enable_txq(mvm, sta, queue, ssn,
2972 				   &cfg, wdg_timeout);
2973 
2974 	/* Send ADD_STA command to enable aggs only if the queue isn't shared */
2975 	if (queue_status != IWL_MVM_QUEUE_SHARED) {
2976 		ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2977 		if (ret)
2978 			return -EIO;
2979 	}
2980 
2981 	/* No need to mark as reserved */
2982 	mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
2983 
2984 out:
2985 	/*
2986 	 * Even though in theory the peer could have different
2987 	 * aggregation reorder buffer sizes for different sessions,
2988 	 * our ucode doesn't allow for that and has a global limit
2989 	 * for each station. Therefore, use the minimum of all the
2990 	 * aggregation sessions and our default value.
2991 	 */
2992 	mvmsta->max_agg_bufsize =
2993 		min(mvmsta->max_agg_bufsize, buf_size);
2994 	mvmsta->lq_sta.rs_drv.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2995 
2996 	IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2997 		     sta->addr, tid);
2998 
2999 	return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.rs_drv.lq);
3000 }
3001 
3002 static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
3003 					struct iwl_mvm_sta *mvmsta,
3004 					struct iwl_mvm_tid_data *tid_data)
3005 {
3006 	u16 txq_id = tid_data->txq_id;
3007 
3008 	lockdep_assert_held(&mvm->mutex);
3009 
3010 	if (iwl_mvm_has_new_tx_api(mvm))
3011 		return;
3012 
3013 	/*
3014 	 * The TXQ is marked as reserved only if no traffic came through yet
3015 	 * This means no traffic has been sent on this TID (agg'd or not), so
3016 	 * we no longer have use for the queue. Since it hasn't even been
3017 	 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
3018 	 * free.
3019 	 */
3020 	if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
3021 		mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
3022 		tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
3023 	}
3024 }
3025 
3026 int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3027 			    struct ieee80211_sta *sta, u16 tid)
3028 {
3029 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3030 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3031 	u16 txq_id;
3032 	int err;
3033 
3034 	/*
3035 	 * If mac80211 is cleaning its state, then say that we finished since
3036 	 * our state has been cleared anyway.
3037 	 */
3038 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3039 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3040 		return 0;
3041 	}
3042 
3043 	spin_lock_bh(&mvmsta->lock);
3044 
3045 	txq_id = tid_data->txq_id;
3046 
3047 	IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
3048 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
3049 
3050 	mvmsta->agg_tids &= ~BIT(tid);
3051 
3052 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3053 
3054 	switch (tid_data->state) {
3055 	case IWL_AGG_ON:
3056 		tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3057 
3058 		IWL_DEBUG_TX_QUEUES(mvm,
3059 				    "ssn = %d, next_recl = %d\n",
3060 				    tid_data->ssn, tid_data->next_reclaimed);
3061 
3062 		tid_data->ssn = 0xffff;
3063 		tid_data->state = IWL_AGG_OFF;
3064 		spin_unlock_bh(&mvmsta->lock);
3065 
3066 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3067 
3068 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3069 		return 0;
3070 	case IWL_AGG_STARTING:
3071 	case IWL_EMPTYING_HW_QUEUE_ADDBA:
3072 		/*
3073 		 * The agg session has been stopped before it was set up. This
3074 		 * can happen when the AddBA timer times out for example.
3075 		 */
3076 
3077 		/* No barriers since we are under mutex */
3078 		lockdep_assert_held(&mvm->mutex);
3079 
3080 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3081 		tid_data->state = IWL_AGG_OFF;
3082 		err = 0;
3083 		break;
3084 	default:
3085 		IWL_ERR(mvm,
3086 			"Stopping AGG while state not ON or starting for %d on %d (%d)\n",
3087 			mvmsta->sta_id, tid, tid_data->state);
3088 		IWL_ERR(mvm,
3089 			"\ttid_data->txq_id = %d\n", tid_data->txq_id);
3090 		err = -EINVAL;
3091 	}
3092 
3093 	spin_unlock_bh(&mvmsta->lock);
3094 
3095 	return err;
3096 }
3097 
3098 int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3099 			    struct ieee80211_sta *sta, u16 tid)
3100 {
3101 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3102 	struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3103 	u16 txq_id;
3104 	enum iwl_mvm_agg_state old_state;
3105 
3106 	/*
3107 	 * First set the agg state to OFF to avoid calling
3108 	 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
3109 	 */
3110 	spin_lock_bh(&mvmsta->lock);
3111 	txq_id = tid_data->txq_id;
3112 	IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
3113 			    mvmsta->sta_id, tid, txq_id, tid_data->state);
3114 	old_state = tid_data->state;
3115 	tid_data->state = IWL_AGG_OFF;
3116 	mvmsta->agg_tids &= ~BIT(tid);
3117 	spin_unlock_bh(&mvmsta->lock);
3118 
3119 	iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3120 
3121 	if (old_state >= IWL_AGG_ON) {
3122 		iwl_mvm_drain_sta(mvm, mvmsta, true);
3123 
3124 		if (iwl_mvm_has_new_tx_api(mvm)) {
3125 			if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
3126 						   BIT(tid), 0))
3127 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3128 			iwl_trans_wait_txq_empty(mvm->trans, txq_id);
3129 		} else {
3130 			if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
3131 				IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3132 			iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
3133 		}
3134 
3135 		iwl_mvm_drain_sta(mvm, mvmsta, false);
3136 
3137 		iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3138 	}
3139 
3140 	return 0;
3141 }
3142 
3143 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
3144 {
3145 	int i, max = -1, max_offs = -1;
3146 
3147 	lockdep_assert_held(&mvm->mutex);
3148 
3149 	/* Pick the unused key offset with the highest 'deleted'
3150 	 * counter. Every time a key is deleted, all the counters
3151 	 * are incremented and the one that was just deleted is
3152 	 * reset to zero. Thus, the highest counter is the one
3153 	 * that was deleted longest ago. Pick that one.
3154 	 */
3155 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3156 		if (test_bit(i, mvm->fw_key_table))
3157 			continue;
3158 		if (mvm->fw_key_deleted[i] > max) {
3159 			max = mvm->fw_key_deleted[i];
3160 			max_offs = i;
3161 		}
3162 	}
3163 
3164 	if (max_offs < 0)
3165 		return STA_KEY_IDX_INVALID;
3166 
3167 	return max_offs;
3168 }
3169 
3170 static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
3171 					       struct ieee80211_vif *vif,
3172 					       struct ieee80211_sta *sta)
3173 {
3174 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3175 
3176 	if (sta)
3177 		return iwl_mvm_sta_from_mac80211(sta);
3178 
3179 	/*
3180 	 * The device expects GTKs for station interfaces to be
3181 	 * installed as GTKs for the AP station. If we have no
3182 	 * station ID, then use AP's station ID.
3183 	 */
3184 	if (vif->type == NL80211_IFTYPE_STATION &&
3185 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3186 		u8 sta_id = mvmvif->ap_sta_id;
3187 
3188 		sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
3189 					    lockdep_is_held(&mvm->mutex));
3190 
3191 		/*
3192 		 * It is possible that the 'sta' parameter is NULL,
3193 		 * for example when a GTK is removed - the sta_id will then
3194 		 * be the AP ID, and no station was passed by mac80211.
3195 		 */
3196 		if (IS_ERR_OR_NULL(sta))
3197 			return NULL;
3198 
3199 		return iwl_mvm_sta_from_mac80211(sta);
3200 	}
3201 
3202 	return NULL;
3203 }
3204 
3205 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
3206 				u32 sta_id,
3207 				struct ieee80211_key_conf *key, bool mcast,
3208 				u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
3209 				u8 key_offset, bool mfp)
3210 {
3211 	union {
3212 		struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3213 		struct iwl_mvm_add_sta_key_cmd cmd;
3214 	} u = {};
3215 	__le16 key_flags;
3216 	int ret;
3217 	u32 status;
3218 	u16 keyidx;
3219 	u64 pn = 0;
3220 	int i, size;
3221 	bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3222 				  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3223 
3224 	if (sta_id == IWL_MVM_INVALID_STA)
3225 		return -EINVAL;
3226 
3227 	keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
3228 		 STA_KEY_FLG_KEYID_MSK;
3229 	key_flags = cpu_to_le16(keyidx);
3230 	key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3231 
3232 	switch (key->cipher) {
3233 	case WLAN_CIPHER_SUITE_TKIP:
3234 		key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
3235 		if (new_api) {
3236 			memcpy((void *)&u.cmd.tx_mic_key,
3237 			       &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
3238 			       IWL_MIC_KEY_SIZE);
3239 
3240 			memcpy((void *)&u.cmd.rx_mic_key,
3241 			       &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
3242 			       IWL_MIC_KEY_SIZE);
3243 			pn = atomic64_read(&key->tx_pn);
3244 
3245 		} else {
3246 			u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3247 			for (i = 0; i < 5; i++)
3248 				u.cmd_v1.tkip_rx_ttak[i] =
3249 					cpu_to_le16(tkip_p1k[i]);
3250 		}
3251 		memcpy(u.cmd.common.key, key->key, key->keylen);
3252 		break;
3253 	case WLAN_CIPHER_SUITE_CCMP:
3254 		key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
3255 		memcpy(u.cmd.common.key, key->key, key->keylen);
3256 		if (new_api)
3257 			pn = atomic64_read(&key->tx_pn);
3258 		break;
3259 	case WLAN_CIPHER_SUITE_WEP104:
3260 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
3261 		/* fall through */
3262 	case WLAN_CIPHER_SUITE_WEP40:
3263 		key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
3264 		memcpy(u.cmd.common.key + 3, key->key, key->keylen);
3265 		break;
3266 	case WLAN_CIPHER_SUITE_GCMP_256:
3267 		key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
3268 		/* fall through */
3269 	case WLAN_CIPHER_SUITE_GCMP:
3270 		key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
3271 		memcpy(u.cmd.common.key, key->key, key->keylen);
3272 		if (new_api)
3273 			pn = atomic64_read(&key->tx_pn);
3274 		break;
3275 	default:
3276 		key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
3277 		memcpy(u.cmd.common.key, key->key, key->keylen);
3278 	}
3279 
3280 	if (mcast)
3281 		key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3282 	if (mfp)
3283 		key_flags |= cpu_to_le16(STA_KEY_MFP);
3284 
3285 	u.cmd.common.key_offset = key_offset;
3286 	u.cmd.common.key_flags = key_flags;
3287 	u.cmd.common.sta_id = sta_id;
3288 
3289 	if (new_api) {
3290 		u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
3291 		size = sizeof(u.cmd);
3292 	} else {
3293 		size = sizeof(u.cmd_v1);
3294 	}
3295 
3296 	status = ADD_STA_SUCCESS;
3297 	if (cmd_flags & CMD_ASYNC)
3298 		ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
3299 					   &u.cmd);
3300 	else
3301 		ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
3302 						  &u.cmd, &status);
3303 
3304 	switch (status) {
3305 	case ADD_STA_SUCCESS:
3306 		IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3307 		break;
3308 	default:
3309 		ret = -EIO;
3310 		IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3311 		break;
3312 	}
3313 
3314 	return ret;
3315 }
3316 
3317 static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3318 				 struct ieee80211_key_conf *keyconf,
3319 				 u8 sta_id, bool remove_key)
3320 {
3321 	struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3322 
3323 	/* verify the key details match the required command's expectations */
3324 	if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3325 		    (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
3326 		    (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
3327 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
3328 		     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
3329 		return -EINVAL;
3330 
3331 	if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
3332 		    keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3333 		return -EINVAL;
3334 
3335 	igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3336 	igtk_cmd.sta_id = cpu_to_le32(sta_id);
3337 
3338 	if (remove_key) {
3339 		/* This is a valid situation for IGTK */
3340 		if (sta_id == IWL_MVM_INVALID_STA)
3341 			return 0;
3342 
3343 		igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3344 	} else {
3345 		struct ieee80211_key_seq seq;
3346 		const u8 *pn;
3347 
3348 		switch (keyconf->cipher) {
3349 		case WLAN_CIPHER_SUITE_AES_CMAC:
3350 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3351 			break;
3352 		case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3353 		case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3354 			igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
3355 			break;
3356 		default:
3357 			return -EINVAL;
3358 		}
3359 
3360 		memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
3361 		if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3362 			igtk_cmd.ctrl_flags |=
3363 				cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3364 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3365 		pn = seq.aes_cmac.pn;
3366 		igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3367 						       ((u64) pn[4] << 8) |
3368 						       ((u64) pn[3] << 16) |
3369 						       ((u64) pn[2] << 24) |
3370 						       ((u64) pn[1] << 32) |
3371 						       ((u64) pn[0] << 40));
3372 	}
3373 
3374 	IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
3375 		       remove_key ? "removing" : "installing",
3376 		       igtk_cmd.sta_id);
3377 
3378 	if (!iwl_mvm_has_new_rx_api(mvm)) {
3379 		struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
3380 			.ctrl_flags = igtk_cmd.ctrl_flags,
3381 			.key_id = igtk_cmd.key_id,
3382 			.sta_id = igtk_cmd.sta_id,
3383 			.receive_seq_cnt = igtk_cmd.receive_seq_cnt
3384 		};
3385 
3386 		memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
3387 		       ARRAY_SIZE(igtk_cmd_v1.igtk));
3388 		return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3389 					    sizeof(igtk_cmd_v1), &igtk_cmd_v1);
3390 	}
3391 	return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3392 				    sizeof(igtk_cmd), &igtk_cmd);
3393 }
3394 
3395 
3396 static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3397 				       struct ieee80211_vif *vif,
3398 				       struct ieee80211_sta *sta)
3399 {
3400 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3401 
3402 	if (sta)
3403 		return sta->addr;
3404 
3405 	if (vif->type == NL80211_IFTYPE_STATION &&
3406 	    mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3407 		u8 sta_id = mvmvif->ap_sta_id;
3408 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3409 						lockdep_is_held(&mvm->mutex));
3410 		return sta->addr;
3411 	}
3412 
3413 
3414 	return NULL;
3415 }
3416 
3417 static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3418 				 struct ieee80211_vif *vif,
3419 				 struct ieee80211_sta *sta,
3420 				 struct ieee80211_key_conf *keyconf,
3421 				 u8 key_offset,
3422 				 bool mcast)
3423 {
3424 	int ret;
3425 	const u8 *addr;
3426 	struct ieee80211_key_seq seq;
3427 	u16 p1k[5];
3428 	u32 sta_id;
3429 	bool mfp = false;
3430 
3431 	if (sta) {
3432 		struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3433 
3434 		sta_id = mvm_sta->sta_id;
3435 		mfp = sta->mfp;
3436 	} else if (vif->type == NL80211_IFTYPE_AP &&
3437 		   !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
3438 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3439 
3440 		sta_id = mvmvif->mcast_sta.sta_id;
3441 	} else {
3442 		IWL_ERR(mvm, "Failed to find station id\n");
3443 		return -EINVAL;
3444 	}
3445 
3446 	switch (keyconf->cipher) {
3447 	case WLAN_CIPHER_SUITE_TKIP:
3448 		addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3449 		/* get phase 1 key from mac80211 */
3450 		ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3451 		ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
3452 		ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3453 					   seq.tkip.iv32, p1k, 0, key_offset,
3454 					   mfp);
3455 		break;
3456 	case WLAN_CIPHER_SUITE_CCMP:
3457 	case WLAN_CIPHER_SUITE_WEP40:
3458 	case WLAN_CIPHER_SUITE_WEP104:
3459 	case WLAN_CIPHER_SUITE_GCMP:
3460 	case WLAN_CIPHER_SUITE_GCMP_256:
3461 		ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3462 					   0, NULL, 0, key_offset, mfp);
3463 		break;
3464 	default:
3465 		ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3466 					   0, NULL, 0, key_offset, mfp);
3467 	}
3468 
3469 	return ret;
3470 }
3471 
3472 int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3473 			struct ieee80211_vif *vif,
3474 			struct ieee80211_sta *sta,
3475 			struct ieee80211_key_conf *keyconf,
3476 			u8 key_offset)
3477 {
3478 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3479 	struct iwl_mvm_sta *mvm_sta;
3480 	u8 sta_id = IWL_MVM_INVALID_STA;
3481 	int ret;
3482 	static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3483 
3484 	lockdep_assert_held(&mvm->mutex);
3485 
3486 	if (vif->type != NL80211_IFTYPE_AP ||
3487 	    keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3488 		/* Get the station id from the mvm local station table */
3489 		mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3490 		if (!mvm_sta) {
3491 			IWL_ERR(mvm, "Failed to find station\n");
3492 			return -EINVAL;
3493 		}
3494 		sta_id = mvm_sta->sta_id;
3495 
3496 		/*
3497 		 * It is possible that the 'sta' parameter is NULL, and thus
3498 		 * there is a need to retrieve the sta from the local station
3499 		 * table.
3500 		 */
3501 		if (!sta) {
3502 			sta = rcu_dereference_protected(
3503 				mvm->fw_id_to_mac_id[sta_id],
3504 				lockdep_is_held(&mvm->mutex));
3505 			if (IS_ERR_OR_NULL(sta)) {
3506 				IWL_ERR(mvm, "Invalid station id\n");
3507 				return -EINVAL;
3508 			}
3509 		}
3510 
3511 		if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3512 			return -EINVAL;
3513 	} else {
3514 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3515 
3516 		sta_id = mvmvif->mcast_sta.sta_id;
3517 	}
3518 
3519 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3520 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3521 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3522 		ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3523 		goto end;
3524 	}
3525 
3526 	/* If the key_offset is not pre-assigned, we need to find a
3527 	 * new offset to use.  In normal cases, the offset is not
3528 	 * pre-assigned, but during HW_RESTART we want to reuse the
3529 	 * same indices, so we pass them when this function is called.
3530 	 *
3531 	 * In D3 entry, we need to hardcoded the indices (because the
3532 	 * firmware hardcodes the PTK offset to 0).  In this case, we
3533 	 * need to make sure we don't overwrite the hw_key_idx in the
3534 	 * keyconf structure, because otherwise we cannot configure
3535 	 * the original ones back when resuming.
3536 	 */
3537 	if (key_offset == STA_KEY_IDX_INVALID) {
3538 		key_offset  = iwl_mvm_set_fw_key_idx(mvm);
3539 		if (key_offset == STA_KEY_IDX_INVALID)
3540 			return -ENOSPC;
3541 		keyconf->hw_key_idx = key_offset;
3542 	}
3543 
3544 	ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
3545 	if (ret)
3546 		goto end;
3547 
3548 	/*
3549 	 * For WEP, the same key is used for multicast and unicast. Upload it
3550 	 * again, using the same key offset, and now pointing the other one
3551 	 * to the same key slot (offset).
3552 	 * If this fails, remove the original as well.
3553 	 */
3554 	if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3555 	     keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
3556 	    sta) {
3557 		ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
3558 					    key_offset, !mcast);
3559 		if (ret) {
3560 			__iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3561 			goto end;
3562 		}
3563 	}
3564 
3565 	__set_bit(key_offset, mvm->fw_key_table);
3566 
3567 end:
3568 	IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3569 		      keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3570 		      sta ? sta->addr : zero_addr, ret);
3571 	return ret;
3572 }
3573 
3574 int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3575 			   struct ieee80211_vif *vif,
3576 			   struct ieee80211_sta *sta,
3577 			   struct ieee80211_key_conf *keyconf)
3578 {
3579 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3580 	struct iwl_mvm_sta *mvm_sta;
3581 	u8 sta_id = IWL_MVM_INVALID_STA;
3582 	int ret, i;
3583 
3584 	lockdep_assert_held(&mvm->mutex);
3585 
3586 	/* Get the station from the mvm local station table */
3587 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3588 	if (mvm_sta)
3589 		sta_id = mvm_sta->sta_id;
3590 	else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
3591 		sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
3592 
3593 
3594 	IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3595 		      keyconf->keyidx, sta_id);
3596 
3597 	if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3598 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3599 	    keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3600 		return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3601 
3602 	if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3603 		IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3604 			keyconf->hw_key_idx);
3605 		return -ENOENT;
3606 	}
3607 
3608 	/* track which key was deleted last */
3609 	for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3610 		if (mvm->fw_key_deleted[i] < U8_MAX)
3611 			mvm->fw_key_deleted[i]++;
3612 	}
3613 	mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3614 
3615 	if (sta && !mvm_sta) {
3616 		IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3617 		return 0;
3618 	}
3619 
3620 	ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3621 	if (ret)
3622 		return ret;
3623 
3624 	/* delete WEP key twice to get rid of (now useless) offset */
3625 	if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3626 	    keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3627 		ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3628 
3629 	return ret;
3630 }
3631 
3632 void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3633 			     struct ieee80211_vif *vif,
3634 			     struct ieee80211_key_conf *keyconf,
3635 			     struct ieee80211_sta *sta, u32 iv32,
3636 			     u16 *phase1key)
3637 {
3638 	struct iwl_mvm_sta *mvm_sta;
3639 	bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3640 	bool mfp = sta ? sta->mfp : false;
3641 
3642 	rcu_read_lock();
3643 
3644 	mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3645 	if (WARN_ON_ONCE(!mvm_sta))
3646 		goto unlock;
3647 	iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
3648 			     iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx,
3649 			     mfp);
3650 
3651  unlock:
3652 	rcu_read_unlock();
3653 }
3654 
3655 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3656 				struct ieee80211_sta *sta)
3657 {
3658 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3659 	struct iwl_mvm_add_sta_cmd cmd = {
3660 		.add_modify = STA_MODE_MODIFY,
3661 		.sta_id = mvmsta->sta_id,
3662 		.station_flags_msk = cpu_to_le32(STA_FLG_PS),
3663 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3664 	};
3665 	int ret;
3666 
3667 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3668 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3669 	if (ret)
3670 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3671 }
3672 
3673 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3674 				       struct ieee80211_sta *sta,
3675 				       enum ieee80211_frame_release_type reason,
3676 				       u16 cnt, u16 tids, bool more_data,
3677 				       bool single_sta_queue)
3678 {
3679 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3680 	struct iwl_mvm_add_sta_cmd cmd = {
3681 		.add_modify = STA_MODE_MODIFY,
3682 		.sta_id = mvmsta->sta_id,
3683 		.modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3684 		.sleep_tx_count = cpu_to_le16(cnt),
3685 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3686 	};
3687 	int tid, ret;
3688 	unsigned long _tids = tids;
3689 
3690 	/* convert TIDs to ACs - we don't support TSPEC so that's OK
3691 	 * Note that this field is reserved and unused by firmware not
3692 	 * supporting GO uAPSD, so it's safe to always do this.
3693 	 */
3694 	for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3695 		cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3696 
3697 	/* If we're releasing frames from aggregation or dqa queues then check
3698 	 * if all the queues that we're releasing frames from, combined, have:
3699 	 *  - more frames than the service period, in which case more_data
3700 	 *    needs to be set
3701 	 *  - fewer than 'cnt' frames, in which case we need to adjust the
3702 	 *    firmware command (but do that unconditionally)
3703 	 */
3704 	if (single_sta_queue) {
3705 		int remaining = cnt;
3706 		int sleep_tx_count;
3707 
3708 		spin_lock_bh(&mvmsta->lock);
3709 		for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3710 			struct iwl_mvm_tid_data *tid_data;
3711 			u16 n_queued;
3712 
3713 			tid_data = &mvmsta->tid_data[tid];
3714 
3715 			n_queued = iwl_mvm_tid_queued(mvm, tid_data);
3716 			if (n_queued > remaining) {
3717 				more_data = true;
3718 				remaining = 0;
3719 				break;
3720 			}
3721 			remaining -= n_queued;
3722 		}
3723 		sleep_tx_count = cnt - remaining;
3724 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
3725 			mvmsta->sleep_tx_count = sleep_tx_count;
3726 		spin_unlock_bh(&mvmsta->lock);
3727 
3728 		cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3729 		if (WARN_ON(cnt - remaining == 0)) {
3730 			ieee80211_sta_eosp(sta);
3731 			return;
3732 		}
3733 	}
3734 
3735 	/* Note: this is ignored by firmware not supporting GO uAPSD */
3736 	if (more_data)
3737 		cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
3738 
3739 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3740 		mvmsta->next_status_eosp = true;
3741 		cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
3742 	} else {
3743 		cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
3744 	}
3745 
3746 	/* block the Tx queues until the FW updated the sleep Tx count */
3747 	iwl_trans_block_txq_ptrs(mvm->trans, true);
3748 
3749 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3750 				   CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3751 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3752 	if (ret)
3753 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3754 }
3755 
3756 void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3757 			   struct iwl_rx_cmd_buffer *rxb)
3758 {
3759 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3760 	struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3761 	struct ieee80211_sta *sta;
3762 	u32 sta_id = le32_to_cpu(notif->sta_id);
3763 
3764 	if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
3765 		return;
3766 
3767 	rcu_read_lock();
3768 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3769 	if (!IS_ERR_OR_NULL(sta))
3770 		ieee80211_sta_eosp(sta);
3771 	rcu_read_unlock();
3772 }
3773 
3774 void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3775 				   struct iwl_mvm_sta *mvmsta, bool disable)
3776 {
3777 	struct iwl_mvm_add_sta_cmd cmd = {
3778 		.add_modify = STA_MODE_MODIFY,
3779 		.sta_id = mvmsta->sta_id,
3780 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3781 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3782 		.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3783 	};
3784 	int ret;
3785 
3786 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3787 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3788 	if (ret)
3789 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3790 }
3791 
3792 void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3793 				      struct ieee80211_sta *sta,
3794 				      bool disable)
3795 {
3796 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3797 
3798 	spin_lock_bh(&mvm_sta->lock);
3799 
3800 	if (mvm_sta->disable_tx == disable) {
3801 		spin_unlock_bh(&mvm_sta->lock);
3802 		return;
3803 	}
3804 
3805 	mvm_sta->disable_tx = disable;
3806 
3807 	/* Tell mac80211 to start/stop queuing tx for this station */
3808 	ieee80211_sta_block_awake(mvm->hw, sta, disable);
3809 
3810 	iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3811 
3812 	spin_unlock_bh(&mvm_sta->lock);
3813 }
3814 
3815 static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
3816 					      struct iwl_mvm_vif *mvmvif,
3817 					      struct iwl_mvm_int_sta *sta,
3818 					      bool disable)
3819 {
3820 	u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
3821 	struct iwl_mvm_add_sta_cmd cmd = {
3822 		.add_modify = STA_MODE_MODIFY,
3823 		.sta_id = sta->sta_id,
3824 		.station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3825 		.station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3826 		.mac_id_n_color = cpu_to_le32(id),
3827 	};
3828 	int ret;
3829 
3830 	ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, 0,
3831 				   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3832 	if (ret)
3833 		IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3834 }
3835 
3836 void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3837 				       struct iwl_mvm_vif *mvmvif,
3838 				       bool disable)
3839 {
3840 	struct ieee80211_sta *sta;
3841 	struct iwl_mvm_sta *mvm_sta;
3842 	int i;
3843 
3844 	lockdep_assert_held(&mvm->mutex);
3845 
3846 	/* Block/unblock all the stations of the given mvmvif */
3847 	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
3848 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3849 						lockdep_is_held(&mvm->mutex));
3850 		if (IS_ERR_OR_NULL(sta))
3851 			continue;
3852 
3853 		mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3854 		if (mvm_sta->mac_id_n_color !=
3855 		    FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3856 			continue;
3857 
3858 		iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3859 	}
3860 
3861 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3862 		return;
3863 
3864 	/* Need to block/unblock also multicast station */
3865 	if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
3866 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3867 						  &mvmvif->mcast_sta, disable);
3868 
3869 	/*
3870 	 * Only unblock the broadcast station (FW blocks it for immediate
3871 	 * quiet, not the driver)
3872 	 */
3873 	if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
3874 		iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3875 						  &mvmvif->bcast_sta, disable);
3876 }
3877 
3878 void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3879 {
3880 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3881 	struct iwl_mvm_sta *mvmsta;
3882 
3883 	rcu_read_lock();
3884 
3885 	mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3886 
3887 	if (!WARN_ON(!mvmsta))
3888 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3889 
3890 	rcu_read_unlock();
3891 }
3892 
3893 u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
3894 {
3895 	u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3896 
3897 	/*
3898 	 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
3899 	 * to align the wrap around of ssn so we compare relevant values.
3900 	 */
3901 	if (mvm->trans->trans_cfg->gen2)
3902 		sn &= 0xff;
3903 
3904 	return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
3905 }
3906