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