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